Android测验计时器

问题描述 投票:-2回答:1

今天是个好日子!我正在用计时器制作android测验应用程序。每个问题都设置了30秒回答。然后在我进入下一个问题时刷新计时器。现在,我在我的应用程序中发现了一个错误,我的测验计时器没有刷新或回到零,即使我进入下一个问题?谁能帮我?非常感谢你

公共类QuizHistoryActivity扩展AppCompatActivity {

private TextView timer;
private TextView countLabel;
private TextView questionLabel;
private Button answerBtn1;
private Button answerBtn2;
private Button answerBtn3;

private String rightAnswer;
private int rightAnswerCount = 0;
private int quizCount = 1;

static final private int QUIZ_COUNT = 10;

ArrayList<ArrayList<String>> quizArray = new ArrayList<>();

String quizData [][] = {
//Question  
{"What is the meaning of ALCU?",
//Answer   
"The association of Local College and University",
//Choice A      
"The association of Local College and University",
//Choice B     
 "The Association of Land Colleges  and Universities",
//Choice C      
"The Association of Level Colleges and Universities"}
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz_history);


    countLabel = (TextView)findViewById(R.id.countlabel);
    questionLabel= (TextView)findViewById(R.id.questionlabel);
    answerBtn1 = (Button)findViewById(R.id.answerbtn1);
    answerBtn2 = (Button)findViewById(R.id.answerbtn2);
    answerBtn3 = (Button)findViewById(R.id.answerbtn3);
    timer = (TextView)findViewById(R.id.timers);

    //Create quizArray from quizdata
    for (int i = 0; i < quizData.length; i++) {

        //Prepare array
        ArrayList<String> tmpArray = new ArrayList<>();
        tmpArray.add(quizData[i][0]);
        tmpArray.add(quizData[i][1]);
        tmpArray.add(quizData[i][2]);
        tmpArray.add(quizData[i][3]);
        tmpArray.add(quizData[i][4]);

        //Add tmpArray to quizArray
        quizArray.add(tmpArray);

    }
    CountDownTimer countDown;
    countDown= new CountDownTimer(30000, 1000) {

        public void onTick(long millisUntilFinished) {
            timer.setText("seconds remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
    showNextQuiz();
        }
    };
    countDown.start();
}

public  void showNextQuiz () {

    //Update quizCountLabel
    countLabel.setText("Question #" + quizCount);

    //Generate random number between 0 and 14 (Quiz Array's size -1)

    Random random = new Random();
    int randomNum = random.nextInt(quizArray.size());

    //Pick ine quiz set
    ArrayList<String> quiz = quizArray.get(randomNum);

    //Set question and right answer
    //array format
    questionLabel.setText(quiz.get(0));
    rightAnswer = quiz.get(1);

    //remove "country" from quiz and shuffle choice
    quiz.remove(0);
    Collections.shuffle(quiz);

    //Set Choices
    answerBtn1.setText(quiz.get(0));
    answerBtn2.setText(quiz.get(1));
    answerBtn3.setText(quiz.get(2));

    //Remove this quiz from quizArray
    quizArray.remove(randomNum);
}

public void checkAnswer (View view) {

    //Get pushed button
    Button answerBtn = (Button)findViewById(view.getId());
    String btnText = answerBtn.getText().toString();

    String alertTitle;

    if(btnText.equals(rightAnswer)) {
        //Correct!
        alertTitle = "Correct!";
        rightAnswerCount++;
    }else {
        //Wrong
        alertTitle = "Wrong";
    }

    //create Dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(alertTitle);
    builder.setMessage("Answer : \n \t \t" + rightAnswer);
    builder.setPositiveButton("Got It!", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            if (quizCount == QUIZ_COUNT) {
                //Show Result

                Intent resultintent = new Intent(getApplicationContext(), ResultQuizHistoryActivity.class);
                resultintent.putExtra("RIGHT_ANSWER_COUNT", rightAnswerCount);
                startActivity(resultintent);
            }else {
                quizCount++;
                showNextQuiz();
            }
        }
    });
    builder.setCancelable(false);
    builder.show();
}

}

java android
1个回答
0
投票

您正在onCreate方法中创建CountDownTimer,该方法在Activity生命周期中调用一次。您应该将其创建为类的实例,并在显示新问题之前尝试重置它。

© www.soinside.com 2019 - 2024. All rights reserved.