简体   繁体   English

如何在密码字段中显示或隐藏文本

[英]How to show or hide text in a password field

I would like to change the input type in a password field to show or hide the password. 我想在密码字段中更改输入类型以显示或隐藏密码。

An example is the Facebook login where the password field has an eye icon that shows the password when I type in it. 一个示例是Facebook登录名,其中密码字段带有一个眼睛图标,当我键入密码时,它会显示密码。

How can I achieve that in Android? 如何在Android中实现?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_Password);
    final EditText password = (EditText)findViewById(R.id.editText_Password);
    ((CheckBox)findViewById(R.id.checkBox_ShowPassword))
            .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            password.setInputType(isChecked ? 
                    InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : 
                    InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM