"Android Source resolution" Custom can erase the input box

Source: Internet
Author: User


Today to share this about EditText, before the request can be a key to clear the input box, think about the idea, you can put a small clear picture next to the input box, and then to EditText and clear the small picture into the layout, to the layout of a background picture, it looks more beautiful, Then according to Edittext.gettext (). Length to set the small picture is visible, feel also OK. But as I have seen more of it, I find that although it can be realized, it is really water, so I want to customize a edittext that can be cleared.

Let's talk about your ideas:

1. First customize the component inheritance EditText

2. Override the construction method, initialize a small icon with a clear button, and set its position

3. Monitor EditText focus change, show hidden small icon according to focus change

4. Rewrite the button click event for a small icon

Put some code on it:

/** * Here we can't set EditText click events, so we can do this: * When we press the position is in (the width of the input box-the width of the icon-the width of the icon distance to the right) and (input box     Width-icon to the right of the width) between * that is, we press the position near the icon, set the input box * by Hankkin at:2015 August 11 16:45:42 * @param event * @return            */@Override public boolean ontouchevent (Motionevent event) {if (Getcompounddrawables () [2]!=null) { if (event.getaction () = = Motionevent.action_up) {Boolean touchable = Event.getx () > (getwidth ()-getpa Ddingright ()-deletedraw.getintrinsicwidth ()) && (Event.getx () < ((GetWidth ()-getpadding                Right ())));                if (touchable) {SetText ("");    }}} return Super.ontouchevent (event); }
/**     * Set clear whether the picture is visible     * by Hankkin at:2015 August 11 16:17:24     * @param isVisible */    private void Setdeletevisible (Boolean isVisible) {        drawable drawable = isvisible?deletedraw:null;        Setcompounddrawables (Getcompounddrawables () [0],getcompounddrawables () [1],drawable,getcompounddrawables () [3]);    }    /**     * Change the display of the image according to the focus of the edittext or not, if the string length in the input box is greater than 0, it is shown, otherwise hidden     * by Hankkin at:2015 year August 11 16:32:47     * @param v     * @param hasfocus */    @Override public    void Onfocuschange (View V, boolean hasfocus) {        if ( Hasfocus) {            setdeletevisible (GetText (). Length () >0);        } else {            setdeletevisible (false);        }    }

And then directly referencing this control in our XML is OK.

Source Address:

http://download.csdn.net/detail/lyhhj/8991711

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Android Source resolution" Custom can erase the input box

Related Article

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.