【发布时间】:2015-08-18 04:06:18
【问题描述】:
我在堆栈上看到了这个Need to save a high score for an Android game
这是它告诉我的
//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
我在想,“key”应该是高分所在的字符串吗?我命名我的偏好键是否重要。 感谢您的宝贵时间。
【问题讨论】:
-
是的,键必须是字符串,名称无所谓