Android实现动态显示或隐藏密码输入框的内容

本文实例展示了Android实现动态显示或隐藏密码输入框内容的方法,分享给大家供大家参考之用。具体方法如下:

该功能可通过设置EditText的setTransformationMethod()方法来实现隐藏密码或者显示密码。

  1. 示例代码如下:
private ImageView iv_pwd;
private EditText et_password;
private boolean mbDisplayFlg = false;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(activity_main_login);

  et_password = (EditText)findViewById(R.id.et_password_login);
  iv_pwd = (ImageView) findViewById(R.id.iv_pwd);
  iv_pwd .setOnClickListener(new OnClickListener() {

 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 Log.d("AndroidTest", "mbDisplayFlg = " + mbDisplayFlg);
 if (!mbDisplayFlg) {
  // display password text, for example "123456"
   iv_pwd.setImageResource(R.drawable.icon_show_pw);//设置显示密码的图片
   et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
   et_password.setSelection(et_password.getText().toString().length()); //调整光标的位置到最后                                                            
 } else {
  // hide password, display "."
  iv_pwd.setImageResource(R.drawable.icon_hide_pw);//设置隐藏密码的图片
  et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
  et_password.setSelection(et_password.getText().toString().length());
 }
  mbDisplayFlg = !mbDisplayFlg;
  mEtPassword.postInvalidate();
 }
  });
}
 2. xml文件
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">

    <LinearLayout
        style="@style/TitleLinearlayoutStyle">

        <ImageView
            android:id="@+id/iv_login_forward"
            android:clickable="true"
            android:focusable="true"
            style="@style/TitleImageviewStyle"/>
        <TextView
            style="@style/TitleTextviewStyle"
            android:text="登录" />


        <TextView
            android:id="@+id/tv_register_login"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:gravity="center_vertical"
            android:onClick="tv_register_login"
            android:clickable="true"
            android:text="注册"
            android:textColor="@color/white"
            android:textSize="@dimen/title_text_size" />
    </LinearLayout>
    <TextView style="@style/TextviewShadowStyle" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical">

        <EditText
            android:id="@+id/et_account_login"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:hint="手机/邮箱/用户名"
            android:gravity="center_vertical"
            android:paddingLeft="15dp"
            android:textColor="#000000"
            android:background="@null"
            android:textSize="@dimen/text_size_tip_default"/>

        <TextView style="@style/TextviewLineStyle" />
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:gravity="center_vertical">
            <EditText
                android:id="@+id/et_password_login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@null"
                android:hint="登录密码"
                android:gravity="center_vertical"
                android:inputType="textPassword"
                android:textColor="#000000"
                android:textSize="@dimen/text_size_tip_default" />
            <ImageView
                android:id="@+id/iv_pwd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:src="@drawable/icon_hide_pw"/>
        </RelativeLayout>


        <TextView style="@style/TextviewLineStyle" />

        <TextView style="@style/TextviewShadowStyle" />

        <Button
            android:id="@+id/btn_login"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:background="@drawable/btn_selector"
            android:text="登录"
            android:textColor="@color/white"
            android:textSize="@dimen/button_size" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:paddingTop="25dp">

            <TextView
                android:id="@+id/tv_forgetpas_login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="忘记密码?"
                android:textColor="@color/normal_orange" />

            <TextView
                android:id="@+id/tv_sms_login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="短信验证登录"
                android:textColor="@color/normal_orange" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/tv_third_login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="第三方登录"
                android:textColor="#000000"
                android:textSize="@dimen/text_size_tip_default"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#6a836d6d"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@id/tv_third_login"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#6a836d6d"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@id/tv_third_login"/>

        </RelativeLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingTop="10dp"
            android:layout_marginLeft="90dp"
            android:layout_marginRight="90dp"
            android:orientation="horizontal">
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="微信"
                android:textSize="@dimen/text_size_tip_default"
                android:gravity="center"
                android:drawableTop="@drawable/accounts_icon_weichat"/>
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="QQ"
                android:textSize="@dimen/text_size_tip_default"
                android:gravity="center"
                android:drawableTop="@drawable/accounts_icon_qq"
                />
            <TextView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="微博"
                android:textSize="@dimen/text_size_tip_default"
                android:gravity="center"
                android:drawableTop="@drawable/accounts_icon_weibo"
                />
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

3.效果图
显示效果
隐藏效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值