1.content_main.xml
java
//輸入框 <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPassword" android:ems="10" android:id="@+id/password" android:layout_marginTop="151dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> //選擇框 <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="xianshimima" android:id="@+id/show" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:checked="false" />
2.MainActivityandroid
//數據監聽 this.password = (EditText) super.findViewById(R.id.password); this.show = (CheckBox) super.findViewById(R.id.show); show.setOnClickListener(new OnClickListenerImp()); private class OnClickListenerImp implements View.OnClickListener { @Override public void onClick(View v) { //選中設置爲顯示密碼 if(show.isChecked()) { MainActivity.this.password.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); }else { MainActivity.this.password.setTransformationMethod(PasswordTransformationMethod.getInstance()); } } }