登录页面密码显示隐藏功能

来源:互联网 发布:springmvc json转对象 编辑:程序博客网 时间:2024/05/22 08:17
public class MainActivity extends Activity {


//密码设置它
private EditText password;
//用户名,基本就是占位置
private EditText username;
//切换密码显示隐藏的图标,用checkbox
private CheckBox hideorshow;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加载布局
initView();
}


//设置光标
private void setCursor() {
// TODO Auto-generated method stub
CharSequence text = password.getText();

if (text instanceof Spannable) {
Spannable spanText = (Spannable) text;
Selection.setSelection(spanText, text.length());//设置光标位置在文本末尾
}


}


private void initView() {
// TODO Auto-generated method stub
password = (EditText) findViewById(R.id.Login_edit_password);
username=(EditText) findViewById(R.id.Login_edit_name);
hideorshow = (CheckBox) findViewById(R.id.login_showorhide);
//切换密码显示隐藏的checkbox点击事件
hideorshow.setOnCheckedChangeListener(new OnCheckedChangeListener() {


@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
//设置密码可见
password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
//这个属性很少见,设置密码字间距
password.setTypeface(Typeface.MONOSPACE);
setCursor();

} else {
//设置密码隐藏
password.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
setCursor();
}
}
});


}


}
0 0
原创粉丝点击