博客
关于我
Android EditText密码框的显示和隐藏
阅读量:707 次
发布时间:2019-03-21

本文共 2149 字,大约阅读时间需要 7 分钟。

Android Password EditText显示和隐藏密码的实现方法

在Android开发中,当需要设置密码输入框为显示和隐藏模式时,可以通过以下方法实现。首先需要在 XML 资源文件中定义EditText控件,将其设置为密码类型,确保不显示实际输入的文本。需要注意的是,EditText默认的TransformationMethod是PasswordTransformationMethod,这会使密码在输入时显示为星号或圆点。

1. 隐藏和显示密码框的切换逻辑

为了实现密码框显示和隐藏切换功能,可以在EditText的父容器中添加一个CompoundButton,用于控制TransformationMethod的切换。以下是实现步骤:

// 1. 在布局文件中定义CompoundButton和EditText CompoundButton checkBox = (CompoundButton) findViewById(R.id.password_show_hide); EditText passwordEditText = (EditText) findViewById(R.id.password_edit);
  1. 实现CompoundButton的点击事件listener,切换TransformationMethod:
  2. checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {    @Override    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {        // 根据isChecked判断是否显示或隐藏密码        if (isChecked) {            // 显示密码            passwordEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());        } else {            // 隐藏密码            passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());        }    }});

    2. XML设置

    在Android XML布局文件中,通过设置android:inputType="textPassword"可以确保 EditText默认显示为密码类型,这样可以显示星号或圆点而非真实字符。同时,为了避免EditText占据焦点,可以通过设置android:focusable="false"来实现,这样可以防止其他控件的一些交互问题。

    3. 自定义样例代码

    以下是一个完整的实现代码示例,供参考:

    // 在Activity中设置TransformationMethodboolean isChecked = false;CheckBox displayCheckbox = new CheckBox(this);displayCheckbox.setText("显示密码");displayCheckbox.setId(R.id.display_password_checkbox);displayCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {    @Override    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {        if (isChecked) {            // 显示密码            passwordEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());        } else {            // 隐藏密码            passwordEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());        }    }});

    4. 注意事项

  3. 需要确保HideReturnsTransformationMethod和PasswordTransformationMethod已经被正确导入,或者正常在Android的包名中找到。
  4. 如果需要更复杂的密码显示效果,可以通过自定义TransformationMethod来实现。
  5. 对于更复杂的布局问题,可以通过设置style来调整EditText的外观。
  6. 通过以上方法,可以轻松实现Android EditText的显示和隐藏密码功能。

转载地址:http://ylgez.baihongyu.com/

你可能感兴趣的文章
python 利用已有Ner模型进行数据清洗合并
查看>>
python 到大数据开发工程师_如何成为一个大数据开发工程师?
查看>>
python 加密解密(base64, AES)
查看>>
Python 和Java 哪个更适合做自动化测试?
查看>>
python 图片转ico
查看>>
python 图片转文字、语音转文字、文字转语音保存音频并朗读
查看>>
python 在包含类似字符\x16、\x12、\x某某的数组中将以\x开头的字符找出来的方法
查看>>
Python 在并行进程之间共享字典
查看>>
Python 垃圾收集器文档
查看>>
python 基于 wordcloud + jieba + matplotlib 生成词云
查看>>
python 基于detectron或mask_rcnn的mask遮罩区域进行图片截取
查看>>
Python 基础 - Day 5 Learning Note - 模块 之 标准库:time (1)
查看>>
python 基础第七篇
查看>>
Python编程:Tkinter图形界面设计(1)
查看>>
Python 基础语法:None
查看>>
Python 基础语法:基本数据类型(一)
查看>>
python编程读取写入excel_Python读取txt内容写入xls格式excel中的方法
查看>>
Python 基础语法:基本数据类型(字典)
查看>>
Python 基础语法:基本数据类型(字符串)
查看>>
Python 基础语法:基本数据类型(集合)
查看>>