EditText,TextView drawableRight图标的点击事件

具体操作不多讲,附上代码,就清楚!!!

time_choose_tv.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // getCompoundDrawables获取是一个数组,数组0,1,2,3,对应着左,上,右,下 这4个位置的图片,如果没有就为null
        Drawable drawable = time_choose_tv.getCompoundDrawables()[2];
        //如果右边没有图片,不再处理
        if (drawable == null){
            return false;
        }
        //如果不是按下事件,不再处理
        if (event.getAction() != MotionEvent.ACTION_DOWN) {
            return false;
        }
        if (event.getX() > time_choose_tv.getWidth()
                - time_choose_tv.getPaddingRight()
                - drawable.getIntrinsicWidth()){
            //具体操作
        }
        return false;
    }
});

猜你喜欢

转载自blog.csdn.net/fmc088/article/details/80491369