今天用昨天學的sharepreferences
來完成之前記住密碼的操作
//宣告
private SharedPreferences preferences;
preferences = getSharedPreferences("config",MODE_PRIVATE);
//讀取上次有沒有勾選記住密碼
boolean remember = preferences.getBoolean("remember", false);
if(remember){
reload();
}
//reload
private void reload() {
et_phone.setText(preferences.getString("phone",""));
et_password.setText(preferences.getString("password",""));
ck_remember.setChecked(true);
}
//如果有勾選就把手機號碼跟密碼記住下次使用
//如果沒有清除資料
SharedPreferences.Editor edit = preferences.edit();
if(ck_remember.isChecked()){
edit.putString("phone",et_phone.getText().toString());
edit.putString("password",et_password.getText().toString());
edit.putBoolean("remember",true);
edit.commit();
}else{
edit.remove("phone");
edit.remove("password");
edit.remove("remember");
edit.commit();
}
}