Android 实现的EditText响应drawableRight的点击事件

1.自定义Edittext 实现右侧图标点击清空

package com.dxw.live.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.dxw.live.R;

public class RightPicClickEditText extends AppCompatEditText {
    private Drawable drawableRight;

    public RightPicClickEditText(Context context) {
        super(context);
        drawableRight = context.getResources().getDrawable(
                R.mipmap.edit_close);
    }

    public RightPicClickEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        drawableRight = context.getResources().getDrawable(
                R.mipmap.edit_close);
    }

    public RightPicClickEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        drawableRight = context.getResources().getDrawable(
                R.mipmap.edit_close);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        if (TextUtils.isEmpty(text)) {
            setCompoundDrawablesWithIntrinsicBounds(null,
                    null, null, null);
        } else {
            setCompoundDrawablesWithIntrinsicBounds(null,
                    null, drawableRight, null);
        }
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
    }

    // 触摸事件
    // 判断DrawableLeft/DrawableRight是否被点击
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 触摸状态
        if (event.getAction() == MotionEvent.ACTION_UP) {
            // 监听DrawableLeft
//            if (onDrawableRightListener != null) {
                // 判断DrawableLeft是否被点击
                Drawable drawableRight = getCompoundDrawables()[2];
                // 当按下的位置 < 在EditText的到左边间距+图标的宽度+Padding
//                if (drawableLeft != null && event.getRawX() 在EditText的到右边间距 - 图标的宽度 - Padding
                if (drawableRight != null) {
                    setText("");
                    setCompoundDrawablesWithIntrinsicBounds(null,
                            null, null, null);
                    // 执行DrawableRight点击事件
//                    onDrawableRightListener.onDrawableRightClick();
                }
//            }
        }
        return super.onTouchEvent(event);
    }

    // 定义一个DrawableLeft点击事件接口
    public interface OnDrawableLeftListener {
        void onDrawableLeftClick();
    }

    private OnDrawableLeftListener onDrawableLeftListener;

    public void setOnDrawableLeftListener(OnDrawableLeftListener onDrawableLeftListener) {
        this.onDrawableLeftListener = onDrawableLeftListener;
    }

    // 定义一个DrawableRight点击事件接口
    public interface OnDrawableRightListener {
        void onDrawableRightClick();
    }

    private OnDrawableRightListener onDrawableRightListener;

    public void setOnDrawableRightListener(OnDrawableRightListener onDrawableRightListener) {
        this.onDrawableRightListener = onDrawableRightListener;
    }
}

猜你喜欢

转载自www.cnblogs.com/huihuizhang/p/10849157.html