分享

Android开发笔记(十一) — 设置EditText的显示方式

 openwudi 2010-10-08

通常使用EditText来作为密码输入方式,但是由于布局文件中把EditText的显示方式设置成 android:password=”true” 这样所有输入字符将变成●●●●●●的形式显示。这样虽然可以增强保密效果,但是如果想查看输入的字符怎么办?这里我们需要加一个CheckBox来控制是否显示密码。
设计思路:
1、布局加入一个EditText,把属性设置成 android:password=”true” 。
2、布局加入一个CheckBox,把属性设置成 android:checked=”false” 。
3、在程序中把CheckBox使用setOnCheckedChangeListener()设置监听器,OnCheckedChangeListener()监听器需要重写onCheckedChanged方法。判断如果选中就显示字符,不选中就使用●代替字符。

程序代码:

Java语言: EX03_22.java
01 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.xml
01 <?xml version="1.0" encoding=”utf-8″?>
02 <RelativeLayout
03 android:id="@+id/main"
04 android:background="#ffffffff"
05 android:layout_width="fill_parent"
06 android:layout_height="fill_parent"
07 xmlns:android="http://schemas./apk/res/android"
08 >
09 <CheckBox
10 android:id="@+id/select"
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 </CheckBox>
19 <EditText
20 android:id="@+id/psw"
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 </EditText>
29 <TextView
30 android:id="@+id/text"
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 </TextView>
39 </RelativeLayout>

 

运行效果:

 

图片
图片

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约