EditText里的清除按钮


public class ClearAllText extends AppCompatEditText implements View.OnFocusChangeListener, TextWatcher {

        private Drawable mClearDrawable;
        private Bitmap bitmap;

        public ClearAllText(Context context) {
                this(context, null);
        }
        public ClearAllText(Context context, AttributeSet attrs) {
                this(context, attrs, android.R.attr.editTextStyle);
        }
        public ClearAllText(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                init(context);
        }
        private void init(Context context) {
                //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
                mClearDrawable = getCompoundDrawables()[2];
                if (mClearDrawable == null) {
                        mClearDrawable = new BitmapDrawable(null,createBitmap(R.drawable.delete,context));
         }
                mClearDrawable.setBounds(0, 0, mClearDrawable.getIntrinsicWidth(), mClearDrawable.getIntrinsicHeight());
                setClearIconVisible(false);
                setOnFocusChangeListener(this);
                addTextChangedListener(this);
        }


        /**
         * 因为我们不能直接给EditText设置点击事件,所以我们用记住我们按下的位置来模拟点击事件
         * 当我们按下的位置 在  EditText的宽度 - 图标到控件右边的间距 - 图标的宽度  和
         * EditText的宽度 - 图标到控件右边的间距之间我们就算点击了图标,竖直方向没有考虑
         */
        @Override
        public boolean onTouchEvent(MotionEvent event) {
                if (getCompoundDrawables()[2] != null) {
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                                boolean touchable = event.getX() > (getWidth()
                                        - getPaddingRight() - mClearDrawable.getIntrinsicWidth())
                                        && (event.getX() < ((getWidth() - getPaddingRight())));
                                if (touchable) {
                                        this.setText("");
                                }
                        }
                }
                return super.onTouchEvent(event);
        }

        /**
         * 当ClearEditText焦点发生变化的时候,判断里面字符串长度设置清除图标的显示与隐藏
         */
        public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                        setClearIconVisible(getText().length() > 0);
                } else {
                        setClearIconVisible(false);
                }
        }


        /**
         * 设置清除图标的显示与隐藏,调用setCompoundDrawables为EditText绘制上去
         * @param visible
         */
        protected void setClearIconVisible(boolean visible) {
                Drawable right = visible ? mClearDrawable : null;
                setCompoundDrawables(getCompoundDrawables()[0],
                getCompoundDrawables()[1], right, getCompoundDrawables()[3]);
        }


        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        /**
         * 当输入框里面内容发生变化的时候回调的方法
         */
        public void onTextChanged(CharSequence s, int start, int count,
                int after) {
                setClearIconVisible(s.length() > 0);
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }

        /**
         * 设置晃动动画
         */
        public void setShakeAnimation(){
                this.setAnimation(shakeAnimation(5));
        }

        /**
         * 晃动动画
         * @param counts 1秒钟晃动多少下
         * @return
         */
        public static Animation shakeAnimation(int counts){
                Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
                translateAnimation.setInterpolator(new CycleInterpolator(counts));
                translateAnimation.setDuration(1000);
                return translateAnimation;
        }

        /**
         * 给图标染上当前提示文本的颜色并且转出Bitmap
         * @param resources
         * @param context
         * @return
         */
        public Bitmap createBitmap(int resources, Context context) {
                final Drawable drawable = ContextCompat.getDrawable(context, resources);
                final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
                DrawableCompat.setTint(wrappedDrawable, getCurrentHintTextColor());
                return drawableToBitamp(wrappedDrawable);
        }
        /**
         * drawable 转换成 bitmap
         * @param drawable
         * @return
         */
        private Bitmap drawableToBitamp(Drawable drawable) {
                int w = drawable.getIntrinsicWidth();
                int h = drawable.getIntrinsicHeight();
                Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
                Bitmap bitmap = Bitmap.createBitmap(w, h, config);
                Canvas canvas = new Canvas(bitmap);
                drawable.setBounds(0, 0, w, h);
                drawable.draw(canvas);
                return bitmap;
        }
}

然后在XML文件里直接引用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android 中,如果需要在用户输入内容时显示清除按钮,并且在用户没有输入内容时隐藏清除按钮,可以通过添加 TextWatcher 监听器实现。 以下是一个示例代码,可以根据用户输入的内容动态显示或隐藏清除按钮: ```java EditText editText = findViewById(R.id.edit_text); Button clearButton = findViewById(R.id.clear_button); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // 在文本变化前执行的操作 } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // 在文本变化中执行的操作 } @Override public void afterTextChanged(Editable s) { // 在文本变化后执行的操作,包括清空操作 if (s.length() > 0) { clearButton.setVisibility(View.VISIBLE); } else { clearButton.setVisibility(View.GONE); } } }); clearButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editText.setText(""); } }); ``` 在这个示例代码中,我们首先在 EditText 上添加了一个 TextWatcher 监听器,然后在 `afterTextChanged()` 方法中判断当前输入框中是否有内容,如果有,则显示清除按钮;否则隐藏清除按钮。在清除按钮的点击事件中,我们调用 `editText.setText("")` 方法清空输入框中的内容。这样,当用户输入内容时,清除按钮就会自动显示出来,而当用户清空内容时,清除按钮会自动隐藏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值