Editiew 有删除图标 和焦点判断

  1. public class ClearEditText extends EditText implements    
  2.         OnFocusChangeListener, TextWatcher {   
  3.     /** 
  4.      * 删除按钮的引用 
  5.      */  
  6.     private Drawable mClearDrawable;   
  7.     /** 
  8.      * 控件是否有焦点 
  9.      */  
  10.     private boolean hasFoucs;  
  11.    
  12.     public ClearEditText(Context context) {   
  13.         this(context, null);   
  14.     }   
  15.    
  16.     public ClearEditText(Context context, AttributeSet attrs) {   
  17.         //这里构造方法也很重要,不加这个很多属性不能再XML里面定义  
  18.         this(context, attrs, android.R.attr.editTextStyle);   
  19.     }   
  20.       
  21.     public ClearEditText(Context context, AttributeSet attrs, int defStyle) {  
  22.         super(context, attrs, defStyle);  
  23.         init();  
  24.     }  
  25.       
  26.       
  27.     private void init() {   
  28.         //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片  
  29.         mClearDrawable = getCompoundDrawables()[2];   
  30.         if (mClearDrawable == null) {   
  31. //          throw new NullPointerException("You can add drawableRight attribute in XML");  
  32.             mClearDrawable = getResources().getDrawable(R.drawable.delete_selector);   
  33.         }   
  34.           
  35.         mClearDrawable.setBounds(00, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());   
  36.         //默认设置隐藏图标  
  37.         setClearIconVisible(false);   
  38.         //设置焦点改变的监听  
  39.         setOnFocusChangeListener(this);   
  40.         //设置输入框里面内容发生改变的监听  
  41.         addTextChangedListener(this);   
  42.     }   
  43.    
  44.    
  45.     /** 
  46.      * 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件 
  47.      * 当我们按下的位置 在  EditText的宽度 - 图标到控件右边的间距 - 图标的宽度  和 
  48.      * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向就没有考虑 
  49.      */  
  50.     @Override   
  51.     public boolean onTouchEvent(MotionEvent event) {  
  52.         if (event.getAction() == MotionEvent.ACTION_UP) {  
  53.             if (getCompoundDrawables()[2] != null) {  
  54.   
  55.                 boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight())  
  56.                         && (event.getX() < ((getWidth() - getPaddingRight())));  
  57.                   
  58.                 if (touchable) {  
  59.                     this.setText("");  
  60.                 }  
  61.             }  
  62.         }  
  63.   
  64.         return super.onTouchEvent(event);  
  65.     }  
  66.    
  67.     /** 
  68.      * 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏 
  69.      */  
  70.     @Override   
  71.     public void onFocusChange(View v, boolean hasFocus) {   
  72.         this.hasFoucs = hasFocus;  
  73.         if (hasFocus) {   
  74.             setClearIconVisible(getText().length() > 0);   
  75.         } else {   
  76.             setClearIconVisible(false);   
  77.         }   
  78.     }   
  79.    
  80.    
  81.     /** 
  82.      * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去 
  83.      * @param visible 
  84.      */  
  85.     protected void setClearIconVisible(boolean visible) {   
  86.         Drawable right = visible ? mClearDrawable : null;   
  87.         setCompoundDrawables(getCompoundDrawables()[0],   
  88.                 getCompoundDrawables()[1], right, getCompoundDrawables()[3]);   
  89.     }   
  90.        
  91.       
  92.     /** 
  93.      * 当输入框里面内容发生变化的时候回调的方法 
  94.      */  
  95.     @Override   
  96.     public void onTextChanged(CharSequence s, int start, int count,   
  97.             int after) {   
  98.                 if(hasFoucs){  
  99.                     setClearIconVisible(s.length() > 0);  
  100.                 }  
  101.     }   
  102.    
  103.     @Override   
  104.     public void beforeTextChanged(CharSequence s, int start, int count,   
  105.             int after) {   
  106.            
  107.     }   
  108.    
  109.     @Override   
  110.     public void afterTextChanged(Editable s) {   
  111.            
  112.     }   
  113.       
  114.      
  115.     /** 
  116.      * 设置晃动动画 
  117.      */  
  118.     public void setShakeAnimation(){  
  119.         this.setAnimation(shakeAnimation(5));  
  120.     }  
  121.       
  122.       
  123.     /** 
  124.      * 晃动动画 
  125.      * @param counts 1秒钟晃动多少下 
  126.      * @return 
  127.      */  
  128.     public static Animation shakeAnimation(int counts){  
  129.         Animation translateAnimation = new TranslateAnimation(01000);  
  130.         translateAnimation.setInterpolator(new CycleInterpolator(counts));  
  131.         translateAnimation.setDuration(1000);  
  132.         return translateAnimation;  
  133.     }  
  134.    
  135.    
  136. }  








xml 布局  
 <com.example.test.ClearEditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="26dp"
        android:ems="10" >

        <requestFocus />
    </com.example.test.ClearEditText>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值