Android Password display and hide

Source: Internet
Author: User

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

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.