有没有一个聪明的方法让用户在隐藏和查看密码之间切换在一个android EditText? 许多基于PC的应用程序都允许用户这样做。
当前回答
只需在Xml文件中创建一个复选框
然后简单的用Java代码编写这个函数
checkbox = findViewById(R.id.checkbox);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if (isChecked) {
password.setTransformationMethod(null);
}
else{
password.setTransformationMethod(new PasswordTransformationMethod());
}
}
});
其他回答
if (inputPassword.getTransformationMethod() == PasswordTransformationMethod.getInstance()) {
//password is visible
inputPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
else if(inputPassword.getTransformationMethod() == HideReturnsTransformationMethod.getInstance()) {
//password is hidden
inputPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
您必须询问当前文本是否已经显示了圆点,函数PasswordTransformationMethod.getInstance()允许您这样做。
这是我在kotlin中的函数:
fun hideAndShowPassword(editText: EditText, indicator: ImageView) {
if (editText.transformationMethod == PasswordTransformationMethod.getInstance()) {
editText.transformationMethod = HideReturnsTransformationMethod.getInstance()
indicator.setImageDrawable(
ContextCompat.getDrawable(
editText.context,
R.drawable.eye
)
)
indicator.imageTintList =
ContextCompat.getColorStateList(editText.context, R.color.colorTintIcons)
} else {
editText.transformationMethod = PasswordTransformationMethod.getInstance()
indicator.setImageDrawable(
ContextCompat.getDrawable(
editText.context,
R.drawable.eye_off
)
)
indicator.imageTintList =
ContextCompat.getColorStateList(editText.context, R.color.colorTintIcons)
}
editText.setSelection(editText.text.length)
}
一个很好的解决方案。设置一个按钮,然后使用下面的代码:
public void showPassword(View v)
{
TextView showHideBtnText = (TextView) findViewById(R.id.textView1);
if(showHideBtnText.getText().toString().equals("Show Password")){
password.setTransformationMethod(null);
showHideBtnText.setText("Hide");
} else{
password.setTransformationMethod(new PasswordTransformationMethod());
showHideBtnText.setText("Show Password");
}
}
只需在Xml文件中创建一个复选框
然后简单的用Java代码编写这个函数
checkbox = findViewById(R.id.checkbox);
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if (isChecked) {
password.setTransformationMethod(null);
}
else{
password.setTransformationMethod(new PasswordTransformationMethod());
}
}
});
你试过setTransformationMethod吗?它继承自TextView,并希望TransformationMethod作为参数。
你可以在这里找到更多关于TransformationMethods的信息。
它还有一些很酷的功能,比如字符替换。
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。