经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android开发EditText实现密码显示隐藏
来源:jb51  时间:2022/8/2 14:46:00  对本文有异议

最近在做一个登录、注册页面,里面需要显示或隐藏密码,故做了一个简单的显示和隐藏功能。

关键类TextView.setTransformationMethod(TransformationMethod method),其中TransformationMethod 有两个子类:

  • HideReturnsTransformationMethod 隐藏回车
  • PasswordTransformationMethod 密码类型

关键代码:

  1. @OnClick(R.id.iv_psw_eye)
  2. ? ? void clickPswEye() {
  3. ? ? ? ? int tag = Integer.parseInt(pswEyeIV.getTag().toString());
  4. ? ? ? ? if (tag == 1) {//显示密码
  5. ? ? ? ? ? ? pswEyeIV.setTag(2);
  6. ? ? ? ? ? ? pswEyeIV.setImageResource(R.mipmap.icon_psw_not_eye);
  7. ? ? ? ? ? ? passwordET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
  8. ? ? ? ? } else {//隐藏密码
  9. ? ? ? ? ? ? pswEyeIV.setTag(1);
  10. ? ? ? ? ? ? pswEyeIV.setImageResource(R.mipmap.icon_psw_eye);
  11. ? ? ? ? ? ? passwordET.setTransformationMethod(PasswordTransformationMethod.getInstance());
  12. ? ? ? ? }
  13. ? ? }

我用ImageView的Tag属性存储当前密码输入框的类型,1是密码类型,2是显示类型。布局组件关键代码如下:

  1. <RelativeLayout
  2. ? ? ? ? ? ? android:layout_width="match_parent"
  3. ? ? ? ? ? ? android:layout_height="45dp"
  4. ? ? ? ? ? ? android:layout_gravity="center"
  5. ? ? ? ? ? ? android:layout_marginTop="26dp"
  6. ? ? ? ? ? ? android:background="@drawable/bg_edit_login"
  7. ? ? ? ? ? ? android:paddingLeft="26dp">
  8.  
  9. ? ? ? ? ? ? <EditText
  10. ? ? ? ? ? ? ? ? android:id="@+id/password_ET"
  11. ? ? ? ? ? ? ? ? android:layout_width="match_parent"
  12. ? ? ? ? ? ? ? ? android:layout_height="match_parent"
  13. ? ? ? ? ? ? ? ? android:layout_toLeftOf="@+id/iv_psw_eye"
  14. ? ? ? ? ? ? ? ? android:background="@null"
  15. ? ? ? ? ? ? ? ? android:gravity="center_vertical"
  16. ? ? ? ? ? ? ? ? android:hint="请输入密码"
  17. ? ? ? ? ? ? ? ? android:inputType="textPassword"
  18. ? ? ? ? ? ? ? ? android:maxLines="1"
  19. ? ? ? ? ? ? ? ? android:textColor="@color/color_666666"
  20. ? ? ? ? ? ? ? ? android:textColorHint="@color/color_999999"
  21. ? ? ? ? ? ? ? ? android:textSize="13sp" />
  22.  
  23. ? ? ? ? ? ? <ImageView
  24. ? ? ? ? ? ? ? ? android:id="@+id/iv_psw_eye"
  25. ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
  26. ? ? ? ? ? ? ? ? android:layout_height="match_parent"
  27. ? ? ? ? ? ? ? ? android:layout_alignParentRight="true"
  28. ? ? ? ? ? ? ? ? android:paddingLeft="20dp"
  29. ? ? ? ? ? ? ? ? android:paddingRight="26dp"
  30. ? ? ? ? ? ? ? ? android:src="@mipmap/icon_psw_eye"
  31. ? ? ? ? ? ? ? ? android:tag="1" />
  32. </RelativeLayout>

截图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号