2015-06-27 42 views
0

我有一个动态创建的多项选择测试。
每次按下按钮时,都会显示一个不同的随机问题。另外,我将正确答案以及三个错误答案放入数组中,并将它们放入单选按钮中。如何将所选单选按钮的文本返回字符串

这种方式至今在每次单击按钮时都会显示一个新的随机问题,其中所有答案都是乱码。

我现在想要做的是检查选择的答案与我放入单选按钮的正确答案。我可以成功检索选定的单选按钮的ID。

现在我需要检查存储在字符串变量“y”中的正确答案的单选按钮。我将如何做到这一点?

虽然我可以将单选按钮的文本返回到一个字符串,然后匹配字符串y。我无法将该单选按钮的文本转换为字符串。这是可能的还是有不同的方式我可以做到这一点?我的代码如下:

所有的
public void nextQuestion(View view){ 
    TextView questionsTextView = (TextView)findViewById(R.id.questions); 
    RadioButton answerOne = (RadioButton)findViewById(R.id.answerOne); 
    RadioButton answerTwo = (RadioButton)findViewById(R.id.answerTwo); 
    RadioButton answerThree = (RadioButton)findViewById(R.id.answerThree); 
    RadioButton answerFour = (RadioButton)findViewById(R.id.answerFour); 

    Button next = (Button)findViewById(R.id.next); 

    int i = questions.size(); 
    if(i == 0){ 
     questionsTextView.setText("You have finished the Test!"); 
     next.setText("Submit"); 
    } 
    else { 
     //Show Radio Buttons 
     answerOne.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT; 
     answerTwo.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT; 
     answerThree.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT; 
     answerFour.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT; 
     //Pull Random Question 
     Random randomGenerator = new Random(); 
     String randQuestion = questions.get(randomGenerator.nextInt(questions.size())); 
     //Get index number of randomly pulled question 
     int x = questions.indexOf(randQuestion); 
     //Remove randomly selected question from array 
     questions.remove(randQuestion); 
     //Show randomly selected question 
     questionsTextView.setText(randQuestion); 
     //Place correct answer into string "y" 
     String y = answers.get(x); 
     //Create array to hold answers 
     List<String> answersForQuestion = new ArrayList<>(); 
     //Add right answer to answersForQuestion array 
     answersForQuestion.add(y); 
     //Add wrong answers to answersForQuestions array 
     String randWrongAnswers1 = wrongAnswers.get(randomGenerator.nextInt(wrongAnswers.size())); 
     answersForQuestion.add(randWrongAnswers1); 
     wrongAnswers.remove(randWrongAnswers1); 
     String randWrongAnswers2 = wrongAnswers.get(randomGenerator.nextInt(wrongAnswers.size())); 
     answersForQuestion.add(randWrongAnswers2); 
     wrongAnswers.remove(randWrongAnswers2); 
     String randWrongAnswers3 = wrongAnswers.get(randomGenerator.nextInt(wrongAnswers.size())); 
     answersForQuestion.add(randWrongAnswers3); 
     wrongAnswers.remove(randWrongAnswers3); 
     //Randomize all answers 
     String randomizeOrderOfAnswers1 = answersForQuestion.get(randomGenerator.nextInt(answersForQuestion.size())); 
     answerOne.setText(randomizeOrderOfAnswers1); 
     answersForQuestion.remove(randomizeOrderOfAnswers1); 
     String randomizeOrderOfAnswers2 = answersForQuestion.get(randomGenerator.nextInt(answersForQuestion.size())); 
     answerTwo.setText(randomizeOrderOfAnswers2); 
     answersForQuestion.remove(randomizeOrderOfAnswers2); 
     String randomizeOrderOfAnswers3 = answersForQuestion.get(randomGenerator.nextInt(answersForQuestion.size())); 
     answerThree.setText(randomizeOrderOfAnswers3); 
     answersForQuestion.remove(randomizeOrderOfAnswers3); 
     String randomizeOrderOfAnswers4 = answersForQuestion.get(randomGenerator.nextInt(answersForQuestion.size())); 
     answerFour.setText(randomizeOrderOfAnswers4); 
     answersForQuestion.remove(randomizeOrderOfAnswers4); 
     //Remove answer from array 
     answers.remove(y); 
     //Add users answer to array 
     RadioGroup g = (RadioGroup)findViewById(R.id.radioGroup); 
     int selected = g.getCheckedRadioButtonId(); 
     String selectedString = Integer.toString(selected); 

     questionsTextView.setText(selectedString); 

     /*// Gets a reference to our radio group 
     // rBtnDigits is the name of our radio group (code not shown) 
     RadioGroup g = (RadioGroup) findViewById(R.id.rBtnDigits); 

     // Returns an integer which represents the selected radio button's ID 
     int selected = g.getCheckedRadioButtonId(); 

     // Gets a reference to our "selected" radio button 
     RadioButton b = (RadioButton) findViewById(selected); 

     // Now you can get the text or whatever you want from the "selected" radio button 
     b.getText();*/ 



     next.setText("Next Question"); 
    } 
+0

radiobutton.getText()。toString() –

回答

0

首先我觉得你有你的游戏/应用逻辑清晰带来了很大麻烦。
为什么你不想使用OOP?

但让我们来谈谈你的问题。

我无法将该单选按钮的文本转换为字符串。

为什么你有麻烦?你究竟做不到什么?

answerOne.setText(randomizeOrderOfAnswers1); 

假设randomizeOrderOfAnswers1'Y'
所以你可以检查正确的答案或不用用户选择。

boolean isAnswerCorrect = answerOne.getText().toString().equals("Y"); 

和比较后...

记住 - toString方法总是给你访问。这是Object类的一个方法。它总是呈现。

+0

嗯,我还是很新的编程,我喜欢自己搞清楚事情。我拥有的代码就是我所想的。当然几乎从来都不是最有效的方式。你能告诉我还是将我链接到OOP的教程?我不知道那是什么。至于你的答案,我会尝试并回复你。 – th3ramr0d

+0

@ th3ramr0d https://en.wikipedia.org/wiki/Object-oriented_programming –

相关问题