
最近在做一个登录、注册页面,里面需要显示或隐藏密码,故做了一个简单的显示和隐藏功能。
关键类TextView.setTransformationMethod(TransformationMethod method),其中TransformationMethod 有两个子类:
- HideReturnsTransformationMethod 隐藏回车
- PasswordTransformationMethod 密码类型
关键代码:
@OnClick(R.id.iv_psw_eye)
void clickPswEye() {
int tag = Integer.parseInt(pswEyeIV.getTag().toString());
if (tag == 1) {//显示密码
pswEyeIV.setTag(2);
pswEyeIV.setImageResource(R.mipmap.icon_psw_not_eye);
passwordET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {//隐藏密码
pswEyeIV.setTag(1);
pswEyeIV.setImageResource(R.mipmap.icon_psw_eye);
passwordET.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
我用ImageView的Tag属性存储当前密码输入框的类型,1是密码类型,2是显示类型。布局组件关键代码如下:
截图:
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)