passwordToggleEnabled 似乎不起作用

passwordToggleEnabled doesn't seem to be working

我已经以通常的方式实现了带有密码字段的 TextInputLayout:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/etPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:passwordToggleEnabled="true">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="15dp"
        android:layout_marginRight="30dp"
        android:autofillHints="password"
        android:background="@drawable/edit_text_border"
        android:hint="@string/password"
        android:inputType="textPassword"
        android:textColor="#000000"
        android:textColorHint="#9e9e9e" />
</com.google.android.material.textfield.TextInputLayout>

我在依赖中添加了这个-

implementation 'com.google.android.material:material:1.1.0'

可见性 "eye- icon" 尚不可见。有没有我遗漏了什么或者我确实做错了什么?

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/pass"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true">

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

确保在您的应用中使用 Material Components theme 并使用如下布局:

<com.google.android.material.textfield.TextInputLayout
    ...
    android:hint="@string/password"
    app:endIconMode="password_toggle">

    <com.google.android.material.textfield.TextInputEditText
        ...
        android:inputType="textPassword"
    />

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

由于您使用的是 EditText,请检查 the official doc:

Note: A text field is composed of a TextInputLayout and a TextInputEditText as a direct child. Using an EditText as the child might work, but TextInputEditText provides accessibility support for the text field and allows TextInputLayout greater control over the visual aspects of the input text. If an EditText is being used, make sure to set its android:background to @null so that TextInputLayout can set the proper background on it.