我在编辑文本的两端都增加了可绘制的内容。当我点击右侧可绘制时,左边的一个消失了。基本上,编辑文本是用于密码的input.Icon锁和图标可见性(眼睛)在编辑文本的左边和右边,respectivily.The右图标切换为密码的可见性。
正确可绘制的侦听器实现:
etPassword.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (event.getRawX() >= (etPassword.getRight() - etPassword.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
if (etPassword.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
etPassword.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
etPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility_off_white_24dp, 0);
etPassword.setSelection(etPassword.getText().length());
} else {
etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
etPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility_white_24dp, 0);
}
return true;
}
}
return false;
}
});xml
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@drawable/et_bg"
android:drawableRight="@drawable/ic_visibility_off_white_24dp"
android:hint="@string/password"
android:drawableLeft="@drawable/ic_lock_white_24dp"
android:inputType="textPassword"
android:maxLines="1"
android:padding="10dp"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/textColorPrimary" />屏幕截图:

问题是点击可见性图标,左边的可绘制消失,这是屏幕截图

发布于 2016-08-08 10:00:21
您正在为EditText etPassword.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_visibility_off_white_24dp, 0);的start\左图标位置添加0
将您的lock_icon添加到EditText的start_left位置:
etPassword.setCompoundDrawablesWithIntrinsicBounds(lock_icon, 0, R.drawable.ic_visibility_off_white_24dp, 0);最终代码:
etPassword.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (event.getRawX() >= (etPassword.getRight() - etPassword.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
if (etPassword.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
etPassword.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
etPassword.setCompoundDrawablesWithIntrinsicBounds(lock_icon, 0, R.drawable.ic_visibility_off_white_24dp, 0);
etPassword.setSelection(etPassword.getText().length());
} else {
etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
etPassword.setCompoundDrawablesWithIntrinsicBounds(lock_icon, 0, R.drawable.ic_visibility_white_24dp, 0);
}
return true;
}
}
return false;
}
});https://stackoverflow.com/questions/38824042
复制相似问题