有没有一种聪明的方法让用户在 android EditText 中在隐藏和查看密码之间切换?
许多基于 PC 的应用程序允许用户执行此操作。
自支持库 v24.2.0 以来,这确实很容易实现。
你需要做的只是:
-
将设计库添加到您的依赖项中
dependencies {
implementation "com.google.android.material:material:1.9.0"
}
-
Use TextInputEditText
和这个结合TextInputLayout
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/etPasswordLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true"
android:layout_marginTop="@dimen/_5sdp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_ed_box"
android:hint="@string/enter_password"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
The passwordToggleEnabled
属性就可以完成这项工作!
-
在你的根布局中不要忘记添加 xmlns:app="http://schemas.android.com/apk/res-auto"
-
您可以使用以下方法自定义密码切换:
app:passwordToggleDrawable
- 可绘制用作密码输入可见性切换图标。
app:passwordToggleTint
- 用于密码输入可见性切换的图标。
app:passwordToggleTintMode
- 用于应用背景色调的混合模式。
更多详情请参阅TextInputLayout 文档.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)