EditText 显示明文和密码

来源:互联网 发布:外星人笔记windows 编辑:程序博客网 时间:2024/05/20 04:30

1、效果图

 

2、布局文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 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="wifi.app.wei.com.myapplication.MainActivity">
 
 
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        >
 
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/eye_close"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            />
 
        <EditText
            android:id="@+id/pass_et"
            android:layout_toLeftOf="@id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            />
 
    </RelativeLayout>
 
 
</RelativeLayout>

  

3、代码实现(方式一)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package wifi.app.wei.com.myapplication;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
 
public class MainActivity extends AppCompatActivity {
 
    private ImageView imageView ;
    private EditText editText ;
    boolean eyeOpen = false ;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main );
        imageView = (ImageView) findViewById( R.id.image) ;
        editText = (EditText) findViewById( R.id.pass_et);
 
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ( eyeOpen ){
                    //密码 TYPE_CLASS_TEXT 和 TYPE_TEXT_VARIATION_PASSWORD 必须一起使用
                    editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    imageView.setImageResource( R.mipmap.eye_close );
                    eyeOpen = false ;
                }else {
                    //明文
                    editText.setInputType( InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD );
                    imageView.setImageResource( R.mipmap.eye_open );
                    eyeOpen = true ;
                }
            }
        });
    }
}

  

 4、代码实现(方式二)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package wifi.app.wei.com.myapplication;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
 
public class MainActivity extends AppCompatActivity {
 
    private ImageView imageView ;
    private EditText editText ;
    boolean eyeOpen = false ;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main );
        imageView = (ImageView) findViewById( R.id.image) ;
        editText = (EditText) findViewById( R.id.pass_et);
 
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ( eyeOpen ){
                    //密码
                    editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    imageView.setImageResource( R.mipmap.eye_close );
                    eyeOpen = false ;
                }else {
                    //明文
                    editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    imageView.setImageResource( R.mipmap.eye_open );
                    eyeOpen = true ;
                }
            }
        });
    }
}
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 洛阳信息港偃师户外 骆文博整容 落到异界当魔王 落跑甜心快乐大本营120714 落日故园情 落水狗qvod 漯河5月27日军嫂事件 漯河高中办公系统 漯河军嫂 漯河军嫂事件 驴拄雁 吕程的修真秘籍 吕冠成 白陆和彦晞 吕令子父母 吕令子微博 吕明奥数 吕婉柔微博 吕锡文的父亲是谁 吕锡文是吕枫的女儿 吕仙枕黄梁未熟 旅鼠之谜的启示 旅鼠之谜点击 旅鼠之谜阅读答案 律师情人电视剧 律政强人百度云 绿灯侠qvod 绿灯侠百度影音 绿灯侠快播 绿豆蛙打酱油 绿豆蛙给生活加点料 绿箭侠更新时间 绿箭侠什么时候更新 绿巨人2008免cd补丁 绿芜为什么自杀 绿原qq一卡通 绿原一卡通 绿源粉粉香 绿之韵网上商城 妈妈别哭百度影音 妈妈的花样年华插曲 妈妈是小学四年生粤语