【问题标题】:Storing a high score and displaying on a different activity存储高分并显示在不同的活动中
【发布时间】:2017-04-24 23:10:26
【问题描述】:

这是对我原来问题的修改,因为我改变了我想要显示分数的方式。

以前,我从 GameActivity 类中获得的分数可以毫无问题地传递给 MainMenu 活动类。我的问题是,我还想显示一个高分,如果用户击败了之前的最高分,它会改变。

我现在改变了主意,我将在游戏结束时显示用户在游戏活动中的当前分数,当他们点击返回 MainMenu 类/屏幕时,只显示最高分数。所以,我稍微更改了代码,只显示最高分,但这只是显示上一场比赛的分数,无论它是否是更高的分数。

我需要知道如何做到这一点,以便它只将分数传递给 MainMenu 活动,如果它击败了前一个活动。

我的 GameActivity 类如下:

public class GameActivity extends Activity {

GameView gameView;// Reference the gameView
MediaPlayer music;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    // remove title bar
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    // Set the layout
    setContentView(R.layout.game_view_container);
    // Get the id of the layout
    RelativeLayout mainscreen = (RelativeLayout) findViewById(R.id.mainscreen);
    // Make the GameView
    gameView = new GameView(this);
    // Get  data from intent and config gameView here

    music = MediaPlayer.create(this, R.raw.backgroundmusic);
    music.start();

    gameView.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT));
    // Add GameView
    mainscreen.addView(gameView);
}

/* Called when activity is done and should be closed. 
 * The ActivityResult is propagated back to whoever launched via onActivityResult()
 */
public void finish(){

    Intent returnIntent = new Intent();
    returnIntent.putExtra("GAME_SCORE", gameView.getHitCount());

    //returnIntent.putExtra("HIGH_SCORE", gameView.getHitCount());
    setResult(RESULT_OK, returnIntent);

    music.release();
    super.finish();

}

private Activity getActivity() {

    return null;
    }

}

我的 MainMenu 类如下:

public class MainMenu extends Activity {

private static final int SCORE_REQUEST_CODE = 1;// The request code for the intent

SharedPreferences prefsScore;

//TextView tvScore;
String score;
Intent gameIntent;
TextView highestScore;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_start);



    //tvScore = (TextView) findViewById(R.id.tvSpriteGame);
    highestScore = (TextView) findViewById(R.id.tvHighestScore);

}

private Activity getActivity() {
    return null;
}

public void startGame(View v) {
    gameIntent = new Intent(this, GameActivity.class);
    startActivityForResult(gameIntent, SCORE_REQUEST_CODE);
}




/* Create Options Menu */
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}


@Override
// Respond to item selected on OPTIONS MENU
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        //put data in Intent
        case R.id.userInfo:
            Intent intentSettings = new Intent(this, SettingsActivity.class);
            startActivity(intentSettings);
            return true;
        case R.id.easy:
            Toast.makeText(this, "Easy chosen", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.medium:
            Toast.makeText(this, "Medium chosen", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.hard:
            Toast.makeText(this, "Hard chosen", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.scores:
            Intent intentScore = new Intent(this, ScoresActivity.class);
            startActivity(intentScore);
            return true;
        case R.id.settings:
            Toast.makeText(this, "Game Settings chosen", Toast.LENGTH_SHORT).show();
            return true;
        case R.id.help:
            Intent intent = new Intent(this, HelpActivity.class);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}


protected void onActivityResult(int requestCode, int resultCode, Intent retIntent) {
    // Check which request we're responding to
    if (requestCode == SCORE_REQUEST_CODE) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            if (retIntent.hasExtra("GAME_SCORE")) {
                int scoreFromGame = retIntent.getExtras().getInt("GAME_SCORE");
                highestScore.setText(Integer.toString(scoreFromGame));




                    /*if (retIntent.hasExtra("HIGH_SCORE")) {
                        SharedPreferences.Editor editor = sharedPref.edit();
                        int highestScore = retIntent.getExtras().getInt("HIGH_SCORE");
                        highScore.setText((Integer.toString(scoreFromGame)));
                            if (scoreFromGame > highestScore) {
                                editor.putInt(getString(R.string.score), scoreFromGame);
                                editor.commit();
                    } else {
                                highScore.setText("" + highestScore);
                            }
                }*/
            }
        }
    }

   }
}

【问题讨论】:

  • 一种简单的方法是为分数设置一个静态变量,然后您可以在任何活动中访问它,例如(公共静态字符串分数;)并访问(TheActivityName.score)stackoverflow.com/questions/29093281/…
  • 第98行是什么

标签: java android android-intent sharedpreferences


【解决方案1】:

对您而言,存储最高价值的最佳和最简单的方法是共享偏好。它们是在您的应用活动期间甚至之后持续存在的键值对。

用法:

https://developer.android.com/training/basics/data-storage/shared-preferences.html

【讨论】:

  • 我以为我在使用共享首选项?
  • 您实际上是在尝试作为额外参数传递给 Intent(非持久性)。
  • 共享首选项是您的简单选择。所以下次你打开应用时,你仍然可以看到你的最高分。
  • 谢谢。我已将其标记为正确答案,但我仍然在试图将 Android 开发人员页面上的内容应用于我的代码时感到困惑
  • 那太好了。明天晚上你会在吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多