Display and hide the password in the Android text input box (EditText)

Source: Internet
Author: User

MainActivity is as follows:

Package cc. c; import android. app. activity; import android. OS. bundle; import android. text. selection; import android. text. spannable; import android. text. method. hideReturnsTransformationMethod; import android. text. method. passwordTransformationMethod; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText;/*** Demo Description: * Switch password in the text input box (EditText) Code display and hide ** reference: * 1 http://developer.android.com/reference/android/text/method/HideReturnsTransformationMethod.html * 2 http://developer.android.com/reference/android/text/method/PasswordTransformationMethod.html * 3 http://blog.csdn.net/dawanganban/article/details/23374187 * Thank you very much */public class MainActivity extends Activity {private Button mSwitchButton; private EditText mPassword EditText; private boolean isHidden = true; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mSwitchButton = (Button) findViewById (R. id. button); mPasswordEditText = (EditText) findViewById (R. id. editText); mSwitchButton. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v ){ If (isHidden) {// set EditText to visible mPasswordEditText. setTransformationMethod (HideReturnsTransformationMethod. getInstance ();} else {// set EditText to a hidden mPasswordEditText. setTransformationMethod (PasswordTransformationMethod. getInstance ();} isHidden =! IsHidden; mPasswordEditText. postInvalidate (); // After switching, place the EditText cursor at the end of CharSequence charSequence = mPasswordEditText. getText (); if (charSequence instanceof Spannable) {Spannable spanText = (Spannable) charSequence; Selection. setSelection (spanText, charSequence. length ());}}});}}

Main. xml is as follows:

 
     
          
  
 


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.