Effect:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" > <EditTextAndroid:id= "@+id/edittext1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:ems= "Ten"Android:inputtype= "Textpassword" > <Requestfocus/> </EditText> <CheckBoxAndroid:id= "@+id/checkbox1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Show password" /></LinearLayout>
Packagecom.example.test;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.text.method.HideReturnsTransformationMethod;ImportAndroid.text.method.PasswordTransformationMethod;ImportAndroid.widget.CheckBox;ImportAndroid.widget.CompoundButton;ImportAndroid.widget.CompoundButton.OnCheckedChangeListener;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {PrivateTextView EditText1;PrivateCheckBox CheckBox1; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.test); EditText1=(TextView) Findviewbyid (R.ID.EDITTEXT1); CheckBox1=(CheckBox) Findviewbyid (r.id.checkbox1); Checkbox1.setoncheckedchangelistener (NewOncheckedchangelistener () {@Override Public voidOnCheckedChanged (Compoundbutton Buttonview,BooleanisChecked) { //TODO auto-generated Method Stub if(isChecked) {//If selected, displays the passwordEdittext1.settransformationmethod (Hidereturnstransformationmethod.getinstance ()); }Else{ //Otherwise hide the passwordEdittext1.settransformationmethod (Passwordtransformationmethod.getinstance ()); } } }); }}
The key is:
Edittext1.settransformationmethod (Hidereturnstransformationmethod.getinstance ());
Edittext1.settransformationmethod (Passwordtransformationmethod.getinstance ());
Android: Password Display hidden