版权声明:本文为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);
}
}
}

混淆配置

参考资料

Android基础--登陆界面,密码的隐藏和显示

Android中EditText显示明文与密码的两种方式

项目demo下载地址

暂时空缺

Android中EditText显示明文与密文的两种方式的更多相关文章

  1. Android中EditText显示明文与密码的两种方式

    效果图如下所述: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:and ...

  2. Android手机上监听短信的两种方式

    Android手机上监听短信有两种方式: 1. 接受系统的短信广播,操作短信内容. 优点:操作方便,适合简单的短信应用. 缺点:来信会在状态栏显示通知信息. AndroidManifest.xml: ...

  3. 在eclipse中使用Maven建web工程的两种方式

    Eclipse版本:Neon Release (4.6.0) Maven版本:3.3.9 第一种方式: 右键新建maven工程,勾选创建一个简单工程 填入信息,注意打包方式要改为war 点击完成,创建 ...

  4. Android画图之抗锯齿 paint 和 Canvas 两种方式

    在画图的时候,图片如果旋转或缩放之后,总是会出现那些华丽的锯齿.其实Android自带了解决方式.    方法一:给Paint加上抗锯齿标志.然后将Paint对象作为参数传给canvas的绘制方法. ...

  5. Android中使用Gson解析JSON数据的两种方法

    Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率;本文将介绍两种方法解析JSON数据,需要的朋友可以参考下   Json是一种类似于XML的通用数据交换格式,具有比XML更高的 ...

  6. js中构造函数的原型添加成员的两种方式

    首先,js中给原型对象添加属性和方法. 方式一:对象的动态特效 给原型对象添加成员 语法:构造函数.prototype.方法名=function (){ } 方式二:替换原型对象(不是覆盖,而是替换, ...

  7. 安卓中使用OkHttp发送数据请求的两种方式(同、异步的GET、POST) 示例-- Android基础

    1.首先看一下最终效果的截图,看看是不是你想要的,这个年代大家都很忙,开门见山很重要! 简要说下,点击不同按钮可以实现通过不同的方式发送OkHttp请求,并返回数据,这里请求的是网页,所以返回的都是些 ...

  8. 【Android】创建Popwindow弹出菜单的两种方式

    方法一的Activity package com.app.test02; import android.app.Activity; import android.os.Bundle; import a ...

  9. Android为TV端助力 切换fragment的两种方式

    使用add方法切换时:载入Fragment1Fragment1 onCreateFragment1 onCreateViewFragment1 onStartFragment1 onResume用以下 ...

随机推荐

  1. 错误:编码GBK的不可映射字符

    当Java源代码中包含中文字符时,我们在用javac编译时会出现"错误:编码GBK的不可映射字符". 由于JDK是国际版的,我们在用javac编译时,编译程序首先会获得我们操作系统 ...

  2. windows server 2012 R2汉化 -- 玩转Microsoft Azure

    Microsoft Azure 试用版小试牛刀 首先需要申请一个账号获得试用权 我这里是1元免费试用, 进入后就可以创建自己的虚拟机及数据库 在这里先说创建的windows server 2012 R ...

  3. 深入理解Java:内省(Introspector)

    深入理解Java:内省(Introspector) 内省(Introspector) 是Java 语言对 JavaBean 类属性.事件的一种缺省处理方法. JavaBean是一种特殊的类,主要用于传 ...

  4. Spring Boot自动配置源码解析(基于Spring Boot 2.0.2.RELEASE)

    在Spring Boot官方介绍中,首一段话是这样的(如下图).我们可以大概了解到其所表达的含义:我们可以利用Spring Boot写很少的配置来创建一个非常方便的基于Spring整合第三方类库的单体 ...

  5. bzoj 4173 打表???

    没有任何思路,打表发现ans=phi(n)*phi(m)*n*m %%% popoqqq Orz 然而并没有看懂-- #include<cstdio> #include<cstrin ...

  6. bzoj 2588 树上主席树

    主席树上树,对于每个节点,继承其父亲的,最后跑f[x]+f[y]-f[lca]-f[fa[lca]] 去重竟然要减一,我竟然不知道?? #include<cstdio> #include& ...

  7. bzoj 4129 Haruna’s Breakfast 树上莫队

    按照dfs序分块,莫队乱搞 再套个权值分块 #include<cstdio> #include<iostream> #include<cstring> #inclu ...

  8. iOS之LLDB常用调试命令

    LLDB是个开源的内置于XCode的调试工具,这里来理一理常用用法.lldb对于命令的简称,是头部匹配方式,只要不混淆,你可以随意简称某个命令.结果为在xcode下验证所得,可能与其它平台有所误差. ...

  9. iOS 8 中如何集成 Touch ID 功能

    2013年9月,苹果为当时发布的最新iPhone产品配备了一系列硬件升级方案.在iPhone 5s当中,最具创新特性的机制无疑要数围绕Home按钮设计的超薄金属圈,也就是被称为Touch ID的指纹传 ...

  10. 解决:git push error: failed to push some refs to

    出现错误的原因是github中的README.md文件不在本地代码目录中. 也就是说我们需要先将远程代码库中的任何文件先pull到本地代码库中,才能push新的代码到github代码库中. 使用如下命 ...