如何在android中显示密码开/关模式的切换按钮

问题描述 投票:1回答:1

我编辑了带有切换按钮的文本输入布局的文本。我的切换按钮(睁眼和交叉/关闭)可见但不显示开/关模式,如睁眼或交叉眼。我的代码如下:

 <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColorHint="@color/white"
            app:errorTextAppearance="@style/PasswordErrorAppearance"
            app:passwordToggleEnabled="true"
            >

            <EditText
                android:id="@+id/input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/hint_password"
                android:inputType="textPassword"
                android:textColor="@android:color/white" />
        </android.support.design.widget.TextInputLayout>

和风格是:

<style name="PasswordErrorAppearance" parent="@android:style/TextAppearance">
        <item name="android:textColor">#ff0000</item>

    </style>
android-edittext passwords android-textinputlayout
1个回答
1
投票

我得到了上述问题的答案,请按照以下流程进行操作,您将看到显示密码的切换按钮的开/关状态。

使用EditText创建TextInputLayout。

 <android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColorHint="@color/white"
        app:passwordToggleDrawable="@drawable/toggle_password"
        app:errorTextAppearance="@style/PasswordErrorAppearance"
        >

        <EditText
            android:id="@+id/input_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_password"
            android:inputType="textPassword"
            android:textColor="@android:color/white"
          />
    </android.support.design.widget.TextInputLayout>

其中app:passwordToggleDrawable =“@ drawable / toggle_password”添加此行,其中toggle_password是选择器xml。现在我正在显示选择器xml。

toggle_password.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/open_eye" android:state_checked="true"
    android:state_pressed="true" />
//currently pressed turning the toggle on
<item android:drawable="@drawable/eye_blocked" android:state_pressed="true" />
//currently pressed turning the toggle off
<item android:drawable="@drawable/eye_blocked" android:state_checked="true" />
//not pressed default checked state
<item android:drawable="@drawable/open_eye" />
//default non-pressed non-checked

在完成该过程后,您将能够进入开/关状态。对其他人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.