Main2Activity
public class Main2Activity extends AppCompatActivity {
private EditText ed_phone;
private EditText ed_pwd;
private CheckBox jz;
private Button btn;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initView();
initData();
}
private void initView() {
ed_phone = findViewById(R.id.ed_phone);
ed_pwd = findViewById(R.id.ed_pwd);
jz = findViewById(R.id.checkbox);
btn = findViewById(R.id.btn);
sp = getSharedPreferences("login", MODE_PRIVATE);
if (sp.getBoolean("ischeck",false)){
jz.setChecked(true);
String phone = sp.getString("phone", "");
String pwd = sp.getString("pwd", "");
ed_phone.setText(phone);
ed_pwd.setText(pwd);
}
}
private void initData() {
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String phone = ed_phone.getText().toString().trim();
String pwd = ed_pwd.getText().toString().trim();
if (jz.isChecked()){
sp.edit().putString("phone",phone).putString("pwd",pwd)
.putBoolean("ischeck",true).commit();
}else {
sp.edit().putBoolean("ischeck",false).commit();
}
}
});
}
}