Android测验,如果答案是正确的,如何显示和随机答案

时间:2014-05-25 15:14:54

标签: java android toast

您好,我目前正在开发Android测验应用程序,该工具正常,它会根据您的分数读取问题并给出评分栏。但是,我还希望能够将问题随机化以使其成为可能更有趣的是,如果答案是否正确,可以通过吐司或文本声明来显示。我一直在网上寻找各种选项,找不到解决方法而不会犯很多错误。我主要活动的代码如下 谢谢,

package com.example.QuizApp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
import android.widget.Toast;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */

    int score = 0;
    int questionNumber = 0;

    Question questionsObject;
    RadioButton quizAnswerA;
    RadioButton quizAnswerB;
    RadioButton quizAnswerC;
    Button next;
    TextView questionTextView;

    ArrayList<Question> questionsList = new ArrayList<Question>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
//      addListenerOnButton();
        next = (Button) findViewById(R.id.nextButton);

          Question q1 = new Question("How much is an adult ticket with camping for the Isle of Wight?", "£30", "£190", "£200", "£190");
    questionsList.add(q1);
    Question q2 = new Question("How many people can the Isle of Wight festival hold?", "60,000", "75,000", "100,000", "60,000");
    questionsList.add(q2);
    Question q3 = new Question("Which year did the Isle of Wight festival begin?", "1968", "1971", "1995", "1968");
    questionsList.add(q3);
    Question q4 = new Question("What is the name of the town outside which Isle of Wight is held?", "New Forest", "Sandown", "Newport", "Newport");
    questionsList.add(q4);
    Question q5 = new Question("Which date does Isle of Wight start??", "12 June", "18 June", "6 August", "12 June");
    questionsList.add(q5);
    Question q6 = new Question("Who are headline acts on Friday?", "Biffy Clyro and Calvin Harris", "Madonna and LMFAO", "The Red Hot Chilli Peppers", "Biffy Clyro and Calvin Harris");
    questionsList.add(q6);
    Question q7 = new Question("By which alternative name was the revival known as?", "Rebellion", "Rock Island", "Big Day Out", "Rock Island");
    questionsList.add(q7);
    Question q8 = new Question("Who was the first headline act?", "Arthur Brown", "The Rolling Stones", "Jefferson Airplane", "Jefferson Airplane");
    questionsList.add(q8);
    //questions declared and initialised


        //object to create current question
        questionsObject = (Question) questionsList.get(questionNumber);

        //button ID's to link XML
        questionTextView = (TextView) findViewById(R.id.question_1);
        quizAnswerA = (RadioButton) findViewById(R.id.answerA);
        quizAnswerB = (RadioButton) findViewById(R.id.answerB);
        quizAnswerC = (RadioButton) findViewById(R.id.answerC);





        //method to call questions and answers
        setView();

        //on click listener
        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup1);
                RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
                if (questionsObject.getCorrectAnswer().equals(answer.getText())) {
                     score++;
                }

                if (questionNumber < questionsList.size()) {
                    questionsObject = questionsList.get(questionNumber);
                    setView();
                } else {
                    Intent intent = new Intent(getApplicationContext(), Results.class);
                    Bundle b = new Bundle();
                    b.putInt("score", score); //"score" is the key to reference the variable score.
                    intent.putExtras(b);
                    startActivity(intent);
                }
            }
        });


    }

    //method to set view of activity using the current object
    private void setView() {
        questionTextView.setText(questionsObject.getQuestion());
        quizAnswerA.setText(questionsObject.getAnswerA());
        quizAnswerB.setText(questionsObject.getAnswerB());
        quizAnswerC.setText(questionsObject.getAnswerC());
        questionNumber++;
    }

}

1 个答案:

答案 0 :(得分:0)

如果您希望随机提出问题的顺序,可以使用Collections.shuffle方法。一旦你添加了所有问题,就写下

Collections.shuffle(questionList);

要表示敬酒,你应该写:

if (questionsObject.getCorrectAnswer().equals(answer.getText())) {
    Toast.makeText(getApplicationContext(), 
                   "Correct!", 
                   Toast.LENGTH_SHORT)
                  .show();
    score++;
}
else {
    Toast.makeText(getApplicationContext(), 
                   "Sorry! Better luck next time.", 
                   Toast.LENGTH_SHORT)
                   .show();
}