简体   繁体   中英

A simple quiz with 3 buttons ANDROID

I'm trying a make a quiz with 3 choices/buttons, so when one button is clicked, it will move to the next question by changing the 3 buttons' texts and the textview (displays the question). For example, I have 3 questions. I used a for loop to loop all the questions but this strategy does not seem to work. Is there a better suggestion of how I should implement my codes?

for(int k=0; k<3;k++){
  t1.setText(Quizz[k].questionText);//textview displays the question
buttons[0].setText(Quizz[k].answers.get(0).getText());
  buttons[1].setText(Quizz[k].answers.get(1).getText());
    buttons[2].setText(Quizz[k].answers.get(2).getText());

//go through three values stored in three buttons
for(int run = 0;run<3;run++){
  if(Quizz[k].answers.get(run).getCorrect()==true){
buttons[k].setOnClickListener(new View.OnClickListener()
      {

   public void onClick(View v)
   {

       //continue to next question
       Toast.makeText(getApplicationContext(), "You just clicked the correct button =)",
       Toast.LENGTH_SHORT).show();


                }
            });

        }
        else {

            buttons[k].setOnClickListener(new View.OnClickListener()
            {


                public void onClick(View v)
                {
               //continue to next question
                Toast.makeText(getApplicationContext(), "Oops that's not right)",
                           Toast.LENGTH_SHORT).show();


                }
            });
            }
        //no matter if correct or wrong, go to the next question



    }

I can only tell you how I would do this.. Not sure this will be compilable but will hopefully give you an idea. Uses three buttons in the xml layout linked with the onClick() method (ie button1 linked to answered(1) using onClick() )

int currentquestion = 0;

public void updateQuestion() {

    t1.setText(Quizz[currentquestion].questionText);
    buttons[0].setText(Quizz[currentquestion].answers.get(0).getText());
    buttons[1].setText(Quizz[currentquestion].answers.get(1).getText());
    buttons[2].setText(Quizz[currentquestion].answers.get(2).getText());
}

public void answered(View clickedButton, int answer) {
    if (Quizz[currentquestion].answers.get(answer).getCorrect() ) {
        // code to celebrate
    } else {
        // other code
    }

    currentquestion++;
    updateQuestion();
}

if you simply doing loop in your main thread, it will executed right from the first code till the end, you will only see the result.

try to create method that display the question when it is called. and you simply listen to the event (eg click)

the flowchart is like this:
display start button -> show question -> click -> process -> show question2 -> click -> process ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM