Android custom View-EditText with the delete button

Source: Internet
Author: User


MainActivity is as follows:

Package cc. textview5; import android. OS. bundle; import android. text. textUtils; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast; import android. app. activity;/*** Demo Description: * Custom Controls implement EditText with cleanup function ** learning materials: * http://blog.csdn.net/xiaanming/article/details/11066685 ** Thank you very much */public class MainActivity extends Activity {private CleanableEditText success; private Button mLoginButton; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mUserNameCleanableEditText = (CleanableEditText) findViewById (R. id. userNameEditText); mPassWordCleanableEditText = (CleanableEditText) findViewById (R. id. passwordEditText); mLoginButton = (Button) findViewById (R. id. loginButton); mLoginButton. setOnClickListener (new OnClickListenerImpl ();} private class OnClickListenerImpl implements OnClickListener {@ Overridepublic void onClick (View view) {if (TextUtils. isEmpty (mUserNameCleanableEditText. getText () {mUserNameCleanableEditText. setShakeAnimation (); Toast. makeText (MainActivity. this, "Enter the user name", Toast. LENGTH_SHORT ). show ();} if (TextUtils. isEmpty (mPassWordCleanableEditText. getText () {mPassWordCleanableEditText. setShakeAnimation (); Toast. makeText (MainActivity. this, "enter the password", Toast. LENGTH_SHORT ). show ();}}}}

CleanableEditText is as follows:

Package cc. textview5; import android. content. context; 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. view. animation. animation; import android. view. animation. cycleInterpolator; import android. view. animation. translateAnimation; import android. widget. editText;/*** when the focus and input content change, you must determine whether to display the clean icon on the right */public class CleanableEditText extends EditText {private Drawable mRightDrawable; private boolean isHasFocus; public CleanableEditText (Context context) {super (context); init ();} public CleanableEditText (Context context, AttributeSet attrs) {super (context, attrs); init ();} public CleanableEditText (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); init () ;}private void init () {// getCompoundDrawables: // Returns drawables for the left, top, right, and bottom borders. drawable [] drawables = this. getCompoundDrawables (); // obtain the right Drawable // android: drawableRight mRightDrawable = drawables [2] in the layout file; // set the focus change listener this. setOnFocusChangeListener (new FocusChangeListenerImpl (); // set the listening for EditText text changes this. addTextChangedListener (new TextWatcherImpl (); // make the clean icon on the right invisible at initialization setClearDrawableVisible (false );} /*** when the finger is lifted in the area of the clean icon * we will take this as a clear operation * getWidth (): Get the width of the control * event. getX (): coordinates when lifting (the changed coordinates are relative to the control itself) * getTotalPaddingRight (): distance from the left edge of the clean icon to the right edge of the control * getPaddingRight (): the distance from the right edge of the clean icon to the right edge of the control *. * getWidth ()-getTotalPaddingRight () indicates: * The area of the left edge of the clean icon * getWidth () -getPaddingRight () indicates: * The area from the left to the right edge of the clean icon * so the area between the two is just the area of the clean icon */@ Overridepublic boolean onTouchEvent (MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_UP: boolean isClean = (event. getX ()> (getWidth ()-getTotalPaddingRight () & (event. getX () <(getWidth ()-getPaddingRight (); if (isClean) {setText ("") ;}break; default: break;} return super. onTouchEvent (event);} private class implements OnFocusChangeListener {@ Overridepublic void onFocusChange (View v, boolean hasFocus) {isHasFocus = hasFocus; if (isHasFocus) {boolean isVisible = getText (). toString (). length ()> = 1; setClearDrawableVisible (isVisible);} else {setClearDrawableVisible (false );}}} // After the input is complete, check whether the clean private class TextWatcherImpl implements TextWatcher {@ Overridepublic void afterTextChanged (Editable s) {boolean isVisible = getText () is displayed (). toString (). length ()> = 1; setClearDrawableVisible (isVisible) ;}@ Overridepublic void beforeTextChanged (CharSequence s, int start, int count, int after) {}@ Overridepublic void onTextChanged (CharSequence s, int start, int before, int count) {}} // hide or show the clean icon protected void setClearDrawableVisible (boolean isVisible) {Drawable rightDrawable; if (isVisible) {rightDrawable = mRightDrawable;} else {rightDrawable = null;} // use the code to set the setCompoundDrawables (getCompoundDrawables () [0], getCompoundDrawables () [1], rightDrawable, getCompoundDrawables () [3]);} // displays an animation to prompt the user to enter public void setShakeAnimation () {this. setAnimation (shakeAnimation (5);} // number of times the CycleTimes Animation is repeated public Animation shakeAnimation (int CycleTimes) {Animation translateAnimation = new TranslateAnimation (0, 10, 0, 10 ); translateAnimation. setInterpolator (new CycleInterpolator (CycleTimes); translateAnimation. setDuration (1000); return translateAnimation ;}}

Main. xml is as follows:

     
           
            
  
 


Zookeeper

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.