我有一个动态创建的多项选择测试。
每按一次按钮,它就会显示一个不同的随机问题。此外,我将正确的答案以及三个错误的答案放入一个数组并将它们放入单选按钮。
这个工作到目前为止,每次你点击按钮它会显示一个新的,随机的问题,所有的答案都被扰乱。
我现在要做的是检查选中的答案,而不是我放入单选按钮的正确答案。我可以成功检索所选单选按钮的ID。
现在我需要针对正确答案检查该单选按钮,该单选按钮存储在字符串变量" y" 中。我该怎么做?
我虽然可以将radiobutton的文本返回到字符串中,然后将其与String y匹配。我无法将该radiobutton的文本变成字符串。这是可能的,还是有不同的方法可以做到这一点?我的代码如下:
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 :(得分:0)
首先,我认为你的游戏/应用程序中有一个明确的逻辑很麻烦。
为什么你不想使用OOP?
然而,让我们谈谈你的问题。
我无法将该radiobutton的文本变成字符串。
为什么遇到麻烦?究竟你能做什么?
answerOne.setText(randomizeOrderOfAnswers1);
假设randomizeOrderOfAnswers1
&#39; Y&#39; 。
因此,您可以选择正确的答案或不是用户选择。
boolean isAnswerCorrect = answerOne.getText().toString().equals("Y");
并在......之后进行比较。
记住 - toString方法总是可以访问的。这是Object类的方法。它总是被呈现出来。