Android:密码显示隐藏
activity_main.xml的完整代码:
<?xmlversion="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="10"android:inputType="textPassword"><requestFocus/></EditText><CheckBoxandroid:id="@+id/checkBox1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="显示密码"/></LinearLayout>
MainActivity.java里的完整代码:
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;publicclassMainActivityextendsActivity{privateTextVieweditText1;privateCheckBoxcheckBox1;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText1=(TextView)findViewById(R.id.editText1);checkBox1=(CheckBox)findViewById(R.id.checkBox1);checkBox1.setOnCheckedChangeListener(newOnCheckedChangeListener(){@OverridepublicvoidonCheckedChanged(CompoundButtonbuttonView,booleanisChecked){//TODOAuto-generatedmethodstubif(isChecked){//如果选中,显示密码editText1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());}else{//否则隐藏密码editText1.setTransformationMethod(PasswordTransformationMethod.getInstance());}}});}}
关键是:
editText1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
editText1.setTransformationMethod(PasswordTransformationMethod.getInstance());
效果如下:
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。