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

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

  1. 示例代码如下:
private ImageView iv_pwd;private EditText et_password;private boolean mbDisplayFlg = false;/** Called when the activity is first created. */@Overridepublic 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.效果图
Android实现动态显示或隐藏密码输入框的内容_第1张图片
Android实现动态显示或隐藏密码输入框的内容_第2张图片

更多相关文章

  1. android图片浏览器 PhotoStore
  2. Android 图片裁剪之三剑式(二)
  3. android 选择本地图片并截剪图片保存到,sdcard指定目录中
  4. Android使用文件管理器打开指定文件夹,浏览里面的内容
  5. Android的Btimap处理大图片解决方法
  6. 如何实现手势缩放图片
  7. 直播软件源码Android 多个ImageView拖拽互换图片
  8. 【Android 界面效果6】Android 控件之ImageSwitcher图片切换器

随机推荐

  1. 【Android】SQLite数据库基本用法详解(极
  2. Android——启动过程详解
  3. (五)Android线程及其消息机制
  4. Android(安卓)下使用 JSON 实现 HTTP 请
  5. Android(安卓)Task
  6. Android(安卓)系统HAL 简介
  7. Android自定义View(1):对话框-Dialog
  8. Android(安卓)widget之ListView
  9. Android样式的开发:shape篇
  10. logcat 总是报: W/StaticLayout: maxLineH