SoFunction
Updated on 2025-03-02

Android custom control EditText implements clearing and jittering functions

This article shares the specific code for Android EditText to implement clearing and jitter functions for your reference. The specific content is as follows

The source code is as follows:

public class ClearEditText extends EditText implements ,TextWatcher { 
 / 
  * Delete the reference to the button 
  */ 
 private Drawable mClearDrawable; 
 / 
  * Is the control focused? 
  */ 
 private boolean hasFoucs; 
 
 public ClearEditText(Context context) { 
  this(context, null); 
 } 
 public ClearEditText(Context context, AttributeSet attrs) { 
  // The construction method here is also very important. Without adding this many attributes, you cannot define it in XML.  this(context, attrs, ); 
 } 
 
 public ClearEditText(Context context, AttributeSet attrs, int defStyle) { 
  super(context, attrs, defStyle); 
  init(); 
 } 
 private void init() { 
  // Get the DrawableRight of EditText. If we do not set it, we will use the default image. 2 is to get the picture on the right. The order is the upper left and the lower right (0, 1, 2, 3,)  mClearDrawable = getCompoundDrawables()[2]; 
  if (mClearDrawable == null) { 
   // throw new 
   // NullPointerException("You can add drawableRight attribute in XML"); 
   mClearDrawable = getResources().getDrawable(.icon_clear_input); 
  } 
 
  (0, 0, (),()); 
  //Hide icons by default  setClearIconVisible(false); 
  // Set the monitoring of the focus change  setOnFocusChangeListener(this); 
  // Set the listening to the contents in the input box that have changed  addTextChangedListener(this); 
 } 
 
 / 
  * Because we can't give it directlyEditTextSetting up click events,So we simulate click event by remembering where we pressed When we press the position exist EditTextWidth of - 
  * Spacing between icons and the right side of the control - 图标Width of and EditTextWidth of - Spacing between icons and the right side of the control之间我们就算点击了图标,The vertical direction is not considered 
  */ 
 @Override 
 public boolean onTouchEvent(MotionEvent event) { 
  if (() == MotionEvent.ACTION_UP) { 
   if (getCompoundDrawables()[2] != null) { 
    boolean touchable = () > (getWidth() - getTotalPaddingRight())&& (() < ((getWidth() - getPaddingRight()))); 
    if (touchable) { 
     (""); 
    } 
   } 
  } 
  return (event); 
 } 
 
 / 
  * whenClearEditTextWhen the focus changes,Determine the display and hide of the string length in the clear icon 
  */ 
 @Override 
 public void onFocusChange(View v, boolean hasFocus) { 
   = hasFocus; 
  if (hasFocus) { 
   setClearIconVisible(getText().length() > 0); 
  } else { 
   setClearIconVisible(false); 
  } 
 } 
 
 / 
  * Set the display and hide of clear icons,CallsetCompoundDrawablesforEditTextDraw it on 
  * 
  * @param visible 
  */ 
 protected void setClearIconVisible(boolean visible) { 
  Drawable right = visible ? mClearDrawable : null; 
  setCompoundDrawables(getCompoundDrawables()[0],getCompoundDrawables()[1], right, getCompoundDrawables()[3]); 
 } 
 
 / 
  * when输入框里面内容发生变化的时候回调的方法 
  */ 
 @Override 
 public void onTextChanged(CharSequence s, int start, int count, int after) { 
  if (hasFoucs) { 
   setClearIconVisible(() > 0); 
  } 
 } 
 
 @Override 
 public void beforeTextChanged(CharSequence s, int start, int count,int after) { 
 
 } 
 
 @Override 
 public void afterTextChanged(Editable s) { 
 
 } 
 
 / 
  * Setting a shake animation 
  */ 
 public void setShakeAnimation() { 
  (shakeAnimation(5)); 
 } 
 
 / 
  * Shake animation 
  * 
  * @param counts 
  *   1How many times will it shake in seconds 
  * @return 
  */ 
 public static Animation shakeAnimation(int counts) { 
  Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0); 
  //Set a cycle accelerator, and the number of times you use it will swing.  (new CycleInterpolator(counts)); 
  (500); 
  return translateAnimation; 
 } 
 
} 

Use method is the same as that of ordinary EditText:

<  
    android:  
    android:layout_marginTop="60dp"  
    android:layout_width="fill_parent"  
    android:background="@drawable/login_edittext_bg"   
    android:drawableLeft="@drawable/icon_user"  
    android:layout_marginLeft="10dip"  
    android:layout_marginRight="10dip"  
    android:singleLine="true"  
    android:drawableRight="@drawable/delete_selector"  
    android:hint="Enter a username"  
    android:layout_height="wrap_content" /> 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.