Android 密码 显示与隐藏功能实例
程序员文章站
2023-12-03 12:34:22
效果:
效果:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <edittext android:id="@+id/edittext1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputtype="textpassword" > <requestfocus /> </edittext> <checkbox android:id="@+id/checkbox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示密码" /> </linearlayout>
package com.example.test; import android.app.activity; import android.os.bundle; import android.text.method.hidereturnstransformationmethod; import android.text.method.passwordtransformationmethod; import android.widget.checkbox; import android.widget.compoundbutton; import android.widget.compoundbutton.oncheckedchangelistener; import android.widget.textview; public class mainactivity extends activity { private textview edittext1; private checkbox checkbox1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.test); edittext1 =(textview) findviewbyid(r.id.edittext1); checkbox1=(checkbox) findviewbyid(r.id.checkbox1); checkbox1.setoncheckedchangelistener(new oncheckedchangelistener() { @override public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { // todo auto-generated method stub if(ischecked){ //如果选中,显示密码 edittext1.settransformationmethod(hidereturnstransformationmethod.getinstance()); }else{ //否则隐藏密码 edittext1.settransformationmethod(passwordtransformationmethod.getinstance()); } } }); } }
关键是:
edittext1.settransformationmethod(hidereturnstransformationmethod.getinstance()); edittext1.settransformationmethod(passwordtransformationmethod.getinstance());
以上所述是小编给大家介绍的android 密码 显示与隐藏功能实例,希望对大家有所帮助