EditText设置DrawableRight,DrawableLeft,DrawableTop... 点击事件

写这个的原因是想实现下拉框+popupwindow实现spinner的效果。唉唉,比较蛋疼的是Edittext的DrawableRight。。。没有点击事件,当然可以做布局嵌套,但是没必要的布局嵌套还是没必要的,影响性能。所以就写个EditTextDrawableClick,代码比较简单,欢迎拍砖。。。

这里写图片描述

1、实现思路,主要是自定义一个控件继承Edittext,然后复写其中的onTouchEvent来,判断手指抬起的时候的x,y坐标是否点击在drawable对象上。比较方便啊这其中需要搞清楚这几个参数,

 /**
     * event.getRawX(),相对于左边界的绝对坐标,以左上角为(0,0)
     * event.getX(),相对于自身的坐标,以该空间的左上角为(0,0)
     * getLeft(),相当于margin,控件左边界相对于父控件的距离
     * getPaddingLeft,相当于padding,控件中元素相对于控件的间距
     * getBounds().width(),获取元素绘制区域的宽度
     * drawableRight.getIntrinsicWidth(),获取drawable的实际宽度
     */

2、实现封装EditTextDrawableClick

package com.coofond.carservice.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

/**
 * @description: 实现drawableleft, ..right的点击事件
 * @Author zsj on 2016/11/30 10:52.
 */

public class EditTextDrawableClick extends EditText {


    private DrawableLeftListener mLeftListener;
    private DrawableRightListener mRightListener;
    private DrawableTopListener mTopListener;
    private DrawableBottomListener mBottomListener;

    private final int DRAWABLE_LEFT = 0;
    private final int DRAWABLE_TOP = 1;
    private final int DRAWABLE_RIGHT = 2;
    private final int DRAWABLE_BOTTOM = 3;

    public EditTextDrawableClick(Context context) {
        super(context);
    }

    public EditTextDrawableClick(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextDrawableClick(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * event.getRawX(),相对于左边界的绝对坐标,以左上角为(0,0)
     * event.getX(),相对于自身的坐标,以该空间的左上角为(0,0)
     * getLeft(),相当于margin,控件左边界相对于父控件的距离
     * getPaddingLeft,相当于padding,控件中元素相对于控件的间距
     * getBounds().width(),获取元素绘制区域的宽度
     * drawableRight.getIntrinsicWidth(),获取drawable的实际宽度
     */

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //监听手指抬起时候的动作
        if (event.getAction() == MotionEvent.ACTION_UP) {
            //首先判断是否被实例化了
            if (mRightListener != null) {
                //getCompoundDrawables() 可以获取一个长度为4的drawable数组,存放drawableLeft,Right,Top,Bottom四个图片资源对象
                Drawable drawableRight = getCompoundDrawables()[DRAWABLE_RIGHT];
                //首先确保得到的drawable对象不为空,然后得到本次点击事件的x轴坐标,如果>当前控件宽度-控件右间距-drawable实际展示大小,
                // 那么说明就是点击在这个drawable上面了。其实就是算该drawable最左边的x坐标,drawableRight.getIntrinsicWidth()==drawableRight.getBounds().width()等于图标的宽度
                if (drawableRight != null && event.getX() > getWidth() - getPaddingRight() - drawableRight.getIntrinsicWidth()) {
//                if (drawableRight != null && event.getRawX() >= (getRight() - drawableRight.getBounds().width())) {
                    Log.d("DEBUG", "getX=" + event.getX() + "getY=" + event.getY() + "\n" + "getRawX=" + event.getRawX()
                            + "getRawY=" + event.getY() + "\n" + "test=" + getLeft() + "\n" + "test:hehe" + getPaddingLeft());
                    mRightListener.onDrawableRightClick(this);
                }
            }
            if (mLeftListener != null) {
                Drawable drawableLeft = getCompoundDrawables()[DRAWABLE_LEFT];
                //getX是相对于控件本身的左上角的x坐标,<=控件左边距+图片对象实际的宽度.这边的getLeft相当于margin,getPaddingLeft相当于padding
                if (drawableLeft != null && event.getX() <= getLeft() + drawableLeft.getIntrinsicWidth()) {
//                    if (drawableLeft != null && event.getRawX() <= (getLeft() + drawableLeft.getBounds().width())) {
                    mLeftListener.onDrawableLeftClick(this);
                }
            }
            //自己去理解,比较容易的
            if (mTopListener != null) {
                Drawable drawableTop = getCompoundDrawables()[DRAWABLE_TOP];
                if (drawableTop != null && event.getY() <= getTop() + drawableTop.getIntrinsicHeight()) {
                    mTopListener.onDrawableTopClick(this);
                }
            }
            //自己去理解,比较容易的
            if (mBottomListener != null) {
                Drawable drawableBottom = getCompoundDrawables()[DRAWABLE_BOTTOM];
                if (drawableBottom != null && event.getX() > getHeight() - drawableBottom.getIntrinsicWidth()) {
                    mBottomListener.onDrawableBottomClick(this);
                }
            }

        }
        return super.onTouchEvent(event);
    }

    //相应的控件注册的事件
    public void setDrawableLeftListener(DrawableLeftListener mListener) {
        this.mLeftListener = mListener;
    }

    //相应的控件注册的事件
    public void setDrawableRightListener(DrawableRightListener mListener) {
        this.mRightListener = mListener;
    }

    //相应的控件注册的事件
    public void setDrawableTopListener(DrawableTopListener mListener) {
        this.mTopListener = mListener;
    } //相应的控件注册的事件

    public void setDrawableBottomListener(DrawableBottomListener mListener) {
        this.mBottomListener = mListener;
    }

    /**
     * 回调接口需要,需要复写
     */
    public interface DrawableRightListener {
        void onDrawableRightClick(View view);
    }

    /**
     * 回调接口
     */
    public interface DrawableLeftListener {
        void onDrawableLeftClick(View view);
    }

    /**
     * 回调接口
     */
    public interface DrawableTopListener {
        void onDrawableTopClick(View view);
    }

    /**
     * 回调接口
     */
    public interface DrawableBottomListener {
        void onDrawableBottomClick(View view);
    }
}

3、调用,直接实现接口中的方法

//实例化
EditTextDrawableClick edtCarBrand=(EditTextDrawableClick) findViewById(R.id.edt_carbrand);
 edtCarBrand.setDrawableRightListener(new EditTextDrawableClick.DrawableRightListener() {
            @Override
            public void onDrawableRightClick(View view) {
                ToastUtil.toastCenter2(CarBaseinfoAct.this,"被点击");
            }
        });

4、总结,具体代码很清楚,就是判断本次点击事件的区域是否在drawable的区域,然后实现接口方法,做相应操作。。。注意,我这边犯了一个比较低级的错误,就是构造方法参数如果不需要调用的话,不需要改变super方法,只是复写这个事件而已。代码直接复制粘贴就可以了,就不上demo了

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值