Show the password with EditText

前端 未结 12 866
旧时难觅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())

提交回复
热议问题