我想在应用程序中设置倒数计时器,我该怎么办?

时间:2019-07-13 18:26:27

标签: android countdowntimer

在我的测验应用中,我有倒数计时器,我将每个问题的持续时间设置为20秒,在20秒钟自动模版放置后转到下一个问题,但是我想为所有问题设置1小时,在1小时后显示结果。 这是我的代码:

   txt_Score = findViewById(R.id.txt_Score);
    txt_Question_Num = findViewById(R.id.txt_Total_Question);
    question_Text = findViewById(R.id.question_text);

    question_Image = findViewById(R.id.question_image);

    btn_AnswerA = findViewById(R.id.btn_AnswerA);
    btn_AnswerB = findViewById(R.id.btn_AnswerB);
    btn_AnswerC = findViewById(R.id.btn_AnswerC);
    btn_AnswerD = findViewById(R.id.btn_AnswerD);

    textView = findViewById(R.id.textView);

    btn_AnswerA.setOnClickListener(this);
    btn_AnswerB.setOnClickListener(this);
    btn_AnswerC.setOnClickListener(this);
    btn_AnswerD.setOnClickListener(this);

}

public void onClick(View view) {

    countDown.cancel();
    if (index < totalQuestion)
    {
        Button clickedView = (Button)view;
        if (clickedView.getText().equals(CommonModel.questionList.get(index).getAnswer())){
            score+=5;
            correctAnswer++;
            showQuestion(++index); //next question
        }
        else //choose wrong answer
        {
            showQuestion(++index);
        }

        txt_Score.setText(String.format("%d",score));
    }
}

private void showQuestion(int index) {
    if (index < totalQuestion)
    {
        thisQuestion++;
        txt_Question_Num.setText(String.format("%d / %d",thisQuestion,totalQuestion));

        if (CommonModel.questionList.get(index).getIsImageQuestion().equals("true"))
        {
            // If is image
            Picasso.get()
                    .load(CommonModel.questionList.get(index).getQuestion())
                    .into(question_Image);

            question_Image.setVisibility(View.VISIBLE);
            question_Text.setVisibility(View.INVISIBLE);
        }
        else
        {
            question_Text.setText(CommonModel.questionList.get(index).getQuestion());

            question_Image.setVisibility(View.INVISIBLE);
            question_Text.setVisibility(View.VISIBLE);
        }

// question_Text.setText(CommonModel.questionList.get(index).getQuestion());

        btn_AnswerA.setText(CommonModel.questionList.get(index).getOption1());
        btn_AnswerB.setText(CommonModel.questionList.get(index).getOption2());
        btn_AnswerC.setText(CommonModel.questionList.get(index).getOption3());
        btn_AnswerD.setText(CommonModel.questionList.get(index).getOption4());

        countDown.start();

    }
    else
    {
        // If it is final question
        Intent intent = new Intent(this,DoneActivity.class);
        Bundle dataSend = new Bundle();
        dataSend.putInt("SCORE",score);
        dataSend.putInt("TOTAL",totalQuestion);
        dataSend.putInt("CORRECT",correctAnswer);
        intent.putExtras(dataSend);
        startActivity(intent);
        finish();
    }

}

@Override
protected void onResume() {
    super.onResume();

    totalQuestion = CommonModel.questionList.size();

    countDown = new CountDownTimer(TIMEOUT,INTERVAL) {
        @Override
        public void onTick(long millisUntilFinished) {

            long secondsInMilli = 1000;
            long minutesInMilli = secondsInMilli * 60;
            long hoursInMilli = minutesInMilli * 60;

            long elapsedHours = millisUntilFinished / hoursInMilli;
            millisUntilFinished = millisUntilFinished % hoursInMilli;

            long elapsedMinutes = millisUntilFinished / minutesInMilli;
            millisUntilFinished = millisUntilFinished % minutesInMilli;

            long elapsedSeconds = millisUntilFinished / secondsInMilli;

            String yy = String.format("%02d", elapsedSeconds);
            textView.setText(yy);

        }

        @Override
        public void onFinish() {
            countDown.cancel();
            showQuestion(++index);
        }
    };
    showQuestion(index);
}

当用户单击对或错时,我需要两个选项,这将转到下一个问题,并且在1小时后显示结果。 我该怎么办?

1 个答案:

答案 0 :(得分:0)

像这样一小时后,您可以使用处理程序来执行任务

在您的活动上创建,创建处理程序,

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            //do what you want to do after one hour
        }
    },1000*60*60);

确保一小时后运行代码时不会创建任何异常