相信这种效果特别常见,平时看到密码框右边有一个小眼睛,点击显示明文密码,再点击显示密文密码
直接看效果图吧!
先看隐藏效果
代码如下
//密码密文显示
pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
然后是显示密文密码
代码如下
//密码明文显示
pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
首先看初始化主要一个checkbox和一个EditText控件初始化代码主要是checkbox里面ischecked方法判断是否选中,这里不再赘述!
private CheckBox cb_isvisiblePwd;//checkbox
private EditText psw;//密码输入框
private boolean isVisiblePwd=false;//是否显示密码 默认不显示
private void initView() {
cb_isvisiblePwd= (CheckBox) findViewById(R.id.check);
psw= (EditText) findViewById(R.id.psw);
cb_isvisiblePwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(!isVisiblePwd){
if(cb_isvisiblePwd.isChecked()){
//密码明文显示
psw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else{
//密码密文显示
psw.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
psw.postInvalidate();
}
}
});
}
到这里结束了!主要做个笔记而已!