我正在努力使您可以保存高分,我还需要用户能够重置/删除他们的高分。 TOAST 有效,但数据不会被删除。
public static final String PREFS_NAME = "MyPrefsFile";
static SharedPreferences settings;
static SharedPreferences.Editor editor;
// When 'back' button is pressed save the highscore to settings
editor = settings.edit();// Create a new editor
editor.putInt("highscore", HighScore); // Storing integer
editor.commit();
// When 'Show' button is pressed
public void showPreferences(View v) {
int highscore = GameActivity.settings.getInt("highscore", GameActivity.HighScore);
Toast.makeText( MainMenu.this, "Your Highscore is: " + highscore, Toast.LENGTH_LONG).show();
}
//When delete button is pressed
public void clearPreferences(View V) {
GameActivity.editor = GameActivity.settings.edit();// Create a new editor
GameActivity.editor.clear();
GameActivity.editor.commit();
Toast.makeText( MainMenu.this,"Highscore has been reset",Toast.LENGTH_LONG).show();
}
最佳答案
我相信你只是读错了,使用这个
int highscore = GameActivity.settings.getInt("highscore", 0);
请注意,第二个参数是默认值,如果设置中不存在该键的值,则会返回该值。
关于java - 清除共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30087204/