有没有一个聪明的方法让用户在隐藏和查看密码之间切换在一个android EditText? 许多基于PC的应用程序都允许用户这样做。
当前回答
如果你想要一个简单的解决方案,你可以使用这个扩展的Android的EditText去这个链接了解更多信息:https://github.com/scottyab/showhidepasswordedittext
将此添加到build.gradle中: 实现“com.github.scottyab: showhidepasswordedittext: 0.8”
然后更改XML文件中的EditText。
来自:< EditText
: < com.scottyab.showhidepasswordedittext.ShowHidePasswordEditText
这是所有。
注意:在XML设计中无法看到它,请尝试在模拟器或物理设备中运行它。
其他回答
首先,这是屏幕加载图像矢量资产可见性
点击它将改变这个图像的可见性关闭
以上密码开关代码(xml代码)
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/laypass"
android:layout_width="330dp"
android:layout_height="50dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="@+id/editText3"
app:layout_constraintStart_toStartOf="@+id/editText3"
app:layout_constraintTop_toBottomOf="@+id/editText3">
<EditText
android:id="@+id/edit_password"
style="@style/EditTextTheme"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/round"
android:drawableLeft="@drawable/ic_password"
android:drawablePadding="10dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:paddingLeft="10dp"
android:paddingRight="15dp"
android:textColor="@color/cyan92a6"
android:textColorHint="@color/cyan92a6"
android:textCursorDrawable="@null"
android:textSize="18sp"
/>
<ImageView
android:id="@+id/show_pass_btn"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="8dp"
android:alpha=".5"
android:onClick="ShowHidePass"
android:padding="5dp"
android:src="@drawable/ic_visibility"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/laypass"
app:layout_constraintTop_toTopOf="@+id/edit_password" />
</androidx.constraintlayout.widget.ConstraintLayout>
按钮操作的Java代码
public void ShowHidePass(View view) {
if(view.getId()==R.id.show_pass_btn){
if(edit_password.getTransformationMethod().equals(PasswordTransformationMethod.getInstance())){
((ImageView)(view)).setImageResource(R.drawable.ic_visibility_off);
//Show Password
edit_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
else{
((ImageView)(view)).setImageResource(R.drawable.ic_visibility);
//Hide Password
edit_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
}
我觉得我想回答这个问题,即使有一些好的答案,
根据文档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());
}
一个很好的解决方案。设置一个按钮,然后使用下面的代码:
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");
}
}
我可以添加ShowPassword / HidePassword代码,只需几行,在一个块中独立:
protected void onCreate(Bundle savedInstanceState) {
...
etPassword = (EditText)findViewById(R.id.password);
etPassword.setTransformationMethod(new PasswordTransformationMethod()); // Hide password initially
checkBoxShowPwd = (CheckBox)findViewById(R.id.checkBoxShowPwd);
checkBoxShowPwd.setText(getString(R.string.label_show_password)); // Hide initially, but prompting "Show Password"
checkBoxShowPwd.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if (isChecked) {
etPassword.setTransformationMethod(null); // Show password when box checked
checkBoxShowPwd.setText(getString(R.string.label_hide_password)); // Prompting "Hide Password"
} else {
etPassword.setTransformationMethod(new PasswordTransformationMethod()); // Hide password when box not checked
checkBoxShowPwd.setText(getString(R.string.label_show_password)); // Prompting "Show Password"
}
}
} );
...
这是我的工作。这绝对对你有帮助
showpass.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(!isChecked){
// show password
password_login.setTransformationMethod(PasswordTransformationMethod.getInstance());
Log.i("checker", "true");
}
else{
Log.i("checker", "false");
// hide password
password_login.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
});
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。