<EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@null" android:hint="密码" android:inputType="textPassword" android:padding="5dp" android:textSize="14sp" /> <CheckBox android:id="@+id/cb_password_vis" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:button="@drawable/password_selector" />
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@mipmap/icon_visible_passwords_ng" android:state_checked="true" /> <item android:drawable="@mipmap/icon_invisible_ng" android:state_checked="false" /> </selector>
cbPasswordVis.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ etPassword.setInputType(InputType.TYPE_CLASS_TEXT); }else{ etPassword.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD); } } });