效果图:
在勾选自动登录后下次打开软件会直接跳过登录界面
代码:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_go);//即activity_login.xml findViews(); if(sp.getBoolean("ISCHECK", false)) { //设置默认是记录密码状态 rem_pw.setChecked(true); etusername.setText(sp.getString("USER_NAME", "")); //判断自动登陆多选框状态 if(sp.getBoolean("AUTO_ISCHECK", false)) { //设置默认是自动登录状态 auto_login.setChecked(true); //跳转界面 Intent intent = new Intent(GoActivity.this,IndexActivity.class); GoActivity.this.startActivity(intent); } } login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = etusername.getText().toString(); System.out.println(username); Log.i("TAG",username); UserService uService=new UserService(GoActivity.this); boolean flag=uService.login(username); if(flag){ Log.i("TAG","登录成功"); Toast.makeText(GoActivity.this, "登录成功", Toast.LENGTH_LONG).show(); if(rem_pw.isChecked()) { //记住用户名、密码、 SharedPreferences.Editor editor = sp.edit(); editor.putString("USER_NAME", username); editor.commit(); } Intent intent = new Intent(GoActivity.this,ListActivity.class);//页面跳转 startActivity(intent); }else{ Log.i("TAG","登录失败"); Toast.makeText(GoActivity.this, "登录失败", Toast.LENGTH_LONG).show(); } } }); register.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent=new Intent(GoActivity.this,RegisterActivity.class); startActivity(intent); } }); rem_pw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { if (rem_pw.isChecked()) { System.out.println("记住密码已选中"); sp.edit().putBoolean("ISCHECK", true).commit(); }else { System.out.println("记住密码没有选中"); sp.edit().putBoolean("ISCHECK", false).commit(); } } }); //监听自动登录多选框事件 auto_login.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { if (auto_login.isChecked()) { System.out.println("自动登录已选中"); sp.edit().putBoolean("AUTO_ISCHECK", true).commit(); } else { System.out.println("自动登录没有选中"); sp.edit().putBoolean("AUTO_ISCHECK", false).commit(); } } }); }