gpt4 book ai didi

java - 在 Android 中使用可绘制图标显示和隐藏密码

转载 作者:太空宇宙 更新时间:2023-11-04 12:19:33 25 4
gpt4 key购买 nike

我使用EditText设计了一个带有密码字段的屏幕,在EditText中我使用drawableRightIcon,它必须在我们单击可绘制按钮时显示可见的密码,并用另一个图标替换该可绘制图标?有人可以帮忙吗?

最佳答案

以下是我目前在我的应用程序中用于此目的的代码。我们基本上在 EditText 上放置了一个触摸监听器,并确定点击是否发生在可绘制对象上并采取相应的操作(也切换图标):

getPasswordEditText().setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2; // index

if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (getPasswordEditText().getRight() - getPasswordEditText().getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
if (passwordShown) {
passwordShown = false;
// 129 is obtained by bitwise ORing InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
getPasswordEditText().setInputType(129);

// Need to call following as the font is changed to mono-space by default for password fields
getPasswordEditText().setTypeface(Typeface.SANS_SERIF);
getPasswordEditText().setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.locked_icon, 0);
} else {
passwordShown = true;
getPasswordEditText().setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

getPasswordEditText().setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.unlocked_icon, 0);
}

return true;
}
}
return false;
}
});

关于java - 在 Android 中使用可绘制图标显示和隐藏密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38996700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com