使用密码输入时,Edittext字段未隐藏密码

3

在使用密码输入时,Android中的编辑文本字段无法隐藏密码。之前它是可以工作的,但我现在无法弄清楚出了什么问题或者发生了什么改变。以下是源代码:

XML

<EditText
    android:id="@+id/login_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:background="@drawable/rectangular_border_edittext"
    android:hint="@string/enter_password"
    android:inputType="textPassword"
    android:maxLines="1"
    android:padding="8dp" />

JAVA

   password.setSingleLine();
    password.setImeOptions(EditorInfo.IME_ACTION_NEXT);   password.setImeActionLabel(getResources().getString(R.string.goButton), EditorInfo.IME_ACTION_NEXT);
    password.setOnEditorActionListener((v, actionId, event) -> {
                if (actionId == EditorInfo.IME_ACTION_NEXT) {
                    checkPasswordAndSend();
                }
                return false;
            });

如果有人之前遇到过类似的问题,请告诉我。另外,我想告诉你,我正在使用最新版本的支持库(25.3.1)。
2个回答

3

所以我找到了解决方法。密码中的maxlines属性会导致这种行为。从xml中删除maxLines = 1并从java中设置setSingleLine,一切都恢复正常。不知道为什么它有效,但它就是有效的。 希望这能帮助某些人。


2

请在您的XML中删除此行:

android:maxLines="1"

您应该拥有这个:

<EditText
android:id="@+id/login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/rectangular_border_edittext"
android:hint="@string/enter_password"
android:inputType="textPassword"
android:padding="8dp" />

请在您的JAVA中保留此行,就像您的示例中一样:

password.setSingleLine();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接