Show the password with EditText

前端 未结 12 894
旧时难觅i
旧时难觅i 2021-01-30 22:04

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()) {
          


        
相关标签:
12条回答
  • 2021-01-30 22:44

    Call this method inside your OnCheckedChangedListener

     public static void toggleShowPassword(boolean show, EditText editText) {
        if (show)
            editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        else
            editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        editText.setSelection(editText.length());
    }
    

    The EditText cursor resets its position after changing the InputType that's why we add the last line editText.setSelection(editText.length())

    0 讨论(0)
  • 2021-01-30 22:45
    public void onCheckBox(View v2)
    {
    
    
    
        CheckBox cb = (CheckBox)this.findViewById(R.id.pass_Check);
        EditText et1=(EditText)this.findViewById(R.id.edt_Pass);
            if(cb.isChecked())
            {
            et1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            }
            else 
            {
            et1.setInputType(129);
            }
    
    }
    
    0 讨论(0)
  • 2021-01-30 22:47

    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. :)

    0 讨论(0)
  • 2021-01-30 22:47

    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());
            }
        }
    });
    
    0 讨论(0)
  • 2021-01-30 22:47

    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;
    }
    
    0 讨论(0)
  • 2021-01-30 22:47

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题