我想保存用戶憑據使用sharedpreferences,我通過這種方式這樣做,但它不工作保存憑據sharedpreferences
public class MainActivity extends Activity {
public EditText UserName;
public EditText Password;
public Button mButton;
public String user_name;
public String pass_word;
public CheckBox mCheckSavePassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UserName = (EditText) findViewById(R.id.textUserName);
Password = (EditText) findViewById(R.id.textPassword);
mButton = (Button) findViewById(R.id.buttonLogin);
mCheckSavePassword = (CheckBox) findViewById(R.id.checkSavePassword);
mCheckSavePassword = (CheckBox) findViewById(R.id.checkSavePassword);
SharedPreferences.Editor preferencesEditor = preferences.edit();
preferencesEditor.putString("prefUserName", user_name);
if(mCheckSavePassword.isChecked()) {
preferencesEditor.putString("prefPassword", pass_word);
}
preferencesEditor.putBoolean("prefSavePassword", mCheckSavePassword.isChecked());
preferencesEditor.apply();
}
你怎麼知道它不工作? – 2014-11-05 19:41:30
,因爲當我再次重新輸入時,需要填寫憑證 – Andrew 2014-11-05 19:46:40
發佈您正在使用的代碼來讀取首選項值,以便我們可以看到什麼不起作用。 – 2014-11-05 19:47:55