当前位置:操作系统 > 安卓/Android >>

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

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

3. 编写代码
[java] 
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. 扩展学习与作业
 

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,