所以我试图用 sharedPreferences 保存最高分,但我遇到了麻烦。我不知道如何将其设置为只取最高分并显示它。
我现在所拥有的只会显示玩家收到的当前分数。这是我到目前为止不起作用的内容:
public class GameOptions extends Activity {
int theScore;
TextView highScore;
public static String filename = "MyHighScore";
SharedPreferences spHighScore;
int dataReturned;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.in_game_menu);
TextView tvTheScore = (TextView) findViewById(R.id.textView2);
TextView highScore = (TextView) findViewById(R.id.high_score);
Bundle gotScore = getIntent().getExtras();
theScore = gotScore.getInt("scoreKey");
//String thisIsTheScoreToDisplay = theScore.toString();
tvTheScore.setText("SCORE: "+theScore);
spHighScore = getSharedPreferences(filename, 0);
SharedPreferences.Editor editor = spHighScore.edit();
editor.putInt("highScoreKey", theScore);
editor.commit();
int dataReturned = spHighScore.getInt("highScoreKey", 0);
highScore.setText("" + dataReturned);