當用戶登錄時,我正在另一個活動中保存兩個字符串,以便我可以保存其數據以使用另一個時間(對於最終用戶而言易用性)。無論如何,我已經設置了代碼並運行它,並且字符串沒有被傳遞。爲了確保,sharedPreferences正在工作,我已經建立了一個敬酒,以查看它是否與我推測的信息相匹配。SharedPreferences不起作用
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();
}
}
嗨,這仍然沒有顯示舉杯我更新了我的代碼太 – TheBlueCat 2012-04-07 16:28:46
@ user1245593。在你的Toast上調用show()方法來顯示它:Toast.makeText(getApplicationContext(),「yaya」,Toast.LENGTH_SHORT).show();' – Luksprog 2012-04-07 16:31:35