sharedPreferences=getSharedPreferences("User", MODE_PRIVATE);
editor=sharedPreferences.edit();
//实现联动效果
cbLogin.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
cbPwd.setChecked(true);
}else{
cbPwd.setChecked(false);
}
}
});
//得到我们的存入的状态值,如果记住密码或自动登录已勾选,那么状态值为true,那就进入一以下的判断,如果未勾选,反之
boolean Checkedone=sharedPreferences.getBoolean("Checkedone", false);
boolean Checkedtwo=sharedPreferences.getBoolean("Checkedtwo", false);
//进入当前判断,从sharedPreferences里取出账号、密码为输入框设置,记住密码的复选框为勾选状态
if(Checkedone){//记住密码
String name=sharedPreferences.getString("name", null);
String pass=sharedPreferences.getString("pass", null);
ed1.setText(name);
ed2.setText(pass);
cbPwd.setChecked(true);
}
if(Checkedtwo){//记住自动登陆
cbLogin.setChecked(true);
//直接跳转到下一页面
Intent it=new Intent(MainActivity.this,TwoActivity.class);
startActivity(it);
finish();
}
}
//设置点击监听
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.but:
//判断当前自动登录的勾选状态
if(cbLogin.isChecked()){
String name=ed1.getText().toString();
String pass=ed2.getText().toString();
editor.putString("name", name);//保存用户名
editor.putString("pass", pass);//保存密码
editor.putBoolean("Checkedtwo", true);//保存状态值
editor.commit();
}
/*
* 判断如果复选框为true时,进入当前判断,首先获取当前的账号和密码,然后在进行保存,我们还需要
* 存入一个状态值
* 记住密码
*/
if(cbPwd.isChecked()){
String pass=ed2.getText().toString();
editor.putString("pass", pass);//保存密码
editor.putBoolean("Checkedone", true);//保存状态值
editor.commit();
}
//跳转到下一页面
Intent it=new Intent(MainActivity.this,TwoActivity.class);
startActivity(it);
//关闭当前的activity
finish();
break;
default:
break;
}
}
记住密码,自动登录
最新推荐文章于 2024-04-23 17:25:59 发布