以编程方式更改的EditText的输入类型的密码来师范大学和放大器;反之亦然 [英] Programmatically change input type of the EditText from PASSWORD to NORMAL & vice versa
问题描述
在我的应用程序,我有一个的EditText
,其默认的输入类型设置为安卓inputType =textPassword
通过deault。它有一个复选框
来的权利,这是最好的时候,改变了的EditText的输入类型为NORMAL纯文本。 code因为这是 -
In my application, I have an EditText
whose default input type is set to android:inputType="textPassword"
by deault. It has a CheckBox
to its right, which is when checked, changes the input type of that EditText to NORMAL PLAIN TEXT. Code for that is-
password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
我的问题是,当复选框被选中,应该重新设定输入方式为口令。我已经使用做到了 -
My problem is, when that CheckBox is unchecked it should again set the input type to PASSWORD. I've done it using-
password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
但是,这里面的EditText文本仍清晰可见。而对于吃惊的是,当我改变了orienatation,它会自动设置输入类型的密码,里面的文字是符号(如密码显示)。
But, the text inside that edittext is still visible. And for surprise, when I change the orienatation, it automatically sets the input type to PASSWORD and the text inside is bulleted (shown like a password).
任何方式实现这一目标?
Any way to achieve this?
推荐答案
就为了谁是有同样的问题的人。只需添加一个额外属性的EditText上编程和你做。
Just for the people who are having same problem. Just add an extra attribute to that EditText programmatically and you are done.
password.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
password.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
此外,确保光标在在的EditText文本的末尾。因为,当你改变输入类型,光标会自动设置为起点。因此,我建议使用下面的code:
Also, make sure that the cursor is at the end of the text in the EditText. Because, when you change the input type, the cursor will be automatically set to the starting point. So I suggest to use the following code:
et_password.setInputType(InputType.TYPE_CLASS_TEXT |
InputType.TYPE_TEXT_VARIATION_PASSWORD);
et_password.setSelection(et_password.getText().length());
这篇关于以编程方式更改的EditText的输入类型的密码来师范大学和放大器;反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!