是否可以在 material 设计中设置计时器来显示/隐藏密码?

Is it possible to set a timer to show / hide passwords in material design?

所以,我正在使用 androidx 库,并在我的 xml 中实现了以下代码来显示/隐藏密码,

  <com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_text"
    app:endIconMode="password_toggle">

  <com.google.android.material.textfield.TextInputEditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inputType="textPassword"/>

</com.google.android.material.textfield.TextInputLayout>

我真正想要做的是,当用户输入密码时......当用户点击眼睛图标显示密码时......它应该在大约 10 秒后隐藏密码并且眼睛图标会自动更改.

要show/hide密码你可以这样用:

TextInputLayout password = findViewById(R.id......);
EditText editText = password.getEditText();
if (editText == null) {
  return;
}
// Store the current cursor position
final int selection = editText.getSelectionEnd();

//This is core condition. It is the key to know if the password is just visible or not.
if (editText != null
    && editText.getTransformationMethod() instanceof PasswordTransformationMethod) {
  editText.setTransformationMethod(null);
} else {
  editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
}

// And restore the cursor position
editText.setSelection(selection);
    private void showHidePassword() {
        textInputLayout = activityBinding.textInputLayout;
        textInputEditText = activityBinding.editTextPassword;

        textInputEditText = (TextInputEditText) textInputLayout.getEditText();
        if (textInputEditText == null) {
            return;
        }
        textInputEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                try {
                    if (textInputEditText.getTransformationMethod() == null) {
                        stopHandlerPassword();
                        setTimerPasswordEditText();
                    } else {
                        stopHandlerPassword();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    private void setTimerPasswordEditText() {
        passwordEditTextRunnable = new Runnable() {
            public void run() {
                try {
                    if (textInputEditText != null && textInputEditText.getTransformationMethod() == null) {
                        hidePassword();
                    }
                } catch (RuntimeException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        passwordHandler.postDelayed(passwordEditTextRunnable, 8000);
    }

    private void stopHandlerPassword() {
        passwordHandler.removeCallbacks(passwordEditTextRunnable);
    }

    private void hidePassword() {
        if (textInputEditText != null) {
            textInputEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
            try {
                int selection = textInputEditText.getSelectionEnd();
                textInputEditText.setSelection(selection);
              }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

这就是我设法添加计时器并隐藏密码的方法。在这

得到了 Gabriele Mariotti 的帮助