Display and hide Android passwords and hide android passwords
Many applications use the function of displaying and hiding passwords.
The previous project did not have this function requirement, nor did it specifically study this. This feature has been added to recent projects, and I just sorted it out here.
My idea is to set the InputType of EditText. The Code is as follows:
If (mPasswordVisible) {// set the EditText password to a visible edtPassword. setInputType (InputType. TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);} else {// set the password to a hidden edtPassword. setInputType (InputType. TYPE_CLASS_TEXT | InputType. TYPE_TEXT_VARIATION_PASSWORD );}
View Code
Run the code to hide the password.
Although I have implemented this function, I am still curious about how others implement it.
Search online and find that most people use the following methods:
If (mPasswordVisible) {// set the EditText password to a visible edtPassword. setTransformationMethod (HideReturnsTransformationMethod. getInstance ();} else {// set the password to a hidden edtPassword. setTransformationMethod (PasswordTransformationMethod. getInstance ());}
View Code
Run the code to display the hidden password normally.
The above two methods can achieve this function, but the second method is too unfamiliar...
In the preceding example, the cursor jumps to the top of the screen after the explicit hidden state is switched. To solve this problem, add the following code:
edtPassword.setSelection(edtPassword.getText().length());
View Code
Perfect solution.