【问题标题】:Is it possible to set a timer to show / hide passwords in material design?是否可以设置计时器以在材料设计中显示/隐藏密码?
【发布时间】:2020-03-15 05:18:18
【问题描述】:

所以,我正在使用 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 秒后隐藏密码并且眼睛图标会自动更改。

【问题讨论】:

  • 您为什么要这样做?这似乎很烦人。

标签: java android android-textinputlayout material-components material-components-android


【解决方案1】:

要显示/隐藏密码,您可以使用以下内容:

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);

【讨论】:

  • 您好,感谢您快速回复...实际上显示/隐藏密码对我来说不是问题。如果密码可见,则只需在其上放置一个计时器,如果用户没有隐藏密码,则将其隐藏
  • @DarpalDhyani 要知道密码是否可见,这一行是:if (editText != null &amp;&amp; editText.getTransformationMethod() instanceof PasswordTransformationMethod)。它既可以单击 endIcon 也可以通过代码工作。放置一个计时器,测试条件,然后显示/隐藏密码。
  • 好的,谢谢你让我明白了。如果您有任何资源可以查看getTransformationMethod()PasswordTransformationMethod ..... 之前从未使用过它,我们将不胜感激。
  • TransformationMethodsTextView 用来做诸如替换字符之类的事情。 PasswordTransformationMethod 是一个“旧类”,它用点替换字符。
  • 感谢您的帮助。它解决了我的问题。用上面的代码设置定时器解决了这个问题。
【解决方案2】:
    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();
            }
        }
    }

这就是我设法添加计时器并隐藏密码的方法。在 link 获得 Gabriele Mariotti 的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-03
    • 2015-02-05
    • 1970-01-01
    • 2021-04-17
    • 2015-06-29
    • 2016-04-25
    • 1970-01-01
    • 2015-07-15
    相关资源
    最近更新 更多