In Android we use the default EditView is only input text, but want to delete, we have to use the Input Method Delete button to delete, now in a lot of applications, will appear in the input box at the end of a delete image, click on the empty all the data, this is very convenient. Let's take a look at the following.
First look at the following:
What we do here is that this delete icon appears only when the input box has text. When the input is empty, it disappears, in fact it is a custom Ediiview:
/** * Input text box to the right there is a delete button when there is input, the Delete button is displayed when there is no input, the Delete button is not displayed. * * */public class Clearedittext extends EditText implements Onfocuschangelistener, Textwatcher {/** * Delete button reference */private drawable mcleardrawable;/** * Control has focus */private Boolean Hasfoucs;public Clearedittext (context context) {This (context, Nu ll);} Public Clearedittext (context context, AttributeSet attrs) {//Here The construction method is also very important, without this many attributes can no longer define this in XML (context, Attrs, Android. R.attr.edittextstyle);} Public Clearedittext (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); init ();} private void Init () {//Get edittext drawableright, if not set we'll use the default picture mcleardrawable = Getcompounddrawables () [2];if ( Mcleardrawable = = null) {//Throw new//nullpointerexception ("You can add Drawableright attribute in XML"); mcleardrawable = Getresources (). getdrawable (R.drawable.button_login_delete);} Mcleardrawable.setbounds (0, 0, mcleardrawable.getintrinsicwidth (), Mcleardrawable.getintrinsicheight ());// The default setting hides the icon setcleariconvisible (false);//Set the focusClick to change the monitor setonfocuschangelistener (this),//Set the input box to change the contents of the monitoring Addtextchangedlistener (this);} /** * Because we can't set the click event directly to EditText, so we'll use the position we pressed to simulate the click event when we press the position in the width of the edittext-* icon to the right of the control spacing-the width of the icon and the width of the EditText-icon to the right of the control Between the spacing we click on the icon, the vertical direction is not considered */@Overridepublic boolean ontouchevent (Motionevent event) {if (event.getaction () = = MOTIONEVENT.ACTION_UP) {if (Getcompounddrawables () [2] = null) {Boolean touchable = Event.getx () > (GetWidth ()-Gettot Alpaddingright ()) && (Event.getx () < ((GetWidth ()-getpaddingright ())); if (touchable) {This.settext ("");}}} Return Super.ontouchevent (event);} /** * When the clearedittext focus changes, determine the length of the string inside the set clear icon display and hide */@Overridepublic void Onfocuschange (View V, Boolean hasfocus) { This.hasfoucs = Hasfocus;if (hasfocus) {setcleariconvisible (GetText (). Length () > 0);} else {setcleariconvisible ( false);}} /** * Set clear icon display and hide, call Setcompounddrawables to EditText draw up * * @param visible */protected void Setcleariconvisible (Boolean V isible) {drawable right = visible? McleardrawaBle:null;setcompounddrawables (Getcompounddrawables () [0], Getcompounddrawables () [1], right, getcompounddrawables () [3]);} /** * The method of callback when the contents of the input box are changed */@Overridepublic void ontextchanged (charsequence s, int start, int count, int after) {if (hasf Oucs) {setcleariconvisible (s.length () > 0);}} @Overridepublic void Beforetextchanged (charsequence s, int start, int count, int after) {} @Overridepublic void Aftertextch Anged (Editable s) {}}
When we use it, just replace the EditView, and we'll just have to customize it:
<com.test.view.clearedittext android:id= "@+id/edit_user" android:layout_width= "1px" android: layout_height= "Fill_parent" android:layout_marginleft= "10.0dip" android:layout_marginright= "15.0dip" android:layout_weight= "3" android:background= "@android: Color/white" android:hint= "account/email/mobile number" android:inputtype= "Textpersonname" android:maxlength= " android:paddingleft=" 10.0dip " Android:singleline= "true" android:textsize= "16.0sp"/>
This will have the effect we want. I will not provide the complete demo.
Android Custom input box with delete icon EditView