public class MainActivity extends AppCompatActivity {
private TextView textView;
private EditText edtTxtMainUserName;
private EditText edtTxtMainPassword;
private CheckBox chkBoxMainauto;
private CheckBox chkBoxMainPSW;
private Button btnMainLogin;
private Button btnMainzhuce;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
sp=getSharedPreferences("Login",MODE_PRIVATE);
chkBoxMainauto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// SharedPreferences.Editor editor=sp.edit();
// editor.putBoolean("autologin",isChecked);
// editor.putBoolean("autologin",isChecked);
if (isChecked){
chkBoxMainauto.setChecked(true);
}
}
});
chkBoxMainPSW.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor=sp.edit();
editor.putBoolean("chkName",isChecked);
editor.putBoolean("chkPSW",isChecked);
editor.commit();
if (isChecked){
chkBoxMainPSW.setChecked(true);
}
}
});
btnMainLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String userName=edtTxtMainUserName.getText().toString();
String password=edtTxtMainPassword.getText().toString();
// if (chkBoxMainauto.isChecked()){
// SharedPreferences.Editor editor=sp.edit();
// editor.putBoolean("autologin",true);
// }else{
// SharedPreferences.Editor editor=sp.edit();
// editor.putBoolean("autologin",false);
// }
if (userName.equals("admin")&&password.equals("123456")){
// Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
SharedPreferences.Editor editor = sp.edit();
editor.putString("userName", userName);
editor.putString("password",password);
editor.commit();
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
MainActivity.this.startActivity(intent);
} else{
AlertDialog dialog;
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setMessage("账号或密码错误");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//
}
});
dialog=builder.create();
dialog.show();
}
}
});
}
@Override
protected void onStart(){
super.onStart();
boolean auto=sp.getBoolean("auto",false);
chkBoxMainPSW.setChecked(auto);
boolean chkPSW=sp.getBoolean("chkPSW",false);
chkBoxMainPSW.setChecked(chkPSW);
boolean chkName=sp.getBoolean("chkName",false);
chkBoxMainPSW.setChecked(chkName);
if (chkName){
edtTxtMainUserName.setText(sp.getString("userName",null));
}
if (chkPSW){
edtTxtMainPassword.setText(sp.getString("password",null));
}
}
private void initView() {
textView = (TextView) findViewById(R.id.textView);
edtTxtMainUserName = (EditText) findViewById(R.id.edtTxtMainUserName);
edtTxtMainPassword = (EditText) findViewById(R.id.edtTxtMainPassword);
chkBoxMainauto = (CheckBox) findViewById(R.id.chkBoxMainauto);
chkBoxMainPSW = (CheckBox) findViewById(R.id.chkBoxMainPSW);
btnMainLogin = (Button) findViewById(R.id.btnMainLogin);
btnMainzhuce = (Button) findViewById(R.id.btnMainzhuce);
}
}