代码之家  ›  专栏  ›  技术社区  ›  Ingor Sievaz

在Android中读/写resultcode

  •  0
  • Ingor Sievaz  · 技术社区  · 6 年前

    在读取 Activity 或者在 SharedPreferences 是的。

    这个应用程序是一个测试,应该显示玩家的高分。

    第一份我的开始菜单 活动 以下内容:

    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_menu);
    
        init();
    
    
    }
    
    private void init() {
    
        // init Ui Elements
       Button startTimeQuizBT = (Button) findViewById(R.id.StartTimeQuiz);
    
    
        // init onClickListeners for Buttons
    
        startTimeQuizBT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick( View v ) {
                Intent intent = new Intent(getApplicationContext(), QuizTimeActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    
    }
    
    
    @Override
    protected void onResume() {
        super.onResume();
        TextView ScoreTV = (TextView) findViewById(R.id.highscoreTV);
        ScoreTV.setText("Aktueller Highscore : " + Integer.toString(readHighscore()));
    
    }
    
    private int readHighscore() {
    
        SharedPreferences pref = getSharedPreferences("GAME", 0);
        return pref.getInt("HIGHSCORE", 0);
    }
    
    @Override
    protected void onActivityResult( int requestCode, int resultCode, Intent data ) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode ==1) {
            if (resultCode > readHighscore()) {
                writeHighscore(resultCode);
            }
        }
    
    
    }
    
    private void writeHighscore( int highscore ) {
    
        SharedPreferences pref = getSharedPreferences("GAME", 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt("HIGHSCORE", highscore);
        editor.apply();
    }
    }
    

    这个 活动 基本上是主菜单,应该开始 QuizTimeActivity 对于 Result 显示玩家当前的最高得分。

    其次是 快速活动 以下内容:

    QuestionLib qLib = new QuestionLib();
    String TAG = "QuizTimeActivity";
    private Button choice1;
    private Button choice2;
    private Button choice3;
    private Button choice4;
    private TextView questionTV;
    private TextView scoreTV;
    private int score = 0;
    private int randomNumber = 0;
    private String correctAnswer;
    private TextView countdownTV;
    
    
    @Override
    protected void onCreate( Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz_time);
    
    
        countdownTV = (TextView) findViewById(R.id.countdown);
    
        CountDownTimer timer = new CountDownTimer(90 * 1000, 100) {
            @Override
            public void onTick( long millisUntilFinished ) {
    
                countdownTV.setText(String.valueOf(millisUntilFinished / 1000));
            }
    
            @Override
            public void onFinish() {
    
                setResult(score);
                Log.i(TAG, "current score : " + score);
                startActivity(new Intent(getApplicationContext(), StartMenuActivity.class));
                finish();
            }
    
        }.start();
    
    
        choice1 = (Button) findViewById(R.id.choice1);
        choice2 = (Button) findViewById(R.id.choice2);
        choice3 = (Button) findViewById(R.id.choice3);
        choice4 = (Button) findViewById(R.id.choice4);
    
        questionTV = (TextView) findViewById(R.id.question);
        scoreTV = (TextView) findViewById(R.id.scoreTV);
    
        generateRandomNumber(0, 162);
    
        updateQuestion();
    
    
    
        choice1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick( View view ) {
                if (choice1.getText() == correctAnswer) {
                    blinkEffectGreen(choice1);
                    score++;
                }
                else {
                    blinkEffectRed(choice1);
                }
            }
        });
    
        choice2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick( View view ) {
                if (choice2.getText() == correctAnswer) {
                    blinkEffectGreen(choice2);
                    score++;
                }
    
                else {
                    blinkEffectGreen(choice2);
                }
            }
        });
    
        choice3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick( View view ) {
                if (choice3.getText() == correctAnswer) {
    
                    blinkEffectGreen(choice3);
                    score++;
    
                }
    
                else {
                    blinkEffectRed(choice3);
                }
            }
        });
    
    
        choice4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick( View view ) {
                if (choice4.getText() == correctAnswer) {
    
                    blinkEffectGreen(choice4);
                    score++;
                }
    
                else {
                    blinkEffectRed(choice4);
                }
            }
        });
    

    不知怎的,当 QuiztimeActivty 90秒后结束。

    我在这里做错什么了吗(我想是的,否则它会起作用…)?

    我希望这些信息是足够的,如果不是我会提供更多。

    谢谢您!

    1 回复  |  直到 6 年前
        1
  •  0
  •   Barns    6 年前

    首先,您需要更改 onFinish() 方法:

    @Override
    public void onFinish() {
        Intent intent = new Intent();
        intent.putExtra("MY_SCORE", score);
        setResult(RESULT_OK, intent);
        finish();
    }
    

    然后你需要在 onActivityResult() 方法:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == 1) {
            if(resultCode == RESULT_OK){
                int score = data.getIntExtra("MY_SCORE", -1);
    
                if (score > readHighscore()) {
                    writeHighscore(resultCode);
                }
            }
        }
    }
    

    你不应该用 startActivity() 在你的 完成() 方法,因为这将启动活动的新实例只要让活动结束,焦点就会回到开始它的活动上。