首先初始化
togglePasswordVisibility(views.tvKey)
其次处理点击事件
views.tvPrivateKey.debouncedClicks {
togglePasswordVisibility(views.tvKey)
}
private var isPasswordVisible = false
private fun togglePasswordVisibility(textView: AppCompatTextView) {
if (isPasswordVisible) {
// 显示明文
views.imPrivateKeyState.setImageResource(R.drawable.ic_private_eye_close)
textView.transformationMethod = null
} else {
// 用星号隐藏
views.imPrivateKeyState.setImageResource(R.drawable.ic_private_eye_open)
textView.transformationMethod = PasswordTransformationMethod.getInstance()
}
isPasswordVisible = !isPasswordVisible
}
网友评论