有没有一个聪明的方法让用户在隐藏和查看密码之间切换在一个android EditText? 许多基于PC的应用程序都允许用户这样做。
当前回答
你可以使用app:passwordToggleEnabled="true"
下面是一个例子
<android.support.design.widget.TextInputLayout
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
android:textColorHint="@color/colorhint"
android:textColor="@color/colortext">
其他回答
我觉得我想回答这个问题,即使有一些好的答案,
根据文档TransformationMethod完成我们的任务
TransformationMethod TextView使用TransformationMethods来做一些事情,比如替换 字符的密码与点,或保持换行符 避免在单行文本字段中引起换行。
注意我使用黄油刀,但它是相同的,如果用户检查显示密码
@OnCheckedChanged(R.id.showpass)
public void onChecked(boolean checked){
if(checked){
et_password.setTransformationMethod(null);
}else {
et_password.setTransformationMethod(new PasswordTransformationMethod());
}
// cursor reset his position so we need set position to the end of text
et_password.setSelection(et_password.getText().length());
}
从支持库v24.2.0开始,这真的很容易实现。
你需要做的就是:
Add the design library to your dependencies dependencies { compile "com.android.support:design:24.2.0" } Use TextInputEditText in conjunction with TextInputLayout <android.support.design.widget.TextInputLayout android:id="@+id/etPasswordLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:passwordToggleEnabled="true" android:layout_marginBottom="@dimen/login_spacing_bottom"> <android.support.design.widget.TextInputEditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/fragment_login_password_hint" android:inputType="textPassword"/> </android.support.design.widget.TextInputLayout>
passwordToggleEnabled属性将完成这项工作!
在你的根布局中,不要忘记添加xmlns:app="http://schemas.android.com/apk/res-auto" 您可以使用以下方法自定义密码切换:
app:passwordToggleDrawable -可绘制的使用作为密码输入可见切换图标。 app:passwordToggleTint -用于密码输入可见切换的图标。 app:passwordToggleTintMode -用于应用背景色调的混合模式。
更多细节在TextInputLayout文档。
对于安卓X
将android. design.widget. textinputlayout替换为com.google.android.material.textfield.TextInputLayout 将android.support.design.widget.TextInputEditText替换为com.google.android.material.textfield.TextInputEditText
我使用了一个OnClickListener(),它与我想用作toogle的按钮相关联。
private EditText email_et, contraseña_et;
protected void onCreate(Bundle savedInstanceState) {
....
contraseña_et = (EditText) findViewById(R.id.contraseña_et);
....
vercontra_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int inputType = contraseña_et.getInputType();
if (inputType == 129){
contraseña_et.setInputType(1);
} else {
contraseña_et.setInputType(129);
}
contraseña_et.setSelection(contraseña_et.getText().length());
}
});
阅读文档,int值似乎是不同的,所以我调试找到正确的值,它的工作棒极了,这是一个更容易一点的方式。
[顺便说一下,Contraseña是西班牙语的密码]
您必须询问当前文本是否已经显示了圆点,函数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)
}
尝试https://github.com/maksim88/PasswordEditText项目在github。 你甚至不需要改变你的Java代码使用它。只改变
EditText
标签
com.maksim88.passwordedittext.PasswordEditText
在XML文件中。
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。