1.
2. Implementing the Code
Activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= " Match_parent " android:layout_height=" Match_parent "> <EditText android:id=" @+id/txtsearch " android:layout_width=" Match_parent " android:layout_height=" Wrap_ Content " android:hint=" Please enter "/></linearlayout>
Mainactivity.java
Importandroid.app.Activity;Importandroid.content.res.Resources;Importandroid.graphics.drawable.Drawable;ImportAndroid.os.Bundle;Importandroid.text.Editable;ImportAndroid.text.InputType;Importandroid.text.TextUtils;ImportAndroid.text.TextWatcher;ImportAndroid.view.Menu;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.View.OnTouchListener;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {PrivateDrawable Miconsearchdefault;//Search text box default icon PrivateDrawable Miconsearchclear;//Search text box Clear text content icon PrivateEditText Msearchview =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //get the icon file inside the resource FinalResources res =getresources (); //the default iconMiconsearchdefault =res.getdrawable (R.drawable.txt_search_default); //Clear IconMiconsearchclear =res.getdrawable (r.drawable.txt_search_clear); Msearchview=(EditText) Findviewbyid (R.id.txtsearch); Msearchview.addtextchangedlistener (tbxsearch_textchanged); Msearchview.setontouchlistener (Txtsearch_ontouch); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } /*** Determine if there is data in the input box, and then display the corresponding icon file*/ PrivateTextwatcher tbxsearch_textchanged =NewTextwatcher () {//caches whether the last text box is empty Private BooleanIsNull =true; @Override Public voidaftertextchanged (Editable s) {if(Textutils.isempty (s)) {if(!isnull) {Msearchview.setcompounddrawableswithintrinsicbounds (NULL, NULL, Miconsearchdefault,NULL); IsNull=true; } } Else { if(IsNull) {msearchview.setcompounddrawableswithintrinsicbounds (NULL, NULL, Miconsearchclear,NULL); IsNull=false; } }} @Override Public voidBeforetextchanged (Charsequence S,intStartintCount,intAfter ) { } /*** Change list content dynamically as text box contents change*/@Override Public voidOnTextChanged (Charsequence S,intStartintbefore,intcount) { } }; //handle event when clear icon is clicked PrivateOntouchlistener Txtsearch_ontouch =NewOntouchlistener () {@Override Public BooleanOnTouch (View V, motionevent event) {Switch(Event.getaction ()) { Casemotionevent.action_up:intCurX = (int) Event.getx (); if(CurX > V.getwidth ()-&&!Textutils.isempty (Msearchview.gettext ())) {Msearchview.settext (""); intCacheinputtype = Msearchview.getinputtype ();//Backup//The input//typeMsearchview.setinputtype (Inputtype.type_null);//Disable//Soft//inputMsearchview.ontouchevent (event);//Call Native HandlerMsearchview.setinputtype (Cacheinputtype);//Restore InputToast toast = Toast.maketext (mainactivity. This, "Hello.", Toast.length_short); Toast.show (); //type return true;//consume touch even } Break; } return false; } };}
3. Pictures to use
4. Description
In the farmer Uncle's blog to see, because can not find the URL, hereby explain
Android--edit box change style