Android 带一键删除按钮的EditText

导读:本篇文章讲解 Android 带一键删除按钮的EditText,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1.操作步骤简单,使用方便

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.EditText;

public class EditTextWithDel extends EditText {
    private final static String TAG = "EditTextWithDel";
    private Drawable imgInable;
    private Drawable imgAble;
    private Context mContext;

    public EditTextWithDel(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public EditTextWithDel(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        init();
    }

    public EditTextWithDel(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    private void init() {
        imgInable = mContext.getResources().getDrawable(R.drawable.delete_gray);
        imgAble = mContext.getResources().getDrawable(R.drawable.delete);
        //事件监听
        addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }
            @Override
            public void afterTextChanged(Editable s) {
                setDrawable();
            }
        });
        setDrawable();
    }

    //设置删除图片
    private void setDrawable() {
        if(length() < 1){
            //表示没有输入文本内容时显示的图片,个人觉得不需要设置默认图片
            setCompoundDrawablesWithIntrinsicBounds(null, null, imgInable, null);
//          setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        }else{
            setCompoundDrawablesWithIntrinsicBounds(null, null, imgAble, null);
        }
    }

     // 处理删除事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (imgAble != null && event.getAction() == MotionEvent.ACTION_UP) {
            int eventX = (int) event.getRawX();
            int eventY = (int) event.getRawY();
            Log.e(TAG, "eventX = " + eventX + "; eventY = " + eventY);
            Rect rect = new Rect();
            getGlobalVisibleRect(rect);
            rect.left = rect.right - 70;
            if(rect.contains(eventX, eventY)) 
                setText("");
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }



        /**
         * 设置晃动动画
         */
         public void setShakeAnimation(){
             this.startAnimation( 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;
         }




}

2.效果图:
这里写图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/12877.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!