Show the password with EditText

2020-05-15 07:40发布

I use an EditText to enter password. And a CheckBox to show password or not. Below function is the part:

public void ShowPassword() {
    if (cb.isChecked()) {
        password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
    } else {
        password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }
}

When it checked, it show password. But when it not checked, it does show stars. How to modify it to show star while the cb is not checked?

12条回答
闹够了就滚
2楼-- · 2020-05-15 08:03

okay:

To show readeable text:

setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD)

To "star" the password:

setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD)

But why? The Type_Text_Variation_Password set it to stars, but why does only adding Type_Claas_Text revive the visibel password?
I would have thought that Type_Text_Variation_VISIBLE_Password comes into play ...

查看更多
看我几分像从前
3楼-- · 2020-05-15 08:05

This will work -

public void ShowPassword() {
    if (cb.isChecked()) {
        password.setInputType(InputType.TYPE_CLASS_TEXT);
    } else {
        password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }
}
查看更多
贼婆χ
4楼-- · 2020-05-15 08:07

The Password Visibility Toggle feature has been added to support library version 24.2.0 enabling you to toggle the password straight from the password field without the need for a CheckBox.

You can make that work basically by setting an inputType of password on the TextInputEditText. Here's how to do that:

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/password"
            android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>
查看更多
啃猪蹄的小仙女
5楼-- · 2020-05-15 08:11

instead of visible password, can you try with TYPE_TEXT_VARIATION_NORMAL

public void ShowPassword() {
password.setInputType((cb.isChecked()) ? 
InputType.TYPE_TEXT_VARIATION_NORMAL : InputType.TYPE_TEXT_VARIATION_PASSWORD;
}
查看更多
Bombasti
6楼-- · 2020-05-15 08:18

This is not an answer,

Answer already given and accepted..

I just want to clarify about 129

password.setInputType(129);

is Actually,

password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

'|' is not a pipe, it's a bitwise OR. It takes two binary numbers and if either of the bits of equal value are 1,

How this relates to the input type: Each of the InputTypes are actually just ints. TYPE_CLASS_TEXT is 1, and TYPE_TEXT_VARIATION_PASSWORD is 128 (or 10000000).

Perform a bitwise OR on them:

00000001

10000000

------------

10000001 which is 129.

Try entering input.setInputType(129); instead, you'll see it'll work. :)

查看更多
劳资没心,怎么记你
7楼-- · 2020-05-15 08:19

This might help you mate

checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // checkbox status is changed from uncheck to checked.
        if (!isChecked) {
            // show password
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            // hide password
            editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }
});
查看更多
登录 后发表回答