当用户登录时,我正在为另一个活动保存两个字符串,这样我就可以保存他们的数据以供下次使用(最终用户易于使用)。无论如何,我已经设置了代码并运行它并且字符串没有被传递。为了确保 sharedPreferences 正常工作,我设置了一个 toast,看看它是否与我估算的信息相匹配。
1类:
uname = (EditText) findViewById(R.id.txt_username);
String username = uname.getText().toString();
pword = (EditText) findViewById(R.id.txt_password);
String password = pword.getText().toString();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("key1", username);
editor.putString("key2", password);
editor.commit();
第二类:
private void Test() {
// TODO Auto-generated method stub
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String username = settings.getString("key1", null);
String password = settings.getString("key2", null);
if(username.equals("irock97")) {
Toast.makeText(getApplicationContext(), "yaya", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
}