首先在xml里创建两个控件 EditText和CheckBox
然后就很简单了
dt1=(EditText)findViewById(R.id.password);
cb1=(CheckBox)findViewById(R.id.checkbox_1);
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(cb1.isChecked()){
dt1.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); //如果被选中则显示密码
dt1.setSelection(dt1.getText().length()); //TextView默认光标在最左端,这里控制光标在最右端
}else {
dt1.setTransformationMethod(PasswordTransformationMethod.getInstance()); //如果没选中CheckBox则隐藏密码
dt1.setSelection(dt1.getText().length());
}
}
});
效果如下: