如何在android中显示密码on/off模式的切换按钮
How to display toggle button of password on/off mode in andorid
我使用带有切换按钮的文本输入布局编辑文本。我的切换按钮(睁眼和 cross/Close)是可见的,但没有像睁眼或交叉眼那样显示开/关模式。
我的代码如下:
<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>
我得到了上述问题的答案,请按照下面的流程进行操作,您将获得on/off显示密码切换按钮的状态。
使用 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
完成此过程后,您将能够获得 on/off 状态。它可能对其他人有帮助。
我使用带有切换按钮的文本输入布局编辑文本。我的切换按钮(睁眼和 cross/Close)是可见的,但没有像睁眼或交叉眼那样显示开/关模式。 我的代码如下:
<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>
我得到了上述问题的答案,请按照下面的流程进行操作,您将获得on/off显示密码切换按钮的状态。
使用 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
完成此过程后,您将能够获得 on/off 状态。它可能对其他人有帮助。