我在edittext中添加了可绘制的左右图标。 Drawable正确用于显示或隐藏密码但是当我点击drawable右图标时,drawable left变得不可见。如何解决这个问题?
txtPassword.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (txtPassword.getRight() - txtPassword.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
if (IsHidePwd) {
txtPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_show_pwd, 0);
txtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
IsHidePwd = false;
return true;
} else {
txtPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_hide_pwd, 0);
txtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
IsHidePwd = true;
return true;
}
}
}
return false;
}
});
答案 0 :(得分:1)
在这一行中,我认为你还需要为左侧设置drawable。
txtPassword.setCompoundDrawablesWithIntrinsicBounds(R.drawable.left_img,0,R.drawable.ic_show_pwd,0);