我看到這個堆棧Need to save a high score for an Android game保存int的共享首選項?
這就是它告訴我
//setting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putInt("key", score);
editor.commit();
//getting preferences
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int score = prefs.getInt("key", 0); //0 is the default value
我在想,應該「鍵」可以在字符串,其中高分所在?這是否與我命名我的首選項密鑰有關? 謝謝你的時間。
是的,關鍵必須是字符串,名稱並不重要 – 3xplore