【android】给EditText的drawable添加点击事件

        etPhoneNum.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // et.getCompoundDrawables()得到一个长度为4的数组,分别表示左右上下四张图片
                Drawable drawable = etPhoneNum.getCompoundDrawables()[2];
                //如果右边没有图片,不再处理
                if (drawable == null)
                    return false;
                //如果不是按下事件,不再处理
                if (event.getAction() != MotionEvent.ACTION_UP)
                    return false;
                if (event.getX() > etPhoneNum.getWidth()
                        - etPhoneNum.getPaddingRight()
                        - drawable.getIntrinsicWidth()){
                    etPhoneNum.setText("");
                    etPhoneNum.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);
                }
                return false;
            }
        });
发布了73 篇原创文章 · 获赞 30 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/yonghuming_jesse/article/details/97389210