广告
返回顶部
首页 > 资讯 > 移动开发 >Android开发EditText实现密码显示隐藏
  • 961
分享到

Android开发EditText实现密码显示隐藏

摘要

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

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

关键类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>

截图:

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

--结束END--

本文标题: Android开发EditText实现密码显示隐藏

本文链接: https://www.lsjlt.com/news/171503.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作