android - 在 TextInputLayout 中使用 endIconMode 默认情况下启用结束图标(可见图标)

标签 android kotlin passwords

我正在 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> 

enter image description here

enter image description here

最佳答案

我找到的解决方案是,您应该将 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/

相关文章:

android - 如何将 Android DatePicker 模式设置为 21 级以下 API 的微调器

android - Stripe PaymentMethodsActivity 添加谷歌支付按钮

android - 没有这样的方法错误: No interface method getOrDefault(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; in class Ljava/util/Map;

asp.net - 为什么要加密 Web 配置文件?

android - 无法使用 android 将 react-native 更新到 0.59.2

java - 安卓工作室 : cannot read packageName from manifest

android - 在 Android 中与 Xmpp 服务器保持实时连接的最佳方法

spring - 使用具体实现时 @JsonTypeInfo 对 REST 端点的影响

ios - 在 iOS 中通过 http 提交密码的最佳做法是什么?

java - 我想在 Java 中制作一个隐藏的 "password"