Android开发EditText实现密码显示隐藏 最近在做一个登录.注册页面,里面需要显示或隐藏密码,故做了一个简单的显示和隐藏功能. 关键类TextView.setTransformationMethod(TransformationMethod method),其中TransformationMethod 有两个子类: HideReturnsTransformationMethod 隐藏回车
最近在做一个登录、注册页面,里面需要显示或隐藏密码,故做了一个简单的显示和隐藏功能。
关键类TextView.setTransformationMethod(TransformationMethod method),其中TransformationMethod 有两个子类:
- HideReturnsTransformationMethod 隐藏回车
- PasswordTransformationMethod 密码类型
关键代码:
@OnClick(R.id.iv_psw_eye)
void clickPswEye() {
int tag = Integer.parseInt(pswEyeIV.getTag().toString());
if (tag == 1) {//显示密码
pswEyeIV.setTag(2);
pswEyeIV.setImageResource(R.mipmap.icon_psw_not_eye);
passwordET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {//隐藏密码
pswEyeIV.setTag(1);
pswEyeIV.setImageResource(R.mipmap.icon_psw_eye);
passwordET.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
我用ImageView的Tag属性存储当前密码输入框的类型,1是密码类型,2是显示类型。布局组件关键代码如下:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_marginTop="26dp"
android:background="@drawable/bg_edit_login"
android:paddingLeft="26dp">
<EditText
android:id="@+id/password_ET"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/iv_psw_eye"
android:background="@null"
android:gravity="center_vertical"
android:hint="请输入密码"
android:inputType="textPassword"
android:maxLines="1"
android:textColor="@color/color_666666"
android:textColorHint="@color/color_999999"
android:textSize="13sp" />
<ImageView
android:id="@+id/iv_psw_eye"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:paddingLeft="20dp"
android:paddingRight="26dp"
android:src="@mipmap/icon_psw_eye"
android:tag="1" />
</RelativeLayout>
截图:


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
织梦狗教程
本文标题为:Android开发EditText实现密码显示隐藏
基础教程推荐
猜你喜欢
- IOS应用内跳转系统设置相关界面的方法 2022-11-20
- Android中的webview监听每次URL变化实例 2023-01-23
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
- android studio按钮监听的5种方法实例详解 2023-01-12
- Android多返回栈技术 2023-04-15
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- Flutter手势密码的实现示例(附demo) 2023-04-11
