Java 如何在Android问答应用程序中更改计时器?

Java 如何在Android问答应用程序中更改计时器?,java,android,Java,Android,我的测验android应用程序有20个选择题。每个问题都有一个30秒的计时器。我不想每个问题都单独计时。我希望倒数计时器对所有20个问题集体运行20分钟,而不是对每个问题运行30秒。怎么做?请帮忙 private void startCountDown() { countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) { @Override public void onTick(long mil

我的测验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();
    }

}
private void startCountDown(){
倒计时=新的倒计时(timeLeftInMillis,1000){
@凌驾
公共void onTick(长毫秒未完成){
timeLeftInMillis=完成前的毫秒数;
updateCountDownText();
}
@凌驾
公共无效onFinish(){
timeLeftInMillis=0;
updateCountDownText();
检查答案();
}
}.start();
}
私有void updateCountDownText(){
整数分钟=(整数)(timeLeftInMillis/1000)/60;
整数秒=(整数)(timeLeftInMillis/1000)%60;
String timeFormatted=String.format(Locale.getDefault(),%02d:%02d),分,秒);
textViewCountDown.setText(时间格式);
如果(timeLeftInMillis<10000){
textViewCountDown.setTextColor(Color.RED);
}否则{
textViewCountDown.setTextColor(textColorDefaultCd);
}
}
私有void checkAnswer(){
回答=正确;
倒计时。取消();
RadioButton rbSelected=findViewById(rbGroup.getCheckedRadioButtonId());
int answerNr=rbGroup.indexOfChild(rbSelected)+1;
if(answerNr==currentQuestion.getAnswerNr()){
分数++;
textViewScore.setText(“分数:+Score”);
}
showSolution();
}
私有void showSolution(){
rb1.setTextColor(Color.RED);
rb2.setTextColor(Color.RED);
rb3.setTextColor(Color.RED);
rb4.setTextColor(Color.RED);
开关(currentQuestion.getAnswerNr()){
案例1:
rb1.setTextColor(Color.GREEN);
textViewQuestion.setText(“选项1正确”);
打破
案例2:
rb2.setTextColor(Color.GREEN);
textViewQuestion.setText(“选项2是正确的”);
打破
案例3:
rb3.setTextColor(Color.GREEN);
textViewQuestion.setText(“选项3正确”);
打破
案例4:
rb4.setTextColor(Color.GREEN);
textViewQuestion.setText(“选项4是正确的”);
打破
}
如果(问号计数器<问号总数){
buttonConfirmNext.setText(“下一步”);
}否则{
按钮confirmNext.setText(“完成”);
}
}
私有void finishquick(){
完成();
}
@凌驾
public void onBackPressed(){
如果(backPressedTime+2000>System.currentTimeMillis()){
完成测验();
}否则{
Toast.makeText(这是“再次按下以完成”,Toast.LENGTH_SHORT.show();
}
backPressedTime=System.currentTimeMillis();
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
if(倒计时!=null){
倒计时。取消();
}
}

对于计时操作,应使用处理器

如果您需要运行后台服务,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);

你们想不想在屏幕上显示timmer?是的,我想在屏幕上显示它。