安卓自定义带删除图标的输入框EditView

在安卓中我们使用默认的Editview是只能输入文字的,但是想要删除,我们得利用输入法的删除按钮来一个个删除,现在在好多应用当中,会在输入框的最后出现一个删除图片,点击就清空了所有的数据,这个很方便。下面我们来实现一下。

先看下效果图:



我们这里实现的是,当输入框有文本是,才会出现这个删除图标。当输入为空是,就会消失,其实就是自定义一个Ediiview:

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

我们在使用的时候,只要替换Editview,换成我们自定义的就行了:

[html]  view plain copy print ?
  1. <com.test.view.ClearEditText  
  2.                     android:id="@+id/edit_user"  
  3.                     android:layout_width="1px"  
  4.                     android:layout_height="fill_parent"  
  5.                     android:layout_marginLeft="10.0dip"  
  6.                     android:layout_marginRight="15.0dip"  
  7.                     android:layout_weight="3"  
  8.                     android:background="@android:color/white"  
  9.                     android:hint="账号/邮箱/手机号码"  
  10.                     android:inputType="textPersonName"  
  11.                     android:maxLength="30"  
  12.                     android:paddingLeft="10.0dip"  
  13.                     android:singleLine="true"  
  14.                     android:textSize="16.0sp" />  


这样就会有我们想要的效果了。完整demo我就不提供了。






版权声明:本文为博主原创文章,未经博主允许不得转载。from:http://blog.youkuaiyun.com/lengguoxing/article/details/42145467

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值