当我运行这段代码时,密码可见性切换图标是反转的。如果密码被隐藏,它会显示“睁眼”图标,反之亦然。
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/id_txtInpLayout_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
android:layout_marginTop="10dp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/id_txtInpEditTxt_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/hint_pass"
android:inputType="textPassword"
android:ems="14"/>
</com.google.android.material.textfield.TextInputLayout>
我没有在 MainActivity.java 中编写任何代码。
最佳答案
app:passwordToggleEnabled
已弃用。为了更好地使用,您现在应该像这样在 TextInputLayout 中使用 endIconMode:
app:endIconMode="password_toggle"
然后在您的 TextInputEditText 中使用:
android:inputType="textPassword"
就是这样。以下是完整的示例 XML 代码:
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:id="@+id/editTextLayoutPassword"
android:layout_width="300dp"
android:layout_height="match_parent"
android:hint="Password"
app:endIconMode="password_toggle">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:maxLength="20" />
</com.google.android.material.textfield.TextInputLayout>
编辑:看起来它可能仍然是倒置的,所以这是最终的解决方案。
创建一个新的 Drawable 资源文件,将其命名为 custom_eye
并插入以下代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_opened_eye"
android:state_checked="true"/>
<item android:drawable="@drawable/ic_closed_eye"/>
</selector>
然后只需下载两个 SVG 图标(您可以选择 here 您的图标)。
最后,只需将它们作为“Vector Assets”导入到 drawable 文件夹中并重命名一个 ic_opened_eye
,另一个 ic_closed_eye
在这里,您可以更改图标以提高密码可见性.
然后只需在您的 TextInputLayout 中使用 app:endIconDrawable="@drawable/custom_eye"
。
如果您仍然不喜欢它,只需在 XML 文件中随时反转两个图标即可。
关于android-studio - 密码切换图标在 Android Studio 的 Material Design 中倒置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67556571/