Android with recognition removal rate edittext (it comes with a shake-out effect)

Source: Internet
Author: User

Android It does not provide a clear effect like the input box to iOS comes with (iOS simply adds attributes to achieve). So, Android where we want to implement it requires this effect to be implemented with your own defined controls.

Idea: Be able to use a linearlayout inside a horizontal layout with a edittext and a deleted picture. Listen for focus and text changes in the input box, set the explicit and click Erase events for the image. However, to do this, first add the UI layout hierarchy is not conducive to the optimization of the UI structure and may appear too long to block the picture of the situation.

So the use of their own definition of the control inherited from the EditText, using Getcompounddrawables to get up and down to join the picture, through the monitoring focus changes and input content changes to control the surrounding image of the explicit and clear events, (there is also attached a shaking animation, For example, when the register assumes that the input is empty to be able to shake the hint).

The principle is very simple directly on the code:

public class Clearedittext extends EditText implements Onfocuschangelistener,textwatcher {/** * Remove the reference to button */    Private drawable mcleardrawable;    Private Boolean Hasfoucs;    Public Clearedittext (Context context) {This (context, NULL); The public clearedittext (context context, AttributeSet attrs) {//The construction method here is also very important. Not adding this very many attributes can no longer be defined within the XML inside this (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 will use the default picture, 2 is to get to the right of the picture order is left upper right bottom (0,1,2,3,) Mcleard        rawable = Getcompounddrawables () [2]; if (mcleardrawable = = null) {//throw new//NullPointerException ("You can add drawableright attrib            Ute in XML ");        mcleardrawable = Getresources (). getdrawable (R.drawable.delete_selector); } mcleardrawable.setbounds (0, 0, Mcleardrawable.getintrinsicwidth (), Mcleardrawable.getintrinsicheight ());        The default setting hides the icon setcleariconvisible (false);        Set Focus Change monitoring Setonfocuschangelistener (this);    Sets the monitor addtextchangedlistener (this) where the contents of the input box change; }/** * Because we cannot set the Click event directly to EditText.     So we used to remember the position we pressed to simulate the click event when we pressed 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 even click on the icon, vertical direction is not considered */@Override public boolean ontouchevent (Motionevent event) {if (event.getaction () = = Motionevent.action_u P) {if (Getcompounddrawables () [2] = null) {Boolean touchable = Event.getx () > (GetWidth ()                -Gettotalpaddingright ()) && (Event.getx () < ((GetWidth ()-getpaddingright ()));                if (touchable) {This.settext ("");    }}} return Super.ontouchevent (event); }/** * When the clearedittext focus changes, infer that the string length set clear icon display and hide */@Override public 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 for edittext draw up * * @param visible */protected void Setcleariconvisible (Boolean visible) {drawable right = visible?

Mcleardrawable:null; Setcompounddrawables (Getcompounddrawables () [0],getcompounddrawables () [1], right, Getcompounddrawables () [3]); }/** * The callback method when the contents of the input box are changed */@Override public void ontextchanged (charsequence s, int start, int count, int after) {if (Hasfoucs) {setcleariconvisible (s.length () > 0); }} @Override public void beforetextchanged (Charsequence s, int. start, int count,int after) {} @Override public void aftertextchanged (Editable s) {}/** * Set shake animation */public void setshakeanimation () {T His.setanimation (Shakeanimation (5)); }/** * Shaking animation * * @param counts * 1 seconds shaking how many next * @return */public static Animation s hakeanimation (int counts) {Animation translateanimation = new Translateanimation (0, 10, 0, 0); Set up a loop accelerator, which uses the number of passes to have the effect of swinging. Translateanimation.setinterpolator (New Cycleinterpolator (counts)); TranslateanimAtion.setduration (500); return translateanimation; }}

The following is the use of your own definition of the control XML:

<com.exmaple.clearedittext                android:id= "@+id/etusername"                android:layout_width= "Fill_parent                " android:layout_height= "30dip"                android:background= "@null"                android:drawableleft= "@drawable/user_login"                android:drawablepadding= "7dip"                android:hint= "@string/str_hit_inputname"                android:singleline= "true"                android:textsize= "17SP" >                <requestfocus/>            </com.exmaple.ClearEditText>
The following are:



Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

Android with recognition removal rate edittext (it comes with a shake-out effect)

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.