Android:我怎么知道测验中是否点击了正确的按钮?

时间:2013-03-08 03:45:25

标签: android multiple-choice

好吧,我是Java编程的新手,并且遵循我在这里看到的一些技巧,我将这段代码用于测验游戏:

public class OtherActivity extends Activity {


    TextView textView1, textView2;
    Button btn1, btn2, btn3, btn4;

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

        textView1 = (TextView) findViewById(R.id.textView1);
        textView2 = (TextView) findViewById(R.id.textView2);

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);


        final ArrayList<Question> qsts = new ArrayList<Question>();
        qsts.add(Question.q1);
        qsts.add(Question.q2);
        qsts.add(Question.q3);
        qsts.add(Question.q4);
        qsts.add(Question.q5);
        qsts.add(Question.q6);

        final Random rng = new Random();

        final List<Integer> generated = new ArrayList<Integer>();
        int nxt = rng.nextInt(6);

                    generated.add(nxt);

                    final Question nextQuestion = qsts.get(nxt);

                    textView1.setText(nextQuestion.questionText);

                    final ArrayList<String> allAnswers = new ArrayList<String>();
                    allAnswers.add(nextQuestion.correctAnswerText);
                    allAnswers.add(nextQuestion.wrongAnswer1);
                    allAnswers.add(nextQuestion.wrongAnswer2);
                    allAnswers.add(nextQuestion.wrongAnswer3);

                    Collections.shuffle(allAnswers);

                    btn1.setText(allAnswers.get(0));
                    btn2.setText(allAnswers.get(1));
                    btn3.setText(allAnswers.get(2));
                    btn4.setText(allAnswers.get(3));

                    btn1.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            if(allAnswers.get(0) == nextQuestion.correctAnswerText){
                                textView2.setText("CORRECT ANSWER, MAN!");

                                while(true){

                                    int nxt = rng.nextInt(6);

                                    if (!generated.contains(nxt)){

                                        generated.add(nxt);

                            Question nextQuestion = qsts.get(nxt);

                            textView1.setText(nextQuestion.questionText);

                            ArrayList<String> allAnswers = new ArrayList<String>();
                            allAnswers.add(nextQuestion.correctAnswerText);
                            allAnswers.add(nextQuestion.wrongAnswer1);
                            allAnswers.add(nextQuestion.wrongAnswer2);
                            allAnswers.add(nextQuestion.wrongAnswer3);

                            Collections.shuffle(allAnswers);

                            btn1.setText(allAnswers.get(0));
                            btn2.setText(allAnswers.get(1));
                            btn3.setText(allAnswers.get(2));
                            btn4.setText(allAnswers.get(3));

                                        break;
                                    }
                                }

                            }
                            else{
                                textView2.setText("WRONG ANSWER, MAN!");

                                while(true){

                                    int nxt = rng.nextInt(6);

                                    if (!generated.contains(nxt)){

                                        generated.add(nxt);

                            //  ---->   Integer nxt = rng.nextInt(6); <---- random nxt aqui!

                            Question nextQuestion = qsts.get(nxt);

                            textView1.setText(nextQuestion.questionText);

                            ArrayList<String> allAnswers = new ArrayList<String>();
                            allAnswers.add(nextQuestion.correctAnswerText);
                            allAnswers.add(nextQuestion.wrongAnswer1);
                            allAnswers.add(nextQuestion.wrongAnswer2);
                            allAnswers.add(nextQuestion.wrongAnswer3);

                            Collections.shuffle(allAnswers);

                            btn1.setText(allAnswers.get(0));
                            btn2.setText(allAnswers.get(1));
                            btn3.setText(allAnswers.get(2));
                            btn4.setText(allAnswers.get(3));

                                        break;
                                    }
                                }

                            }
                        }
                    });

                    //
                    // AND THE SAME METHOD TO THE BUTTONS btn2, btn3, btn4
                    //
                    // btn2 with a if (allAnswers1.get(1) == nextQuestion.correctAnswerText) { ...
                    // btn3 with a if (allAnswers1.get(2) == nextQuestion.correctAnswerText) { ...
                    // btn4 with a if (allAnswers1.get(3) == nextQuestion.correctAnswerText) { ...
                    //

    }
}

我还有其他类代码:

    public class Question {

    String questionText;
    String correctAnswerText;       
    String wrongAnswer1;
    String wrongAnswer2;
    String wrongAnswer3;


    Question (String qst, String cAns, String wAns1, String wAns2, String wAns3){

        questionText = qst;
        correctAnswerText = cAns;
        wrongAnswer1 = wAns1;
        wrongAnswer2 = wAns2;
        wrongAnswer3 = wAns3;

    }

    static Question q1 = new Question(
            "Question 1",

            "Correct answer - question 1",
            "Wrong Answer 1 - question 1",
            "Wrong Answer 2 - question 1",
            "Wrong Answer 3 - question 1"
            );
    static Question q2 = new Question(
            "Question 2",

            "Correct answer - question 2",
            "Wrong Answer 1 - question 2",
            "Wrong Answer 2 - question 2",
            "Wrong Answer 3 - question 2"
            );
    // ...
    // and the same with q3, q4, q5 and q6
    // ...

那么,你怎么看,我在确定正确答案方面遇到了问题。在第一个问题,它工作正常,但不是在此之后。我相信这个问题的发生是因为当我尝试比较allAnswers.get(0) == nextQuestion.correctAnswerText或者与ArrayList allAnswers的其他索引如(1),(2)或(3)时,为比较选择的“时段”仍然有第一个问题文字答案。那么,我该怎么办?

* Plus:代码变得非常大,我试图在onClickListener中放置if并与4个按钮的id进行比较,但它必须与其他函数一起使用,在外面onCreate,'因为ArrayList qsts (包含Question对象)没有被声明,我无法比较按钮点击的原因我必须声明一个不同的ArrayList。有更好的方法来做我想要的事情吗?

4 个答案:

答案 0 :(得分:0)

对于拥有大代码的问题,我建议你单独使用onclicklistener并将button.setonclicklistener放在所有4个按钮中。

然后在点击监听器上,您将视图作为参数,该视图是一个按下的按钮。将其转换为按钮,然后获取按钮文本。那么你可以设置你在按钮动作中实现的其他领导者。

设置不同的方法来设置新问题,这样你只需要调用它,而不是多次编写相同的代码。

我希望它能解决你的两个问题。

答案 1 :(得分:0)

按照vikrant所说的说明进行操作。

此外,在用户选择任何选项时,请设置一个布尔变量,该值将更改。

通过检查布尔值,您可以获得用户是否已回答。

要检查相应的答案,您必须将所选值与正确的值进行比较。

享受编码。 :)

答案 2 :(得分:0)

听到的是vikrant所说的例子

public class MyActivity extends Activity implements OnClickListener {

Question nextQuestion;

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

@Override
public void onClick(View v) {
Button b = (Button)v;
String buttonText = b.getText().toString();

   if(buttonText.equals( nextQuestion.correctAnswerText ))
   { 
      DoSomething();
      return;
   }else{

   }
}

并且别忘了添加

Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(this);

答案 3 :(得分:0)

如果您想创建测验应用程序,请检查此LINK

另请查看此链接以了解existing database with an Android application

的使用情况

如果您有任何问题,请告诉我。

祝你好运。