2015年7月8日 星期三
[Android] SharedPreferences
private CheckBox savePwd;
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
//----------------------------------------------
loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
isIDPwdExist = loginPreferences.getBoolean("isIDPwdExist", false); //if there is not value exist, false by default.
if (isIDPwdExist == true) {
et_userid.setText(loginPreferences.getString("username", ""));
et_pwd.setText(loginPreferences.getString("password", ""));
savePwd.setChecked(true);
}
//----------------------------------------------
public void preferenceSetting(){
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et_userid.getWindowToken(), 0);
if (savePwd.isChecked()) {
loginPrefsEditor.putBoolean("isIDPwdExist", true);
loginPrefsEditor.putString("username", et_userid.getText().toString());
loginPrefsEditor.putString("password", et_pwd.getText().toString());
loginPrefsEditor.commit();
} else {
// loginPrefsEditor.clear();
loginPrefsEditor.putBoolean("isIDPwdExist", false);
loginPrefsEditor.remove("password");
loginPrefsEditor.commit();
}
}
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言