EditText的drawableright 的点击明文和密文

pwd.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        // et.getCompoundDrawables()得到一个长度为4的数组,分别表示左右上下四张图片
                        Drawable[] drawables = pwd.getCompoundDrawables();
                        //如果右边没有图片,不再处理
                        if (drawables[2] == null){
                            return false;
                        }
                       // boolean touchable = event.getX() > (getWidth() - getTotalPaddingRight()) && (event.getX() < ((getWidth() - getPaddingRight())));
                        //如果不是按下事件,不再处理
                        if (event.getAction() != MotionEvent.ACTION_UP){
                            return false;
                        }
                        //获取右侧图片的大小位置
                        if (event.getX() > pwd.getWidth() - pwd.getPaddingRight() - drawables[2].getIntrinsicWidth()){
                            if (b){
                                //设置为明文显示
                                pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                                b=false;
                            }else{
                                //设置为密文显示
                                pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                                b=true;
                            }
                            //设置光标位置
                            pwd.setSelection(pwd.getText().length());
                        }
                        return false;
                    }
                });

猜你喜欢

转载自blog.csdn.net/wangshuo_/article/details/88421335