In project development, with the Delete button input box is also commonly used by people, the article describes how to create a box with the delete input. Among the problems that need to be addressed, such as the following:
A) Create your own definition EditText class
b) Display the delete picture in your own definition EditText
c) Show or hide the image based on input from the input box
d) Click Delete Picture text disappears, picture hidden
e) Show or hide the picture by losing and gaining status based on the input box focus
All right. The problem is clear. Start implementing Features:
A) Create a class file named Myclearedittext, and integrate the EditText to implement its construction method:
Public Myclearedittext (Context context) {This (context, null);//TODO auto-generated constructor Stub}public Myclearedittext (context context, AttributeSet Attrs) {This (context, Attrs, Android. R.attr.edittextstyle);//TODO auto-generated Constructor Stub}public Myclearedittext (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);}
OK, the first question has been conquered, and the second step has been reached.
b) in the EditText. If we want to show the picture of the upper and lower left and right direction, there is setcompounddrawables or Setcompounddrawableswithintrinsicbounds method, detailed words. Can go to Baidu a bit of the difference, here, I use the Setcompounddrawableswithintrinsicbounds method. The code is as follows:
/** * Initialize cleared picture */private void initcleardrawable () {draw = Getcompounddrawables () [2];//infers whether the cleared picture is empty if (draw = = null) {Draw = Getresources (). getdrawable (R.drawable.editdelete);} Set the picture for the input box this.setcompounddrawableswithintrinsicbounds (null, NULL, draw, NULL);}
The idea is: first find the EditText in the right of the picture, if NULL, then set the default picture for it. Then show the picture for the input box and get the result:
c) The need to obtain the input box situation. To implement the Textwatcher interface.
Listening:
This.addtextchangedlistener (this);
Methods to be implemented:
public void ontextchanged (charsequence text, int start, int lengthbefore,int lengthafter) {//Infer if the input box has content if (Text.length ( ) > 0) {this.setcompounddrawableswithintrinsicbounds (null, NULL, draw, NULL);} else { This.setcompounddrawableswithintrinsicbounds (NULL, NULL, NULL, NULL);}} public void beforetextchanged (Charsequence s, int. start, int count,int after) {//TODO auto-generated method Stub}public V OID aftertextchanged (Editable s) {//TODO auto-generated method stub}
d) How to monitor is click to delete the picture, this is a question worth thinking, here, there is a way to solve. That's the contact monitoring. Infer whether the position of the picture is within the range of the location according to the location of the click:
@Overridepublic boolean ontouchevent (Motionevent event) {//infer whether the touch ends if (event.getaction () = = motionevent.action_up) {// Infer if the position touched is cleared buttonif (Event.getx () > (GetWidth ()-gettotalpaddingright ()) && Event.getx () < (GetWidth ( )-Getpaddingright ()) {//To clear the contents of EditText inside SetText ("");}} Return Super.ontouchevent (event);}
After implementing the above steps, the general definition of the Delete input box function can be achieved, but there are some problems, if there are two input boxes. When you enter text into one of the input boxes, click another input box, and the previous input box will still display the delete picture, and the workaround is as follows:
e) Since it is based on the focus of gain and loss to infer, of course, to achieve focus monitoring method:
@Overrideprotected void Onfocuschanged (Boolean focused, int direction,rect previouslyfocusedrect) {//TODO Auto-generated method Stubsuper.onfocuschanged (focused, direction, previouslyfocusedrect);//infer that the focus loses and gets when the action if ( Focused &&!this.gettext (). toString (). Equals ("")) {this.setcompounddrawableswithintrinsicbounds (null, NULL , draw, null);} else {this.setcompounddrawableswithintrinsicbounds (null, NULL, NULL, NULL);}}
Ok. The full version of your own definition with the Delete input box is fully implemented. For the convenience of learning, the following is the complete code:
Package Com.xiaoyan.xiaoyanlibrary.common.widget.edittext;import Com.xiaoyan.xiaoyanlibrary.r;import Android.content.context;import Android.graphics.rect;import Android.graphics.drawable.drawable;import Android.text.editable;import Android.text.textwatcher;import Android.util.attributeset;import Android.view.motionevent;import android.widget.edittext;/** * Define a EditText with clear function * * @author Xiejinxiong * */public C Lass Myclearedittext extends EditText implements Textwatcher {/** store erased pictures */private drawable draw;public myclearedittext ( Context context) {This (context, null);//TODO auto-generated Constructor Stub}public Myclearedittext (context context, AttributeSet attrs) {This (context, Attrs, Android. R.attr.edittextstyle);//TODO auto-generated Constructor Stub}public Myclearedittext (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); initcleardrawable (); This.addtextchangedlistener (this);} @Overrideprotected void Onfocuschanged (Boolean focused, int directioN,rect previouslyfocusedrect) {//TODO auto-generated method Stubsuper.onfocuschanged (focused, direction, Previouslyfocusedrect);//infer that the focus loses and gets when the operation if (focused &&!this.gettext (). toString (). Equals ("")) { This.setcompounddrawableswithintrinsicbounds (null, NULL, draw, NULL);} else {this.setcompounddrawableswithintrinsicbounds (null, NULL, NULL, NULL);}} /** * Initialize cleared picture */private void initcleardrawable () {draw = Getcompounddrawables () [2];//infers whether the cleared picture is empty if (draw = = null) {Draw = Getresources (). getdrawable (R.drawable.editdelete);} The input box is set by default to Buttonthis.setcompounddrawableswithintrinsicbounds (NULL, NULL, NULL, NULL) without purging;} public void ontextchanged (charsequence text, int start, int lengthbefore,int lengthafter) {//Infer if the input box has content if (Text.length ( ) > 0) {this.setcompounddrawableswithintrinsicbounds (null, NULL, draw, NULL);} else { This.setcompounddrawableswithintrinsicbounds (NULL, NULL, NULL, NULL);}} public void beforetextchanged (Charsequence s, int. start, int count,int after) {//TODO Auto-geneRated method Stub}public void aftertextchanged (Editable s) {//TODO auto-generated Method stub} @Overridepublic Boolean OnT Ouchevent (Motionevent event) {//infer if the touch ends if (event.getaction () = = motionevent.action_up) {//infer if the position touched is a cleared buttonif ( Event.getx () > (GetWidth ()-gettotalpaddingright ()) && Event.getx () < (getwidth ()-getpaddingright ())) {/ /Remove the contents of the EditText settext ("");}} Return Super.ontouchevent (event);}}
[Android] own definition with delete input box