首先在xml里创建两个控件 EditText和CheckBox
然后就很简单了
dt1=(EditText)findViewById(R.id.password);
cb1=(CheckBox)findViewById(R.id.checkbox_1);
cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(cb1.isChecked()){
dt1.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); //如果被选中则显示密码
dt1.setSelection(dt1.getText().length()); //TextView默认光标在最左端,这里控制光标在最右端
}else {
dt1.setTransformationMethod(PasswordTransformationMethod.getInstance()); //如果没选中CheckBox则隐藏密码 dt1.setSelection(dt1.getText().length());
}
}
});效果如下:
本文介绍如何使用Android中的CheckBox和EditText实现密码的显示与隐藏功能。通过设置OnCheckedChangeListener监听器,根据CheckBox的状态改变EditText的TransformationMethod属性来达到显示或隐藏密码的效果。
747

被折叠的 条评论
为什么被折叠?



