本文实例展示了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.效果图

更多相关文章

  1. ListView的使用2
  2. Android界面布局基本属性
  3. [RK3399][Android7.1] 调试笔记 --- 设置里打开返回按键
  4. Android(安卓)ApiDemos示例解析(15):App->Activity->SetWallpaper
  5. 如何隐藏滚动条在ScrollView中
  6. Android利用setLayoutParams在代码中调整布局(Margin和居中)
  7. Android布局_View属性
  8. android 关于程序升级问题
  9. EditText 的常用属性与解释

随机推荐

  1. 图解垃圾算法,No,捡垃圾算法
  2. 如何在Mac上更新Chrome?
  3. 我们不一样!运维自动化典型案例观止
  4. Redis6 终于还是迎来了多线程版本!
  5. 我不配有五一
  6. 喂,业余草吗?你的生死簿管理系统我出10万买
  7. 基于 Python 的企业级运维平台开发实战!
  8. 又一个程序员“倒”下,Pandownload作者被
  9. Github 封禁了有伊朗外部贡献者的项目!
  10. 程序员又开始了疯狂的薅羊毛!