Android实现动态显示或隐藏密码输入框的内容
来源:互联网 发布:女生大数据 编辑:程序博客网 时间:2024/06/05 19:36
本文实例展示了Android实现动态显示或隐藏密码输入框内容的方法,分享给大家供大家参考之用。具体方法如下:
该功能可通过设置EditText的setTransformationMethod()方法来实现隐藏密码或者显示密码。
- 示例代码如下:
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.效果图
0 0
- Android实现动态显示或隐藏密码输入框的内容
- 【Android】实现动态显示隐藏密码输入框的内容
- 【Android】实现动态显示隐藏密码输入框的内容
- Android动态显示隐藏密码输入框的内容
- Android动态显示隐藏密码输入框的内容
- Android动态显示隐藏密码输入框的内容
- Android动态显示隐藏密码输入框的内容
- android之EditText实现动态显示隐藏密码输入框的内容(实用)
- Android 动态显示隐藏EditText 输入框内容
- 动态修改EditText输入键盘并隐藏或显示密码
- VC实现文本输入框密码的显示与隐藏
- Android打造带删除的EditText并且实现输入框密码显示、隐藏
- 输入密码或金额的显示和隐藏
- 输入密码或金额的显示和隐藏(续)
- EditText输入密码隐藏或显示文本
- Android UI设计 文本输入框EditText用法(输入密码 隐藏显示输入的字符串)
- 登录界面密码输入框控制显示与隐藏(登录一般所需要的内容)
- android 实现密码的显示和隐藏
- 常见文件操作代码(文件管理软件)
- USB的内核线程 与 完成量之间的关系 赏析
- Android Studio 使用Lambda表达式
- 数据结构课设 家谱处理 (map)
- Handler机制
- Android实现动态显示或隐藏密码输入框的内容
- JDK安装与环境变量的配置
- WebSocket效果真不错
- K-means聚类算法
- windows上安装linux系统,黑屏进不到桌面解决办法
- 贪心1016
- 理解sparse coding
- JavaScript Source Map
- 南阳题目62-笨小熊