现在的位置: 首页 > 综合 > 正文

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

2012年08月14日 ⁄ 综合 ⁄ 共 1103字 ⁄ 字号 评论关闭

1. 范例说明

 

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

 

2. 运行结果

3. 编写代码

 

	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());
				}
			}
		});
	}

 

4. 扩展学习与作业

 

1.EditText的Xml属性

http://www.eyeandroid.com/thread-9999-1-1.html

2.Android开发之EditText属性详解

http://www.eyeandroid.com/thread-8233-1-1.html

作业:用textColorHint来改变EditText中提示信息的颜色

 

5.视频讲解

http://www.eyeandroid.com/thread-10025-1-1.html

抱歉!评论已关闭.