动态显示和隐藏密码框_诗未冷的博客-程序员秘密

技术标签: EditText  Android基础  InputType  隐藏和显示密码框  

一、效果图如下


在开发的时候,有时需要可控的显示和隐藏输入框,下面是自己写的demo,贴出来共享

activity_update_password.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:orientation="vertical" >

    <include layout="@layout/titlebar_update_password" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="2dp" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="@string/Current_Password" />

            <TextView
                android:id="@+id/activity_update_show01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="5dp"
                android:clickable="true"
                android:onClick="showPassword"
                android:padding="5dp"
                android:text="@string/Show_Password"
                android:textColor="@drawable/read_more_btn"
                android:textSize="10sp" />
        </RelativeLayout>

        <EditText
            android:id="@+id/activity_update_currentPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@android:drawable/edit_text"
            android:hint="@string/Current_Password"
            android:singleLine="true"
            android:textColor="#000000"
            android:inputType="textPassword"
            android:textCursorDrawable="@null" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="@string/New_Password" />

            <TextView
                android:id="@+id/activity_update_show02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="5dp"
                android:clickable="true"
                android:onClick="showPassword"
                android:padding="5dp"
                android:text="@string/Show_Password"
                android:textColor="@drawable/read_more_btn"
                android:textSize="10sp" />
        </RelativeLayout>

        <EditText
            android:id="@+id/activity_update_newPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@android:drawable/edit_text"
            android:hint="@string/New_Password"
            android:singleLine="true"
            android:textColor="#000000"
             android:inputType="textPassword"
            android:textCursorDrawable="@null" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="@string/confirm_password" />

            <TextView
                android:id="@+id/activity_update_show03"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="5dp"
                android:clickable="true"
                android:onClick="showPassword"
                android:padding="5dp"
                android:text="@string/Show_Password"
                android:textColor="@drawable/read_more_btn"
                android:textSize="10sp" />
        </RelativeLayout>

        <EditText
            android:id="@+id/activity_update_confirmPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@android:drawable/edit_text"
            android:hint="@string/confirm_password"
            android:singleLine="true"
            android:textColor="#000000"
       <span style="color:#ff6666;">      android:inputType="textPassword"</span>
            android:textCursorDrawable="@null" />

        <Button
            android:id="@+id/activity_update_updatePassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/theme_btn"
            android:text="@string/Update_Password" />
    </LinearLayout>

</LinearLayout>
Update按钮点击效果

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/theme_blue_normal" android:state_enabled="true" android:state_window_focused="false"/>
    <item android:drawable="@drawable/theme_blue_normal" android:state_enabled="false"/>
    <item android:drawable="@drawable/theme_blue_press" android:state_pressed="true"/>
    <item android:drawable="@drawable/theme_blue_normal" android:state_focused="true"/>

</selector>

需要提供2张图片背景即可,即点击前后的背景图片

文字点击效果

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="#f00"/>
    <item android:state_focused="true" android:color="@color/activity_search_title_bg"/>
    <item android:color="@color/activity_search_title_bg"/>

</selector>
需要2种颜色,即点击前后的文字颜色

主类

package com.chinabuye.android.activity;

import http.HttpRestClient;
import http.JsonHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import tool.NewTokenCallBack;
import tool.TokenTools;
import tool.UserInfo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chinabuye.android.R;

public class UpdatePasswordActivity extends Activity implements OnClickListener {
	private EditText currentPassword;
	private EditText newPassword;
	private EditText confirmPassword;
	private ImageView back;
	private Button updateBtn;
	private AlertDialog dialog;
	<span style="color:#ff6666;">private boolean mbDisplayFlg01 = false; // 用于动态显示和隐藏密码
	private boolean mbDisplayFlg02 = false;
	private boolean mbDisplayFlg03 = false;</span>

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		manageActivity();
		setContentView(R.layout.activity_update_password);
		initView();
	}

	private void initView() {
		currentPassword = (EditText) this.findViewById(R.id.activity_update_currentPassword);
		newPassword = (EditText) this.findViewById(R.id.activity_update_newPassword);
		confirmPassword = (EditText) this.findViewById(R.id.activity_update_confirmPassword);
		updateBtn = (Button) this.findViewById(R.id.activity_update_updatePassword);
		updateBtn.setOnClickListener(this);
		back = (ImageView) this.findViewById(R.id.activity_update_back);
		back.setOnClickListener(this);
		createDialog();
	}

	private void manageActivity() {
		ActivityInstanceManager.getActivityInstanceManager().addActivity(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.activity_update_updatePassword:
			String currentPass = currentPassword.getText().toString().trim();
			String newPass = newPassword.getText().toString().trim();
			String confirmPass = confirmPassword.getText().toString().trim();
			if ("".equals(currentPass) || currentPass == null) {
				Toast.makeText(this, R.string.Current_password_cannot_be_empty, Toast.LENGTH_LONG).show();
				break;
			}
			if ("".equals(newPass) || newPass == null) {
				Toast.makeText(this, R.string.New_password_connot_be_empty, Toast.LENGTH_LONG).show();
				break;
			}
			if ("".equals(confirmPass) || confirmPass == null) {
				Toast.makeText(this, R.string.confirm_password_connot_be_empty, Toast.LENGTH_LONG).show();
				break;
			}
			if (!confirmPass.equals(newPass)) {
				Toast.makeText(this, R.string.The_password_donnot_match, Toast.LENGTH_LONG).show();
				break;
			}
			//判断输入的格式是否是邮箱格式
			if (currentPass.equals(newPass)) {
				Toast.makeText(this, R.string.The_same_password_enter_again, Toast.LENGTH_LONG).show();
				break;
			}
			updatePasswordMethod(currentPass, newPass);
			break;
		case R.id.activity_update_back:
			finish();
			break;

		default:
			break;
		}

	}

	public void showPassword(View view) {
		<span style="color:#ff6666;">switch (view.getId()) {
		case R.id.activity_update_show01:
			if (!mbDisplayFlg01) {
				// display password text, for example "123456",从隐藏到显示
				currentPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
			} else {
				// hide password, display ".",从显示到隐藏
				currentPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
			}
			mbDisplayFlg01 = !mbDisplayFlg01;
			currentPassword.postInvalidate();
			break;
		case R.id.activity_update_show02:
			if (!mbDisplayFlg02) {
				// display password text, for example "123456",从隐藏到显示
				newPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
			} else {
				// hide password, display ".",从显示到隐藏
				newPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
			}
			mbDisplayFlg02 = !mbDisplayFlg02;
			newPassword.postInvalidate();
			break;
		case R.id.activity_update_show03:
			if (!mbDisplayFlg03) {
				// display password text, for example "123456",从隐藏到显示
				confirmPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
			} else {
				// hide password, display ".",从显示到隐藏
				confirmPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
			}
			mbDisplayFlg03 = !mbDisplayFlg03;
			confirmPassword.postInvalidate();
			break;

		default:
			break;
		}</span>
	}

	private void updatePasswordMethod(final String currentPass, final String newPass) {
		TokenTools.requestNewToken(this, new NewTokenCallBack() {

			@Override
			public void getNewTokenSuccess(String newAccessToken) {
				if (newAccessToken != null) {
					String customerid = UserInfo.getUserInfoInstance().getUserid();
					if (customerid == null || "".equals(customerid)) {
						Toast.makeText(UpdatePasswordActivity.this, R.string.There_are_some_problems_with_customerid, Toast.LENGTH_LONG).show();
					} else {
						HttpRestClient.updatePassword(UpdatePasswordActivity.this, customerid, currentPass, newPass, newAccessToken, new JsonHttpResponseHandler() {
							@Override
							public void onSuccess(JSONObject response) {
								super.onSuccess(response);
								try {
									String result = response.getString("result");
									if ("true".equals(result)) {
										String desc = response.getString("desc");
										handler.sendEmptyMessage(1);
									} else {
										Toast.makeText(UpdatePasswordActivity.this, UpdatePasswordActivity.this.getResources().getString(R.string.Some_problems_have_arisen), Toast.LENGTH_LONG).show();
									}
								} catch (JSONException e) {
									e.printStackTrace();
								}
							}
						});
					}

				}
			}
		});
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				if (dialog != null)
					dialog.show();
				break;

			default:
				break;
			}

		};
	};

	private void createDialog() {
		dialog = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher).setCancelable(false).setTitle("Exit Dialog").setMessage(R.string.You_have_successfully_changed_the_password)
				.setPositiveButton("Exit", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// 将存储在本地的密码设置为空
						SharedPreferences sp = UpdatePasswordActivity.this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
						Editor editor = sp.edit();
						editor.putBoolean("isLogout", true);
						editor.putString("PASSWORD", null);
						editor.commit();
						Intent intent = new Intent(UpdatePasswordActivity.this, LoginActivity.class);
						startActivity(intent);
						finish();
					}
				}).create();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (dialog.isShowing()) {
				return true;
			} else {
				Intent intent = new Intent(UpdatePasswordActivity.this, HelpAboutActivity.class);
				intent.setAction("update_to_helpabout");
				startActivity(intent);
				finish();
			}
		}
		return super.onKeyDown(keyCode, event);
	}
}

主类是我从项目中抠出来的,关键部分用红色标记。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Z18789231876/article/details/40400149

智能推荐

目标检测标注工具labelImg使用方法_xunan003的博客-程序员秘密

目标检测中,原始图片的标注过程是非常重要的,它的作用是在原始图像中标注目标物体位置并对每张图片生成相应的xml文件表示目标标准框的位置。然而博主转载的文章http://blog.csdn.net/u014696921/article/details/53353896中提到的标注工具虽然使用简单,但是无法在同一张图片中标注多个同类目标;并且其标注完成后只能生成对应的txt文件,需要借助一定的工具才能

phpexcel导出emoji符号报错_为天空着色的博客-程序员秘密

原文:http://blog.csdn.net/SakuraLLj/article/details/72823765phpexcel导出用户数据成excel时,如果昵称包含表情,会导致数据不完整。过滤或替换emoji。因为emoji通过json_encode后,大部分都是u[ed] 开头的编码,所以先对昵称json_encode然后用正则过滤或替换成*,以下时相关代码:$value = json_...

matlab怎么算多元多项式,matlab - 操纵多元多项式的最佳方法Matlab - 堆栈内存溢出..._August丶Starten丶Sin的博客-程序员秘密

终于我找到了一种解决方案。 不幸的是(或没有),我使用了Symbolic Toolbox 。 就我而言,我考虑了7个符号变量,多项式的变量是X和Y matlab变量poly是一个包含字段p的struct 。 p是多项式的symfun矩阵。对于输出参数, C , E , MX和MY分别包含在polyOut.mult , polyOut.exph , polyOut.expoX和polyOut...

LinkedHashMap特性与HashMap排序_linkedhashmap 排序_lingchen336的博客-程序员秘密

HashMap:新key的加入或旧key的value的改变会刷新map中key的位置,变化的key总是存储在最前面。LinkedHashMap:有序的HashMap,key的改变不会影响key在map中的位置。如下一道DB面试题:/**@author [email protected] 2021/7/5数字去重:输入一组数字,按数字出现的次数排序,次数多的排在最前面,次数相同按出现顺序排序。使用LinkedHashMap 有序map, map排序输入123,56,123,89输出123,56,89

POJ 百炼 保研机试 2976:All in All_冰竹依梦的博客-程序员秘密

2976:All in All查看提交统计提示提问总时间限制: 1000ms 内存限制: 65536kB描述给定两个字符串s和t,请判断s是否是t的子序列。即从t中删除一些字符,将剩余的字符连接起来,即可获得s。输入包括若干个测试数据。每个测试数据由两个ASCII码的数字和字母串s和t组成,s和t的长度不超过100000。输出对每个测试数据,如果

vue导入静态js_vue引入静态js文件的方法_weixin_39786617的博客-程序员秘密

由于一些演示,需要对编码名称等可快速进行修改,需要页面方便配置。由于build后的vue项目基本已经看不出原样,因此需要创建一个文件,并在打包的时候不会进行编译。vue-cli 2.0的作法是在static文件下创建js。vue-cli 3.0 的写法则是直接在public文件夹下创建js、具体操作如下:1、在public文件夹下创建config.js文件,里面文件的语法是es5,不允许使用浏览器...

随便推点

zsh: command not found: pip_zhangt766的博客-程序员秘密

想给jupter notebook换个皮肤,于是在shell里面安装,就有了以下错误:zsh: command not found: pipzsh: command not found: conda之前bash pip和conda 都可以用,换成zsh了好多命令都用不了了,于是怀疑是zsh里没有环境变量,想到的解决方案是把 bash shell 中.bash_profile 全部环境变量加入zsh shell里。然而。。。open .zshrc既然没有,那就建一个吧,1.创建.

OpenCV图像处理算法——2(《Local Color Correction 》论文阅读及C++复现)_Delv_Peter的博客-程序员秘密

Local Color Correction(局部颜色校正)以下大部分引用自:https://www.yanxishe.com/columnDetail/16833前言这个是出自一篇论文里面的算法。论文地址为:http://www.ipol.im/pub/art/2011/gl_lcc/ 。IPOL 是一个非常好的学习数字图像处理的网站,上面的论文都是提供配套源码的,如果平时在数字图像处...

数据库(MySQL)面经汇总_数据库面经_青竹恶鬼的博客-程序员秘密

转载链接,方便复习,侵删!1.关系型和非关系型数据库的区别(各自优点)2.常用SQL语句(DDL,DML,DCL,TCL)3.数据库中join的类型与区别(inner join, outer join, cross join, natural join, self join),注意适用场景和sql语句的编写4. 数据库的索引类型5.聚集索引和非聚集索引的区别(叶节点存储内容)6.唯一性索引和主码索引的区别7.索引的优缺点,什么时候使用索引,什么时候不能使用索引(重点)8.索引的底层实现(B+

简述文本属性Attributes的用法_diuganqi7521的博客-程序员秘密

文本属性Attributes1.NSKernAttributeName: @10 调整字句 kerning 字句调整2.NSFontAttributeName : [UIFont systemFontOfSize:_fontSize] 设置字体3.NSForegroundColorAttributeName :[UIColor redColor] 设置文字颜色4.NS...

Non-local U-Nets_这就是算法的博客-程序员秘密

code tensorflowNon-local 由三个部分组成:U-Net、Residual Blocks、Global Aggregation BlockU-Net作者将原始u-net的concatenation -&gt; sum的原因:求和不会增加特征图的数量,因此会减少下一层中可训练参数的数量。具有求和的skip connection可以被视为远程残差连接,可以促进模型的...

U盘模式无法引导进入pe系统_weixin_30566111的博客-程序员秘密

有些笔记本、一体机 特别是win8、win10系统维护时需要 通过u盘进入pe系统,就是进不去,需要到bios中更改一下设置。 1.首先我们将已经使用u启动u盘启动盘制作工具制作好的启动u盘插入到电脑usb插口(建议直接将u盘插入USB插口处,因为那样传输的性能比前置的要直接),然后开启电脑! 2.开启电脑后当看到开机画面的时候,连续按下键盘上...

推荐文章

热门文章

相关标签