Need to save a high score for an Android game

前端 未结 4 713
不知归路
不知归路 2020-12-03 01:45

It\'s quite simple, all I need to do is save a high score (an integer) for the game. I\'m assuming the easiest way to do this would be to store it in a text file but I reall

4条回答
  •  爱一瞬间的悲伤
    2020-12-03 02:32

    public class HighScores extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_high);
    
            //get text view
            TextView scoreView = (TextView)findViewById(R.id.high_scores_list);
            //get shared prefs
            SharedPreferences scorePrefs = getSharedPreferences(PlayGame.GAME_PREFS, 0);
            //get scores
            String[] savedScores = scorePrefs.getString("highScores", "").split("\\|");
            //build string
            StringBuilder scoreBuild = new StringBuilder("");
            for(String score : savedScores){
                scoreBuild.append(score+"\n");
            }
            //display scores
            scoreView.setText(scoreBuild.toString());
        }
    
    }
    

提交回复
热议问题