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: