使用了网上搜的修改inputType的方法,但是在小米手机上有问题,小米的安全键盘会在切换时修改type为text,切换体验很差,其他机型没试过,但是发现使用TextInputLayout没有这个问题,遂查看它的源码,盗用TextInputLayout的方法后,实现效果如下:
代码如下:
/**
* 可视化切换
*/
private fun passwordToggle() {
//开关
showEye = !showEye
//记录光标的位置
val selection = et_pwd.selectionEnd
//切换可视化
et_pwd.transformationMethod = if (showEye) null else
PasswordTransformationMethod.getInstance()
//将光标移动到last
et_pwd.setSelection(selection)
//切换开关图片
ib_show_pwd.setImageResource(if (showEye) R.mipmap.login_show_pwd else R.mipmap.login_hide_pwd)
}
通过TextView的setTransformationMethod(TransformationMethod method)实现的,在密码可视化切换时inputType将不会改变,类似于微信那种效果