"Android-edittext" Custom EditText with delete feature

Source: Internet
Author: User
Tags gety

We often see in some applications the input box with the deletion function, today we come to realize this function (text organization ability is not strong, we casually look at). The main is to record their own learning experience, if you have help, I will be more happy.

First:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/25/F4/wKiom1NoiWKxPyZ1AAF6K_ZbO_g941.jpg "title=" Edittextwithdelete.png "alt=" Wkiom1noiwkxpyz1aaf6k_zbo_g941.jpg "/>

Key points of implementation:

1, when the input box is empty, the delete button is hidden;

2. When the input box is not empty, the Delete button is displayed.


Core code:

Package Com.example.view;import Com.example.ui.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.view.view;import Android.widget.EditText ; import Android.widget.toast;import Android.view.View.OnFocusChangeListener;;    public class Edittextwithdelete extends EditText implements onfocuschangelistener{private drawable imgenable;                                                                                    Private context context;        Public Edittextwithdelete (Context context) {super (context);        This.context = context;    Init (); } public Edittextwithdelete (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle)        ;        This.context = context;    Init ();       } public Edittextwithdelete (context context, AttributeSet Attrs) { Super (context, attrs);        This.context = context;    Init ();         } private void Init () {//Get Picture Resource        imgenable = Context.getresources (). getdrawable (R.drawable.delete);                                                                                                    Addtextchangedlistener (New Textwatcher () {                                                                                                        @Override public void ontextchanged (charsequence s, int start, int before, int count) {                                                                                                    } @Override public void Beforetext                                                                                                        Changed (charsequence s, int start, int count, int after) {                                                     }                                               @Override public void aftertextchanged (Editable s) {                Setdrawable ();            Toast.maketext (Context, GetText (), ten). Show ();        }        });    Setdrawable (); }/** * Set Delete picture */Private VO        ID setdrawable () {if (length () = = 0) {setcompounddrawableswithintrinsicbounds (null, NULL, NULL, NULL);        }else {setcompounddrawableswithintrinsicbounds (null, NULL, imgenable, NULL); }}/** * EVENT.GETX () gets relative to its upper left The x-coordinate of the angle * event.gety () gets the y-coordinate relative to the upper-left corner of the getwidth () gets the width of the control * Gettotalpaddingright () Gets the distance from the left edge of the delete icon to the right edge of the control * get Paddingright () Gets the distance from the right edge of the delete icon to the control's left margin * getwidth ()-Gettotalpaddingright () calculates the distance from the left edge of the icon to the control (). * GetWidth ()-Getpaddi Ngright () calculates the distance from the right edge of the delete icon to the left margin of the control */    @Override public boolean ontouchevent (Motionevent event) {if (imgenable! = null && event.getaction ()            = = motionevent.action_up) {int x = (int) event.getx ();                    Determine if the touch point is within the horizontal range Boolean isinnerwidth = (x > (GetWidth ()-gettotalpaddingright ())) &&            (X < (getwidth ()-getpaddingright ()));            Gets the bounds of the delete icon, returning a Rect object rect rect = Imgenable.getbounds ();            Gets the height of the delete icon int height = rect.height ();            int y = (int) event.gety ();            Calculates the distance from the bottom of the icon to the bottom of the control int distance = (getheight ()-height)/2; Determine if the touch point is in the vertical range (there may be a bit of error)//The ordinate of the touch point is within the distance to (the height of the distance+ icon itself), it is considered as a point in the delete icon Boolean isinnerheight = (                                                                                                    Y > Distance) && (Y < (distance + height));     if (isinnerwidth && isinnerheight) {setText ("");       }                                                                                                }    Return Super.ontouchevent (event); } @Override protected void Finalize (    ) throws Throwable {super.finalize ();        } @Override public void Onfocuschange (View V, boolean hasfocus) {if (Hasfocus) {setdrawable ();        }else {setcompounddrawableswithintrinsicbounds (null, NULL, NULL, NULL); }    }                                                                                }

Code comments are very clear, I believe in the level of everyone will see understand. On this side, I don't have much to explain. If you do not understand, you can give me a message, we exchange.

This article is from the "Simple It Life" blog, so be sure to keep this source http://jychen.blog.51cto.com/4792328/1407209

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.