Android EditText实现关键词批量搜索示例
今天在项目中用到了用到了一种特殊的EditText,当用户在EditText中输入内容,点击搜索按钮的时候,输入的内容能够高亮,然后添加到输入的容器中。删除的时候,能够将容器中的关键词逐一删除。附上代码:
SearchEditText.java
packagecom.jackie.searchresultedittext; importandroid.content.Context; importandroid.graphics.Color; importandroid.util.AttributeSet; importandroid.view.Gravity; importandroid.view.KeyEvent; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.inputmethod.EditorInfo; importandroid.widget.EditText; importandroid.widget.LinearLayout; importandroid.widget.RelativeLayout; importandroid.widget.TextView; /** *CreatedbyJackieon2017/2/21. *用于搜索的EditText */ publicclassSearchEditTextextendsRelativeLayout{ privateContextmContext; privateLayoutInflatermInflater; privateViewmView; privateLinearLayoutmContainer; privateEditTextmEditText=null; publicSearchEditText(Contextcontext){ this(context,null); } publicSearchEditText(Contextcontext,AttributeSetattrs){ this(context,attrs,0); } publicSearchEditText(Contextcontext,AttributeSetattrs,intdefStyleAttr){ super(context,attrs,defStyleAttr); init(context); } privateOnSearchChangeListenermSearchChangeListener; publicinterfaceOnSearchChangeListener{ voidsearchChange(Strings); voidremoveView(intposition); } publicvoidsetOnSearchChangeListener(OnSearchChangeListenersearchChangeListener){ mSearchChangeListener=searchChangeListener; } privatevoidinit(Contextcontext){ mContext=context; mInflater=LayoutInflater.from(mContext); mView=mInflater.inflate(R.layout.search_edittext_layout,null); LayoutParamsparams=newLayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); params.leftMargin=15; params.rightMargin=15; addView(mView,params); mContainer=(LinearLayout)mView.findViewById(R.id.layout); mEditText=(EditText)mView.findViewById(R.id.edittext); mEditText.setOnKeyListener(newOnKeyListener(){ @Override publicbooleanonKey(Viewv,intkeyCode,KeyEventevent){ if(keyCode==KeyEvent.KEYCODE_DEL){ if(isNotFastClick()){ if(mEditText.getText().toString().length()>0){ Stringstr=mEditText.getText().toString(); str=str.substring(0,str.length()-1); mEditText.setText(str); mEditText.setSelection(str.length()); }else{ if(mContainer.getChildCount()>0){ if(mSearchChangeListener!=null){ mSearchChangeListener.removeView(mContainer.getChildCount()-1); } mContainer.removeViewAt(mContainer.getChildCount()-1); } } } } returntrue; } }); mEditText.setOnEditorActionListener(newTextView.OnEditorActionListener(){ @Override publicbooleanonEditorAction(TextViewv,intactionId,KeyEventevent){ if(actionId==EditorInfo.IME_ACTION_SEARCH){ if(mEditText.getText().toString().trim().equals("")){ returntrue; } TextViewtextView=newTextView(mContext); textView.setText(mEditText.getText().toString().trim()); textView.setTextSize(14); textView.setTextColor(Color.parseColor("#dfe0e0")); textView.setPadding(10,0,10,0); textView.setBackgroundResource(R.drawable.shape_edittext_round_bg); textView.setGravity(Gravity.CENTER); LinearLayout.LayoutParamsparams=newLinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT); params.leftMargin=10; textView.setLayoutParams(params); if(mSearchChangeListener!=null){ mSearchChangeListener.searchChange(mEditText.getText().toString().trim()); } mEditText.setText(""); mContainer.addView(textView); } returntrue; } }); } publicEditTextgetEditText(){ returnmEditText; } publicLinearLayoutgetContainer(){ returnmContainer; } longlastClickTime=0; publicbooleanisNotFastClick(){ longtime=System.currentTimeMillis(); if(time-lastClickTime>=300){ lastClickTime=time; returntrue; }else{ returnfalse; } } }
search_edittext_layout.xml
<?xmlversion="1.0"encoding="utf-8"?> <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" android:fillViewport="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="33dp" android:orientation="horizontal"> <LinearLayout android:id="@+id/layout" android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_vertical" android:layout_gravity="center_vertical"/> <EditText android:gravity="center_vertical" android:layout_gravity="center_vertical" android:id="@+id/edittext" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@null" android:textSize="16dp" android:textColor="#dfe0e0" android:layout_weight="1" android:minWidth="50dp" android:imeOptions="actionSearch" android:singleLine="true" android:layout_marginLeft="10dp"/> </LinearLayout> </HorizontalScrollView>
shape_edittext_round_bg.xml
<?xmlversion="1.0"encoding="utf-8"?> <shapexmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solidandroid:color="#666666"/> <cornersandroid:radius="10dp"/> </shape>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。