Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否可以在材料设计中设置计时器以显示/隐藏密码?_Java_Android_Android Textinputlayout_Material Components_Material Components Android - Fatal编程技术网

Java 是否可以在材料设计中设置计时器以显示/隐藏密码?

Java 是否可以在材料设计中设置计时器以显示/隐藏密码?,java,android,android-textinputlayout,material-components,material-components-android,Java,Android,Android Textinputlayout,Material Components,Material Components Android,因此,我使用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_togg

因此,我使用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秒后隐藏密码,眼睛图标会自动改变

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

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

这就是我如何添加计时器和隐藏密码的方法。在这里接受了Gabriele Mariotti的帮助

您好,感谢您的快速响应。。。实际上,显示/隐藏密码对我来说不是问题。如果密码是可见的,它只需要在上面加一个计时器,如果用户没有隐藏密码,就把它隐藏起来password@DarpalDhyani要知道密码是否可见,请参见以下行:
if(editText!=null&&editText.getTransformationMethod()instanceof PasswordTransformationMethod)
。它可以通过单击结束图标或通过代码来工作。放一个计时器,测试条件,然后显示/隐藏密码。好的,我知道了,谢谢你让我明白。如果您有任何资源可以查看
getTransformationMethod()
PasswordTransformationMethod
,也将不胜感激。。。。。以前从未使用过它。
TransformationMethods
TextView
用于替换字符等操作。这是一个“旧类”,用点替换字符。谢谢你的帮助。它解决了我的问题。用上面的代码设置计时器解决了这个问题。为什么要这样做?看起来可能很烦人。
    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();
            }
        }
    }