Android中EditText显示明文与密文的两种方式
版权声明:本文为HaiyuKing原创文章,转载请注明出处!
前言
记录输入框显示、隐藏密码的简单布局以及实现方式。
效果图

代码分析
方式一
/**方式一:*/
private void showOrHiddenPwd(){
if(! showPwd){
showPwd = true;
img_pwdshow.setImageResource(R.drawable.pwd_show);
//显示密码
edt_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else{
showPwd = false;
img_pwdshow.setImageResource(R.drawable.pwd_hidden);
//隐藏密码
edt_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
方式二
/**方式二:*/
private void showOrHiddenPwdWithInputType(){
if(! showPwd){
showPwd = true;
img_pwdshow.setImageResource(R.drawable.pwd_show);
//显示密码
edt_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
}else{
showPwd = false;
img_pwdshow.setImageResource(R.drawable.pwd_hidden);
//隐藏密码
edt_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
}
使用步骤
一、项目组织结构图


注意事项:
1、 导入类文件后需要change包名以及重新import R文件路径
2、 Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖
二、导入步骤
将comm_input_selector.xml复制到项目中【本来是想作为输入框的背景框,就会实现输入框获取焦点的时候边框变色,但是现在作为LinearLayout的背景,可以实现聚焦的时候变色的效果】
<?xml version="1.0" encoding="utf-8"?>
<!-- 输入框背景图 -->
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!--选中时效果-->
<!-- <item android:state_focused="true"
android:drawable="@drawable/input_box_focused" /> -->
<!--disable效果 windows焦点在前时 注意写法-->
<!-- <item android:state_window_focused="true" android:state_enabled="false"
android:drawable="@drawable/input_box_focused" /> -->
<!--默认时效果-->
<!-- <item android:drawable="@drawable/input_box_unfocused" /> --> <!-- 选中时效果 -->
<item android:state_focused="true"> <shape android:shape="rectangle">
<!-- 圆角 -->
<corners android:radius="3dp" />
<!-- 描边 -->
<stroke android:width="1dp" android:color="#1296db" />
<!-- 内边距 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<!-- 填充 -->
<solid android:color="@android:color/transparent" />
</shape>
</item>
<!-- disable效果 windows焦点在前时 注意写法 -->
<item android:state_enabled="false" android:state_window_focused="true"> <shape android:shape="rectangle">
<!-- 圆角 -->
<corners android:radius="3dp" />
<!-- 描边 -->
<stroke android:width="1dp" android:color="#1296db" />
<!-- 内边距 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<!-- 填充 -->
<solid android:color="@android:color/transparent" />
</shape> </item>
<!-- 默认时效果 -->
<item>
<shape android:shape="rectangle">
<!-- 圆角 -->
<corners android:radius="3dp" />
<!-- 描边 -->
<stroke android:width="1dp" android:color="#9f9f9f" />
<!-- 内边距 -->
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<!-- 填充 -->
<solid android:color="@android:color/transparent" />
</shape>
</item> </selector>
将图片资源复制到项目中【根据实际情况变更】




三、使用方法
布局文件
<?xml version="1.0" encoding="utf-8"?>
<!-- Android中EditText显示明文与密文的两种方式 -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.why.project.androidcnblogsdemo.activity.MainActivity"> <!-- 当之有一个EditText或者AutoCompleteTextView的时候,进入画面时是默认得到焦点的。 要想去除焦点,可以在auto之前加一个0像素的layout,并设置他先得到焦点。 -->
<LinearLayout
android:layout_width="0px"
android:layout_height="0px"
android:focusable="true"
android:focusableInTouchMode="true"/> <!-- 输入密码区域 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:background="@drawable/comm_input_selector"
android:focusable="true"
android:clickable="true"
android:addStatesFromChildren="true"
android:layout_margin="5dp"> <EditText
android:id="@+id/edt_password"
android:layout_width="0.0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@null"
android:hint="请输入密码"
android:textSize="18sp"
android:imeOptions="actionGo"
android:inputType="textPassword"
android:text=""
android:textColor="#000000"
android:textColorHint="#8c8c8c"
android:drawableLeft="@drawable/pwd_img"
android:drawablePadding="10dp"
/> <!-- 显示隐藏密码图标 -->
<ImageView
android:id="@+id/img_pwdshow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/pwd_hidden"
android:scaleType="fitCenter"
android:contentDescription="控制密码明文密文显示"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:focusable="true"
android:clickable="true"/>
</LinearLayout> </RelativeLayout>
Activity中使用
package com.why.project.androidcnblogsdemo.activity; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView; import com.why.project.androidcnblogsdemo.R; /**
* Android中EditText显示明文与密文的两种方式*/
public class MainActivity extends AppCompatActivity { private EditText edt_password;
private ImageView img_pwdshow;
private boolean showPwd = false;//默认不显示密码 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initViews();
initEvents();
} private void initViews() {
edt_password = (EditText) findViewById(R.id.edt_password);
img_pwdshow = (ImageView) findViewById(R.id.img_pwdshow);
} private void initEvents() {
img_pwdshow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showOrHiddenPwdWithInputType();
}
});
} /**方式一:*/
private void showOrHiddenPwd(){
if(! showPwd){
showPwd = true;
img_pwdshow.setImageResource(R.drawable.pwd_show);
//显示密码
edt_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else{
showPwd = false;
img_pwdshow.setImageResource(R.drawable.pwd_hidden);
//隐藏密码
edt_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
} /**方式二:*/
private void showOrHiddenPwdWithInputType(){
if(! showPwd){
showPwd = true;
img_pwdshow.setImageResource(R.drawable.pwd_show);
//显示密码
edt_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
}else{
showPwd = false;
img_pwdshow.setImageResource(R.drawable.pwd_hidden);
//隐藏密码
edt_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
}
}
混淆配置
无
参考资料
项目demo下载地址
暂时空缺
Android中EditText显示明文与密文的两种方式的更多相关文章
- Android中EditText显示明文与密码的两种方式
效果图如下所述: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...
- Android手机上监听短信的两种方式
Android手机上监听短信有两种方式: 1. 接受系统的短信广播,操作短信内容. 优点:操作方便,适合简单的短信应用. 缺点:来信会在状态栏显示通知信息. AndroidManifest.xml: ...
- 在eclipse中使用Maven建web工程的两种方式
Eclipse版本:Neon Release (4.6.0) Maven版本:3.3.9 第一种方式: 右键新建maven工程,勾选创建一个简单工程 填入信息,注意打包方式要改为war 点击完成,创建 ...
- Android画图之抗锯齿 paint 和 Canvas 两种方式
在画图的时候,图片如果旋转或缩放之后,总是会出现那些华丽的锯齿.其实Android自带了解决方式. 方法一:给Paint加上抗锯齿标志.然后将Paint对象作为参数传给canvas的绘制方法. ...
- Android中使用Gson解析JSON数据的两种方法
Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率;本文将介绍两种方法解析JSON数据,需要的朋友可以参考下 Json是一种类似于XML的通用数据交换格式,具有比XML更高的 ...
- js中构造函数的原型添加成员的两种方式
首先,js中给原型对象添加属性和方法. 方式一:对象的动态特效 给原型对象添加成员 语法:构造函数.prototype.方法名=function (){ } 方式二:替换原型对象(不是覆盖,而是替换, ...
- 安卓中使用OkHttp发送数据请求的两种方式(同、异步的GET、POST) 示例-- Android基础
1.首先看一下最终效果的截图,看看是不是你想要的,这个年代大家都很忙,开门见山很重要! 简要说下,点击不同按钮可以实现通过不同的方式发送OkHttp请求,并返回数据,这里请求的是网页,所以返回的都是些 ...
- 【Android】创建Popwindow弹出菜单的两种方式
方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...
- Android为TV端助力 切换fragment的两种方式
使用add方法切换时:载入Fragment1Fragment1 onCreateFragment1 onCreateViewFragment1 onStartFragment1 onResume用以下 ...
随机推荐
- Asp.Net WebAPI中Filter过滤器的使用以及执行顺序
转发自:http://www.cnblogs.com/UliiAn/p/5402146.html 在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行 ...
- 【游戏开发】在Lua中实现面向对象特性——模拟类、继承、多态
一.简介 Lua是一门非常强大.非常灵活的脚本语言,自它从发明以来,无数的游戏使用了Lua作为开发语言.但是作为一款脚本语言,Lua也有着自己的不足,那就是它本身并没有提供面向对象的特性,而游戏开发是 ...
- kmspico_setup.exe运行提示系统资源不足,无法完成请求的服务
在使用KMSpico激活office时,windows下运行exe会提示系统资源不足,无法完成请求的服务. 我的解决方法是:卸载电脑上的wps...
- hystrix隔离策略(4)
hystrix提供了两种隔离策略:线程池隔离和信号量隔离.hystrix默认采用线程池隔离. 1.线程池隔离 不同服务通过使用不同线程池,彼此间将不受影响,达到隔离效果. 例如: 我们可以通过andT ...
- JAVA基础第二章-java三大特性:封装、继承、多态
业内经常说的一句话是不要重复造轮子,但是有时候,只有自己造一个轮子了,才会深刻明白什么样的轮子适合山路,什么样的轮子适合平地! 我将会持续更新java基础知识,欢迎关注. 往期章节: JAVA基础第一 ...
- Linux 系统目录结构说明
在刚开始接触Linux系统时,对其目录结构迷茫的很,尤其是很少使用或者刚开始接触Linux系统的同学:我也是最近项目需要开始上手,所以查看了些资料,特整理出来供大家互相学习: 1.目录结构总揽 以下是 ...
- SpringBoot自动配置原理
前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 回顾前面Spring的文章(以学习的顺序排好): S ...
- 《k8s-1.13版本源码分析》-测试环境搭建(k8s-1.13版本单节点环境搭建)
本文原始地址(gitbook格式):https://farmer-hutao.github.io/k8s-source-code-analysis/prepare/debug-environment. ...
- LeetCode重建二叉树系列问题总结
二叉树天然的递归特性,使得我们可以使用递归算法对二叉树进行遍历和重建.之前已经写过LeetCode二叉树的前序.中序.后序遍历(递归实现),那么本文将进行二叉树的重建,经过对比,会发现二者有着许多相似 ...
- [转]MS Excel VBO option missing in Blue Prism
本文转自:https://stackoverflow.com/questions/48706743/ms-excel-vbo-option-missing-in-blue-prism 问: I am ...