public class MainActivity extends AppCompatActivity {
private Button button;
private EditText editText1;
private EditText editText2;
private CheckBox checkBox;
private Button bu;
SharedPreferences pref;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (Button) findViewById(R.id.button);
editText1= (EditText) findViewById(R.id.editText);
editText2= (EditText) findViewById(R.id.editText2);
checkBox= (CheckBox) findViewById(R.id.checkBox);
bu= (Button) findViewById(R.id.button);
pref=getSharedPreferences("mypref",MODE_PRIVATE);
editor=pref.edit();
String con=pref.getString("username","");
if (con==null){
checkBox.setChecked(false);
}
else {checkBox.setChecked(true);}
bu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user1=editText2.getText().toString().trim();
String user=editText1.getText().toString().trim();
if ("admin".equals(user)&&"1234567".equals(user1)){
if (checkBox.isChecked()){
editor.putString("username",user1);
editor.putString("password",user);
editor.commit();
}
//Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG);
else {
editor.remove("username");
editor.remove("password");
editor.commit();
}
Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"密码错误",Toast.LENGTH_SHORT);
}
}
});
}
}