java - 如何更改 Android 测验应用程序中的计时器?

标签 java android

我的测验 Android 应用程序有 20 个多项选择题。每个问题有 30 秒的计时器。我不希望计时器单独为每个问题运行。我希望倒计时器为所有 20 个问题总共运行 20 分钟,而不是每个问题运行 30 秒。怎么做?请帮忙。

private void startCountDown() {
    countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            timeLeftInMillis = millisUntilFinished;
            updateCountDownText();
        }

        @Override
        public void onFinish() {
            timeLeftInMillis = 0;
            updateCountDownText();
            checkAnswer();
        }
    }.start();
}

private void updateCountDownText() {
    int minutes = (int) (timeLeftInMillis / 1000) / 60;
    int seconds = (int) (timeLeftInMillis / 1000) % 60;

    String timeFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);

    textViewCountDown.setText(timeFormatted);

    if (timeLeftInMillis < 10000) {
        textViewCountDown.setTextColor(Color.RED);
    } else {
        textViewCountDown.setTextColor(textColorDefaultCd);
    }
}

private void checkAnswer() {
    answered = true;

    countDownTimer.cancel();

    RadioButton rbSelected = findViewById(rbGroup.getCheckedRadioButtonId());
    int answerNr = rbGroup.indexOfChild(rbSelected) + 1;

    if (answerNr == currentQuestion.getAnswerNr()) {
        score++;
        textViewScore.setText("Score: " + score);
    }

    showSolution();
}

private void showSolution() {
    rb1.setTextColor(Color.RED);
    rb2.setTextColor(Color.RED);
    rb3.setTextColor(Color.RED);
    rb4.setTextColor(Color.RED);

    switch (currentQuestion.getAnswerNr()) {
        case 1:
            rb1.setTextColor(Color.GREEN);
            textViewQuestion.setText("Option 1 is correct");
            break;
        case 2:
            rb2.setTextColor(Color.GREEN);
            textViewQuestion.setText("Option 2 is correct");
            break;
        case 3:
            rb3.setTextColor(Color.GREEN);
            textViewQuestion.setText("Option 3 is correct");
            break;
        case 4:
            rb4.setTextColor(Color.GREEN);
            textViewQuestion.setText("Option 4 is correct");
            break;
    }

    if (questionCounter < questionCountTotal) {
        buttonConfirmNext.setText("Next");
    } else {
        buttonConfirmNext.setText("Finish");
    }
}

private void finishQuiz() {

    finish();
}

@Override
public void onBackPressed() {
    if (backPressedTime + 2000 > System.currentTimeMillis()) {
        finishQuiz();
    } else {
        Toast.makeText(this, "Press back again to finish", Toast.LENGTH_SHORT).show();
    }

    backPressedTime = System.currentTimeMillis();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (countDownTimer != null) {
        countDownTimer.cancel();
    }

}

最佳答案

对于定时操作,您应该使用Handler。

如果您需要运行后台服务,则 AlarmManager 是最佳选择。

private final int interval = 1200; // 20 minutes
private Handler handler = new Handler();
private Runnable runnable = new Runnable(){
    public void run() {
        Toast.makeText(MyActivity.this, "Time is up", Toast.LENGTH_SHORT).show();
    }
};
...
handler.postDelayed(runnable, interval);

关于java - 如何更改 Android 测验应用程序中的计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59486498/

相关文章:

java命令行flyway迁移

java - 写入文本复制以前的数据

java - 您如何对项目进行版本控制和管理发布?

android - 如何在 Android 上使用 ndk 独立工具链的 libcurl

android - 无法导入 Google 项目

android - 在cygwin中编译android源代码

java - 删除超过 x 天的文件

java - 如何在两台计算机之间传递大量数据

android - 如何在android中使用runnable

android - Qt qml : Custom dialog not displayed on device, 应用程序崩溃