本文实例展示了android实现动态显示或隐藏密码输入框内容的方法,分享给大家供大家参考之用。具体方法如下:
该功能可通过设置edittext的settransformationmethod()方法来实现隐藏密码或者显示密码。
示例代码如下:
private button mbtnpassword;
private edittext metpassword;
private boolean mbdisplayflg = false;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
metpassword = (edittext)findviewbyid(r.id.password);
mbtnpassword = (button)findviewbyid(r.id.btnpassword);
mbtnpassword.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// todo auto-generated method stub
log.d("androidtest", "mbdisplayflg = " + mbdisplayflg);
if (!mbdisplayflg) {
// display password text, for example "123456"
metpassword.settransformationmethod(hidereturnstransformationmethod.getinstance());
} else {
// hide password, display "."
metpassword.settransformationmethod(passwordtransformationmethod.getinstance());
}
mbdisplayflg = !mbdisplayflg;
metpassword.postinvalidate();
}
});
}
main.xml文件如下:
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
android:textsize="18sp"
android:text="123456">
希望本文所述对大家的android程序设计有所帮助。