Custom EditText resolution for Android with erase and shake effects

Source: Internet
Author: User

Reproduced please indicate the source: http://blog.csdn.net/qq_19986309 respect for the work of others ~ Thank you


Today for everyone to achieve a custom EditText with clear button and shake effect often everyone write login registration button may be needed, well nonsense not much to say directly on





This effect is good is also more practical no matter where the sign-in is the need to paste the code below


public class Clearwriteedittext extends EditText implements View.onfocuschangelistener, Textwatcher {/** * Delete button's citation    Use */private drawable mcleardrawable;    Public Clearwriteedittext (Context context) {this (context,null); } public Clearwriteedittext (context context, AttributeSet attrs) {//Here The constructor method is also important, without this many attributes can no longer define this in XML (c Ontext, Attrs, Android.    R.attr.edittextstyle); } public Clearwriteedittext (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, defst        YLEATTR);    Init ();        } private void Init () {mcleardrawable = Getresources (). getdrawable (R.mipmap.search_clear_pressed_write);        Mcleardrawable.setbounds (0, 0, mcleardrawable.getintrinsicwidth (), Mcleardrawable.getintrinsicheight ());        Setcleariconvisible (FALSE);        This.setonfocuschangelistener (this);    This.addtextchangedlistener (this); }/** * The method of callback when the contents of the input box are changed */@Override public void ONTEXTCHanged (charsequence s, int start, int count, int after) {setcleariconvisible (s.length () > 0); }/** * Set clear icon display and hide, call Setcompounddrawables to EditText draw up * @param visible */protected void Setclearicon        Visible (Boolean visible) {drawable right = visible mcleardrawable:null; Setcompounddrawables (Getcompounddrawables () [0], Getcompounddrawables () [1], right, Getcompounddrawables () [3]    ); }/** * Because we can't set the click event directly to EditText, so we use to remember the position we pressed to simulate the click event * when we press the position in the width of the EditText-icon to the right side of the control-the width of the icon and the * E        Dittext Width-icon to the right of the control between the spacing we click on the icon, the vertical direction does not consider */@Override public boolean ontouchevent (Motionevent event) { if (Getcompounddrawables () [2]! = NULL) {if (event.getaction () = = motionevent.action_up) {bool Ean touchable = Event.getx () > (GetWidth ()-Getpaddingright ()-Mcleardrawable.getintrinsicwidt H ()) && (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 */@Override public void Onfocuschange (View V, BOOL        Ean Hasfocus) {if (Hasfocus) {setcleariconvisible (GetText (). Length () > 0);        } else {setcleariconvisible (false);    }} @Override public void beforetextchanged (Charsequence s, int. start, int count, int after) {} @Override public void aftertextchanged (Editable s) {}/** * Set shake animation */public void setshakeanimation () {This.seta    Nimation (Shakeanimation (3));}        /** * Shaking animation * @param counts half a second shaking how many next * @return */public static Animation shakeanimation (int counts) {        Animation translateanimation = new Translateanimation (0, 10, 0, 0); Translateanimation.setinterpolator (New CycleinterpolatoR (counts));        Translateanimation.setduration (500);    return translateanimation;    } public drawable getcleardrawable () {return mcleardrawable;    } public void Setcleardrawable (drawable mcleardrawable) {this.mcleardrawable = mcleardrawable; }}

We see a custom EditText with a shake sweep effect, of course, a picture of the integrated EditText Delete button set the resource yourself inside the Init methodgetresources (). Getdrawable want to define what kind of formula they can

And then the others are relatively simple there are comments worth thinking is to clear the words click the button directly to the SetText (""); You can give an empty string, but I didn't try it, and then the usage was simple, no different from the normal EditText.

Attach a photo




in the end, we see the following code for shaking effect :


@Override public    void OnClick (View v) {        switch (V.getid ()) {            r.id.btn_regist:                USERACC =  Musername.gettext (). toString (). Trim ();                PWD = Mpassword.gettext (). toString (). Trim ();                Confirmpass = Mpassword_two.gettext (). toString (). Trim ();                if (Textutils.isempty (USERACC)) {                    Message msg = Message.obtain ();                    Msg.what = Usernameisnull;                    Mhandler.sendmessage (msg);                    return;                }

Here is the message to send if the user name is empty goodbye to the following code:


    Private final int usernameisnull = 0;    Private Handler Mhandler = new Handler () {        @Override public        void Handlemessage (Message msg) {            Super.handlemessage (msg);            Switch (msg.what) {case                usernameisnull:                musername.setshakeanimation ();                Ntoast.shorttoast (Mcontext, r.string.username_null_text);                Break;}}    ;

Here is the processing message we saw a Toast. Prompt user name cannot be empty and perform shaking effect animation but what has been bothering me is that whether it's a thread or a handler message or a direct animation in the UI, it's not going to work. But come to a conclusion to share with you. Every time you judge EditText, such as an empty toast, but the animation must be your finger to click EditText or the text has to be added or deleted to trigger this animation effect will not be executed here if a friend get this blog solved this problem also please leave a message Comments or e-mails to tell the author, greatly appreciated!


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

Custom EditText resolution for Android with erase and shake effects

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.