1.需要在EditText中设置属性password
<EditText
android:id="@+id/et_psw"
android:layout_height="wrap_content"
android:layout_width="200dp"
android:password="true" />
2. 设置checkBox的监听
cb_show.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
ed_psw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
}
else{
ed_psw.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
}
});
注意:当由显示变成隐藏时,
InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD