致青春图片剧照:Android开发笔记(十一1) — 设置EditText的显示方式
来源:百度文库 编辑:九乡新闻网 时间:2024/10/06 15:07:29
通常使用EditText来作为密码输入方式,但是由于布局文件中把EditText的显示方式设置成 android:password=”true” 这样所有输入字符将变成●●●●●●的形式显示。这样虽然可以增强保密效果,但是如果想查看输入的字符怎么办?这里我们需要加一个CheckBox来控制是否显示密码。
设计思路:
1、布局加入一个EditText,把属性设置成 android:password=”true” 。
2、布局加入一个CheckBox,把属性设置成 android:checked=”false” 。
3、在程序中把CheckBox使用setOnCheckedChangeListener()设置监听器,OnCheckedChangeListener()监听器需要重写onCheckedChanged方法。判断如果选中就显示字符,不选中就使用●代替字符。
程序代码:
Java语言: EX03_22.java01 package dan.ex03_22;02
03 import android.app.Activity;
04 import android.os.Bundle;
05 import android.text.method.HideReturnsTransformationMethod;
06 import android.text.method.PasswordTransformationMethod;
07 import android.widget.CheckBox;
08 import android.widget.CompoundButton;
09 import android.widget.EditText;
10 import android.widget.TextView;
11
12 public class EX03_22 extends Activity {
13 /** Called when the activity is first created. */
14 EditText psw;
15 CheckBox select;
16
17 @Override
18 public void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.main);
21 psw = (EditText) findViewById(R.id.psw);
22 select = (CheckBox) findViewById(R.id.select);
23
24 select.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
25
26 @Override
27 public void onCheckedChanged(CompoundButton buttonView,
28 boolean isChecked) {
29 if (isChecked) {
30 psw.setTransformationMethod(HideReturnsTransformationMethod
31 .getInstance());
32 } else {
33 psw.setTransformationMethod(PasswordTransformationMethod
34 .getInstance());
35 }
36 }
37
38 });
39 }
40 }
布局代码:
XML语言: main.xml0102
04 android:background="#ffffffff"
05 android:layout_width="fill_parent"
06 android:layout_height="fill_parent"
07 xmlns:android="http://schemas.android.com/apk/res/android"
08 >
09
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
13 android:text="显示密码"
14 android:textColor="#ff000000"
15 android:layout_below="@+id/psw"
16 android:layout_alignParentLeft="true"
17 >
18
19
21 android:layout_width="320px"
22 android:layout_height="wrap_content"
23 android:textSize="18sp"
24 android:password="true"
25 android:layout_below="@+id/text"
26 android:layout_alignParentLeft="true"
27 >
28
29
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:text="请输入密码:"
34 android:textColor="#ff333333"
35 android:layout_alignParentTop="true"
36 android:layout_alignParentLeft="true"
37 >
38
39
运行效果:
Android开发笔记(十一1) — 设置EditText的显示方式
android中EditText文本监听的方法:addTextChangedListene...
Android开发的技术层次
Android开发的技术层次
android开发
千万富翁的思维方式笔记1
M9适合Android开发用机吗? - Android开发进阶交流 - 做最专业的安卓开发者论坛 Android开发网 Android开发教程 Android开发视频教程 Android开发学习资料 -
Android开发中应该注意的细节
Android 的系统属性(SystemProperties)设置分析
Android圆形进度条颜色的设置
学习笔记——表格设置(引自学习表格设置做图框 - 理睬的日志 - 网易博客)
adb工具的使用 - Android开发进阶交流 - eoe·Android开发社区 |最棒Android开发|最好Andriod社区|Android论坛 - Powered by Discuz!
Android应用开发笔记(10):制作自定义背景Button按钮、自定义形状Button...
千万富翁的思维方式笔记
android jni 学习笔记
搭建Android开发环境
Android开发资源
android开发--技术积累
Android开发之旅
Android开发之
Android 开发简介
android小设置
【Android手机上网设置】
开发必读:如何成为一名优秀的Android开发者