Display and hide Android passwords and hide android passwords

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.