使用密码切换显示或隐藏文本

San*_*iya 8 android android-edittext android-textinputlayout material-components material-components-android

我正在使用以下代码显示或隐藏密码

pwdLayout.setEndIconMode(TextInputLayout.END_ICON_PASSWORD_TOGGLE);
Run Code Online (Sandbox Code Playgroud)

我能够实现显示或隐藏密码。但是带有十字符号的眼睛图标显示密码,没有十字符号的眼睛图标隐藏密码。我需要颠倒这个逻辑怎么做?

Gab*_*tti 8

从开始1.2.0这是默认的行为:

在此处输入图片说明 在此处输入图片说明

如果要反转图标,可以使用以下内容:

<com.google.android.material.textfield.TextInputLayout
    app:endIconDrawable="@drawable/custom_password_eye"
Run Code Online (Sandbox Code Playgroud)

和:

<animated-selector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="NewApi">

    <item
        android:id="@+id/visible"
        android:drawable="@drawable/design_ic_visibility"
        android:state_checked="true"/>

    <item
        android:id="@+id/masked"
        android:drawable="@drawable/design_ic_visibility_off"/>

    <transition
        android:drawable="@drawable/avd_show_password"
        android:fromId="@id/masked"
        android:toId="@id/visible"/>

    <transition
        android:drawable="@drawable/avd_hide_password"
        android:fromId="@id/visible"
        android:toId="@id/masked"/>

</animated-selector>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明 在此处输入图片说明