之间是否有一个聪明的办法让隐藏和查看密码的用户切换在Android的EditText切换? 许多基于PC的应用程序都让用户执行此操作。如何隐藏和查看密码
回答
您可以动态更改TextView的属性。如果您将XML Atrribute android:password
设置为true,则该视图将显示点,如果将其设置为false,则显示文本。
随着setTransformationMethod你应该能够改变从代码这个属性的方法。 (免责声明:。如果方法仍然工作时显示的视图后,如果遇到问题,这给我留下了评论,我知道我没有测试过)
完整的示例代码将
yourTextView.setTransformationMethod(new PasswordTransformationMethod());
隐藏密码。要显示密码,您可以设置一个现有的转换方法,或者实现一个空的TransformationMethod,它不会对输入文本产生任何影响。
yourTextView.setTransformationMethod(new DoNothingTransformation());
要显示的点,而不是密码的设置PasswordTransformationMethod:
yourEditText.setTransformationMethod(new PasswordTransformationMethod());
当然
缺省情况下可以在XML布局你的EditText元素设置此
android:password
要重新显示读取密码,只要传递null作为变换法:
yourEditText.setTransformationMethod(null);
'android:password'现已弃用,您应该使用'android:inputType'代替 – Wilka 2011-07-10 17:28:17
您也可以通过在设置转换方法之前保存光标位置并在其之后进行恢复来使用户满意,使用'editText.getSelectionStart )'和'editText.getSelectionEnd()'用于保存光标位置,'editText.setSelection(start,end)'用于恢复它。 – Mostafa 2011-09-07 11:21:14
@Wilka:android:inputType不允许您在运行时在两个状态之间来回切换 - 它只允许你切换一次,一旦你改变它,你不能改变它,并且不,setTransformationMethod不会被废弃。 – AndroidDev 2013-06-30 13:20:27
要显示:
editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
要隐藏:
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
每个这些以后,光标被复位,所以:
editText.setSelection(editText.length());
在Android 4.3和5.0上测试,效果很棒! Docs使它看起来像应该一直工作到API 3。 – James 2014-12-30 13:14:53
@MattLogan但如何获得键盘显示/隐藏键? – NarendraJi 2015-08-19 06:05:32
这应该是公认的答案 – 2015-08-28 08:11:41
使用复选框并相应地改变输入类型。
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int start,end;
Log.i("inside checkbox chnge",""+isChecked);
if(!isChecked){
start=passWordEditText.getSelectionStart();
end=passWordEditText.getSelectionEnd();
passWordEditText.setTransformationMethod(new PasswordTransformationMethod());;
passWordEditText.setSelection(start,end);
}else{
start=passWordEditText.getSelectionStart();
end=passWordEditText.getSelectionEnd();
passWordEditText.setTransformationMethod(null);
passWordEditText.setSelection(start,end);
}
}
工作就像一个魅力谢谢:) – 2015-08-11 13:03:05
这对我很有用。这将帮助你肯定
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());
}
}
});
我觉得我要回答这个问题,甚至有一些很好的答案,
根据文档TransformationMethod做我们的使命
TransformationMethod
TextView使用TransformationMethods来执行诸如替换之类的操作210个带点的密码字符,或保持换行符 在单行文本字段中不产生换行符。
注意我用黄油刀,但其相同的,如果用户检查显示密码
@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());
}
我能够用短短几行添加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"
}
}
});
...
尝试https://github.com/maksim88/PasswordEditText项目在github。 你甚至不需要使用它来改变你的Java代码。只要改变
的EditText
标签
com.maksim88.passwordedittext.PasswordEditText
在XML文件中。
你有任何想法如何使用setError这个组件,一旦错误被标记显示/隐藏图标变得不可见 – guisantogui 2016-02-18 19:53:38
自支持库v24.2.0以来,实现起来非常简单。
您需要做的仅仅是:
结合添加设计库添加到依赖条件
dependencies { compile "com.android.support:design:24.2.0" }
使用
TextInputEditText
与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 documentation。
在版本25.1.0我有一个奇怪的行为:它显示密码切换一次,但如果你按下,它会消失o_O' – MiguelHincapieC 2017-01-02 21:38:17
是的,也有一些''TextInputEditText'上的'android:text'属性的怪癖。您可以随时在[Android版Google问题跟踪器](https://code.google.com/p/android/issues/list)上提出问题 – mmBs 2017-01-03 08:48:37
有什么方法可以将图标移动到左侧? – 2017-01-12 12:44:45
我所做的就是
- 创建一个编辑文本视图和一个普通的文本视图
- 请使用约束布局(就像Facebook应用程序登录屏幕它们相互重叠)
- 将onClickListener附加到普通文本视图,以便相应地更改编辑文本视图的输入类型(可见/不可见)
您可以为一个更详细的步骤和解释,看看这个视频https://youtu.be/md3eVaRzdIM
希望它能帮助:)
private int passwordNotVisible=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
showPassword = (ImageView) findViewById(R.id.show_password);
showPassword.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText paswword = (EditText) findViewById(R.id.Password);
if (passwordNotVisible == 1) {
paswword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
passwordNotVisible = 0;
} else {
paswword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
passwordNotVisible = 1;
}
paswword.setSelection(paswword.length());
}
});
}
您可以使用此下面的代码显示/隐藏密码:
XML代码:
<EditText
android:id="@+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="21dp"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<CheckBox
android:id="@+id/cbShowPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/etPassword"
android:layout_below="@+id/etPassword"
android:text="@string/show_pwd" />
Java代码:
EditText mEtPwd;
CheckBox mCbShowPwd;
mEtPwd = (EditText) findViewById(R.id.etPassword);
mCbShowPwd = (CheckBox) findViewById(R.id.cbShowPwd);
mCbShowPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// checkbox status is changed from uncheck to checked.
if (!isChecked) {
// show password
mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
} else {
// hide password
mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}
}
});
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());
}
您可以使用应用程序:passwordToggleEnabled = “真”
这里例如下面
<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">
这里给出的是我的解决方案,而无需使用TextInputEditText和转换方法。
XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
style="@style/FormLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/username" />
<EditText
android:id="@+id/loginUsername"
style="@style/EditTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_person_outline_black_24dp"
android:drawableStart="@drawable/ic_person_outline_black_24dp"
android:inputType="textEmailAddress"
android:textColor="@color/black" />
<TextView
style="@style/FormLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/password" />
<EditText
android:id="@+id/loginPassword"
style="@style/EditTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/ic_visibility_off_black_24dp"
android:drawableLeft="@drawable/ic_lock_outline_black_24dp"
android:drawableRight="@drawable/ic_visibility_off_black_24dp"
android:drawableStart="@drawable/ic_lock_outline_black_24dp"
android:inputType="textPassword"
android:textColor="@color/black" />
</LinearLayout>
Java代码的
boolean VISIBLE_PASSWORD = false; //declare as global variable befor onCreate()
loginPassword = (EditText)findViewById(R.id.loginPassword);
loginPassword.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_LEFT = 0;
final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
final int DRAWABLE_BOTTOM = 3;
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (loginPassword.getRight() - loginPassword.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
// your action here
//Helper.toast(LoginActivity.this, "Toggle visibility");
if (VISIBLE_PASSWORD) {
VISIBLE_PASSWORD = false;
loginPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
loginPassword.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_lock_outline_black_24dp, 0, R.drawable.ic_visibility_off_black_24dp, 0);
} else {
VISIBLE_PASSWORD = true;
loginPassword.setInputType(InputType.TYPE_CLASS_TEXT);
loginPassword.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_lock_outline_black_24dp, 0, R.drawable.ic_visibility_black_24dp, 0);
}
return false;
}
}
return false;
}
});
编译 'com.android.support:appcompat-v7:24.2.0'
编译“ com.android.support:design:24.2.0'
布局
android:inputType="textPassword"
其工作
- 1. 如何在ios中隐藏和查看密码之间切换?
- 2. 如何查看和隐藏表中所有行的密码
- 3. 隐藏密码
- 4. 隐藏密码()
- 5. 隐藏UITextField密码
- 6. 如何从代码文档中隐藏查看源代码?
- 7. 如何隐藏数据库密码?
- 8. 如何用密码术隐藏javascript?
- 9. 隐藏所有MongoDB查询的密码
- 10. 如何查看内置的“隐藏”python?
- 11. Active Directory如何查看隐藏属性
- 12. Android的隐藏号码,如密码
- 13. 隐藏窗口,用户名和密码
- 14. 显示和隐藏密码字符
- 15. 隐藏/查看编辑查看表格
- 16. 用0height和0width查看并不隐藏
- 17. HTML点击查看和隐藏公告
- 18. 如何在网站中隐藏“查看源代码”选项
- 19. 在android pdf查看器barteksc库中,如何隐藏页码?
- 20. 隐藏密码字符
- 21. 隐藏模拟的密码
- 22. 隐藏密码字段
- 23. 在textField中隐藏密码“•••••••”
- 24. Vim语法隐藏密码
- 25. Jetty-ssl.xml密码隐藏
- 26. 隐藏密码字符串
- 27. iSeries连接隐藏密码
- 28. 隐藏MySQL的密码
- 29. 詹金斯 - 隐藏查看
- 30. 隐藏详细查看
要显示密码,您不需要创建任何新类。只需调用'setTransformationMethod(null)'。 – 2014-07-25 01:06:48
@Janusz,下面的使用会给gud解决方案。setTransformationMethod(PasswordTransformationMethod.getInstance());和setTransformationMethod(HideReturnsTransformationMethod.getInstance()); – 2015-04-13 13:31:40
@Janusz但如何让键盘显示/隐藏键? – NarendraJi 2015-08-19 06:04:17