Many apps are features that show and hide passwords.
None of the previous projects had this functional requirement, nor did they specifically study this. Recently the project has added this function, I also just have to tidy up here.
My train of thought is to set EditText's InputType. The code is as follows:
if (mpasswordvisible) { // set EditText's password as visible edtpassword.setinputtype ( Inputtype.type_text_variation_visible_password); Else { // Set password for hidden edtpassword.setinputtype (inputtype.type_class_text | Inputtype.type_text_variation_password); }
View Code
Run the code to display the hidden password normally.
Although I realized this function, I was still curious about how others realized it.
Search the Internet, find that most people are using the following methods:
if (mpasswordvisible) { // set EditText's password as visible Edtpassword.settransformationmethod (Hidereturnstransformationmethod.getinstance ()); Else { // Set password as hidden Edtpassword.settransformationmethod ( Passwordtransformationmethod.getinstance ()); }
View Code
Running the code will also show the hidden password as normal.
Both of these methods can achieve this function, but the second method is too strange ...
Above, there is a problem, after switching the explicit, the cursor will jump to the front, in order to solve this problem, you need to add the following code:
Edtpassword.setselection (Edtpassword.gettext (). Length ());
View Code
Solve the problem perfectly.
Android Password display and hide