我正在 Android (Kotlin) 中使用 TextInputLayout 构建一个密码字段,当我使用 endIconMode 属性时,最后的图标会被启用(更确切地说,它显示可见的眼睛图标),并且每当我单击它时密码可见,即使密码可见,最后它也会显示可见性关闭图标。我该如何解决这个问题? 这是我的 XML 代码:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/password_field"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/password"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="5dp"
android:hint="Enter your password"
app:boxBackgroundColor="#1AC4C4C4"
app:boxCornerRadiusBottomEnd="20dp"
app:boxCornerRadiusBottomStart="20dp"
app:boxCornerRadiusTopEnd="20dp"
app:boxCornerRadiusTopStart="20dp"
app:boxStrokeColor="@color/text_field_stroke"
app:boxStrokeWidth="0.5dp"
app:endIconMode="password_toggle">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/roboto"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
最佳答案
我找到的解决方案是,您应该将 endIconDrawable 制作为自定义的,并以这种方式自定义它:
**password_icon.xml**
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_visibile" android:state_checked="true"/>
<item android:drawable="@drawable/ic_visibility_off"/>
</selector>
并以这种方式将其添加到 TestInputLayout 上:
app:endIconDrawable="@drawable/password_icon"
关于android - 在 TextInputLayout 中使用 endIconMode 默认情况下启用结束图标(可见图标),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70664655/