【Android每日一讲】2012.11.07 打勾显示输入的密码 - EditText与setTransformationMethod

[b]1. 范例说明[/b]
利用EditText作为密码输入是个不错的方法,保密且无需担心被盗取,但“****”这样的符号,让人不知自己到底输入是否正确。此时若能贴心地提供“显示密码”的选项,就能让User看到自己刚才输入的密码,必要时再关闭“显示密码”。
此范例程序初探EditText与CheckBox这两个Widget,并以CheckBox.setOnCheckedChangedListener()来设置显示密码事件,最后通过isChecked()方法判断显示密码状态。
[b]2. 运行结果[/b]

[img]http://dl.iteye.com/upload/attachment/0076/2665/7d359781-0367-343a-8666-eb7f92e568b8.png[/img]

[b]3. 编写代码[/b]
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* 加载main.xml Layout */
setContentView(R.layout.main);
/* findViewById()取得对象 */
et = (EditText) findViewById(R.id.mPassword);
cb = (CheckBox) findViewById(R.id.mCheck);
/* 设定CheckBox的OnCheckedChangeListener */
cb.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
if (cb.isChecked()) {
/* 设定EditText的内容为可见的 */
et.setTransformationMethod(HideReturnsTransformationMethod
.getInstance());
} else {
/* 设定EditText的内容为隐藏的 */
et.setTransformationMethod(PasswordTransformationMethod
.getInstance());
}
}
});
}
[b]4. 扩展学习与作业[/b]
[b]1.EditText的Xml属性[/b]
[url]http://www.eyeandroid.com/thread-9999-1-1.html[/url]
[b]2.Android开发之EditText属性详解[/b]
[url]http://www.eyeandroid.com/thread-8233-1-1.html[/url]
作业:用textColorHint来改变EditText中提示信息的颜色
[b][size=large][color=red]5.视频讲解[/color][/size][/b]
[url]http://www.eyeandroid.com/thread-10025-1-1.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值