且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android实现仿IOS带清空功能的文本输入框

更新时间:2022-01-14 23:17:11

 


  1. /** 
  2.  * @类名:ClearableEditText 
  3.  * @功能描述: 
  4.  * @作者: William Xu 
  5.  * @创建日期:2013-4-13 
  6.  * @修改人: 
  7.  * @修改日期: 
  8.  * @修改备注: 
  9.  * @版本号:1.0 
  10.  */ 
  11. public class ClearableEditText extends EditText implements  
  12.         OnFocusChangeListener, TextWatcher { 
  13.  
  14.     private Drawable xD; 
  15.  
  16.     public ClearableEditText(Context context) { 
  17.         super(context); 
  18.         init(); 
  19.     } 
  20.  
  21.     public ClearableEditText(Context context, AttributeSet attrs) { 
  22.         super(context, attrs); 
  23.         init(); 
  24.     } 
  25.  
  26.     public ClearableEditText(Context context, AttributeSet attrs, int defStyle) { 
  27.         super(context, attrs, defStyle); 
  28.         init(); 
  29.     } 
  30.  
  31.     private void init() { 
  32.         xD = getCompoundDrawables()[2]; 
  33.         if (xD == null) { 
  34.             xD = getResources() 
  35.                     .getDrawable(R.drawable.search_clear); 
  36.         } 
  37.         xD.setBounds(00, xD.getIntrinsicWidth(), xD.getIntrinsicHeight()); 
  38.         setClearIconVisible(false); 
  39.         super.setOnFocusChangeListener(this); 
  40.         addTextChangedListener(this); 
  41.     } 
  42.  
  43.  
  44.     @Override 
  45.     public void setOnFocusChangeListener(OnFocusChangeListener f) { 
  46.         this.f = f; 
  47.     } 
  48.  
  49.     private OnFocusChangeListener f; 
  50.  
  51.     @Override 
  52.     public boolean onTouchEvent(MotionEvent event) { 
  53.         if (getCompoundDrawables()[2] != null) { 
  54.             if (event.getAction() == MotionEvent.ACTION_UP) { 
  55.                 boolean tappedX = event.getX() > (getWidth() 
  56.                         - getPaddingRight() - xD.getIntrinsicWidth()); 
  57.                 if (tappedX) { 
  58.                     setText(""); 
  59.                      
  60.                     event.setAction(MotionEvent.ACTION_CANCEL); 
  61.  
  62.                 } 
  63.             } 
  64.         } 
  65.  
  66.         return super.onTouchEvent(event); 
  67.     } 
  68.  
  69.     @Override 
  70.     public void onFocusChange(View v, boolean hasFocus) { 
  71.         if (hasFocus) { 
  72.             setClearIconVisible(getText().length() > 0); 
  73.         } else { 
  74.             setClearIconVisible(false); 
  75.         } 
  76.         if (f != null) { 
  77.             f.onFocusChange(v, hasFocus); 
  78.         } 
  79.     } 
  80.  
  81.  
  82.  
  83.     protected void setClearIconVisible(boolean visible) { 
  84.         Drawable x = visible ? xD : null
  85.         setCompoundDrawables(getCompoundDrawables()[0], 
  86.                 getCompoundDrawables()[1], x, getCompoundDrawables()[3]); 
  87.     } 
  88.      
  89.     @Override 
  90.     public void onTextChanged(CharSequence s, int start, int count, 
  91.             int after) { 
  92.         setClearIconVisible(s.length() > 0); 
  93.     } 
  94.  
  95.     @Override 
  96.     public void beforeTextChanged(CharSequence s, int start, int count, 
  97.             int after) { 
  98.         // TODO Auto-generated method stub 
  99.          
  100.     } 
  101.  
  102.     @Override 
  103.     public void afterTextChanged(Editable s) { 
  104.         // TODO Auto-generated method stub 
  105.          
  106.     } 
  107.  
  108.  

本文转自william_xu 51CTO博客,原文链接:http://blog.51cto.com/williamx/1195051,如需转载请自行联系原作者