通过代码来动态的设置,这里面我放了两个控件,一个是EditText,另一个是Button,
实现的目的是通过每一次的点击按钮实现EditText里面的文字可见和不可见的交替变换
具体的代码的实现是:
CheckBox cbEye = (CheckBox) findViewById(R.id.cb_password);
etPassword = (EditText) findViewById(R.id.et_password);
cbEye.setOnCheckedChangeListener(onCheckedChangeListener);
CompoundButton.OnCheckedChangeListener onCheckedChangeListener =new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.cb_password:
if (isChecked){
//选中显示密码
etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else {
//隐藏密码
etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
//把光标设置在文字结尾
etPassword.setSelection(etPassword.getText().length());
break;
}
}
};