[英]How to find out which radiobutton has been selected by the user for comparing with the correct one in QuestionApp in Android
這里有問題,
我想找出用戶選擇的 RadioButton 並將其與正確的 RadioButton 進行比較。 我有 3 個問題,它的答案在一個 ArrayList 中。 當我做一個日志找出哪個被選中時,這是output:
2020-11-30 23:12:19.128 5696-5696/com.example.test1 E/Question: Where is Iran ? Asia
2020-11-30 23:12:19.128 5696-5696/com.example.test1 E/Question: Where is Sweden ? Europe
2020-11-30 23:12:19.128 5696-5696/com.example.test1 E/Question: Where is Mexico ? Europe
第三題的答案,沒有保存,第二題的答案將保存到第三題。 請幫我。 我應該怎么辦
這是我的問題代碼:
QuestionModel q1 = new QuestionModel();
q1.setText("Where is Iran ?");
q1.setOp1("Asia");
q1.setOp2("Europe");
q1.setOp3("America");
q1.setOp4("Africa");
q1.setTrue_ans("Asia");
q1.setScore(5);
questions.add(q1);
QuestionModel q2 = new QuestionModel();
q2.setText("Where is Sweden ?");
q2.setOp1("America");
q2.setOp2("Africa");
q2.setOp3("Asia");
q2.setOp4("Europe");
q2.setTrue_ans("Europe");
q2.setScore(3);
questions.add(q2);
QuestionModel q3= new QuestionModel();
q3.setText("Where is Mexico ?");
q3.setOp1("Africa");
q3.setOp2("America");
q3.setOp3("Europe");
q3.setOp4("Asia");
q3.setTrue_ans("America");
q3.setScore(4);
questions.add(q3);
這就是我到達用戶選擇的單選按鈕的方式:
if (radioGp.getCheckedRadioButtonId() != -1) {
QuestionModel model = questions.get(con);
int id = radioGp.getCheckedRadioButtonId();
RadioButton useranswer = findViewById(id);
model.setUser_ans(useranswer.getText().toString());
}
這是日志和其他內容:
if (con == 2) {
nextbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (QuestionModel q : questions) {
Log.e("Question ", q.getText() + " " + q.getUser_ans());
String user_ans = q.getUser_ans();
String true_ans = q.getTrue_ans();
if( true_ans.equals(user_ans) ) {
score += q.getScore();
}
}
scorebox.setText(Integer.toString(score));
}
});
}
為了使其盡可能簡單,這是您可以依賴的解決方案:
<RelativeLayout
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/question_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="7dp"
android:text="Where is Iran ?"
android:textSize="20sp" />
<RadioGroup
android:layout_below="@+id/question_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/radioGroup">
<RadioButton
android:id="@+id/radio_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Asia"/>
<RadioButton
android:id="@+id/radio_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Europe"/>
<RadioButton
android:id="@+id/radio_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="America"/>
<RadioButton
android:id="@+id/radio_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Africa"/>
</RadioGroup>
<TextView
android:id="@+id/text_view_selected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:text="Your answer is: "
android:textSize="20sp" />
<Button
android:id="@+id/button_apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text_view_selected"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:text="apply" />
</RelativeLayout>
在onCreate里面的MainActivity中:
// the RadioGroup holding the radio buttons
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
// the text that will display the result
textView = (TextView) findViewById(R.id.text_view_selected);
// the button that will check if the selected answer is correct or wrong
Button buttonApply = (Button) findViewById(R.id.button_apply);
buttonApply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// This gets the id of the selected radio button BY THE USER
int radioId = radioGroup.getCheckedRadioButtonId();
RadioButton selectedAnswer = (RadioButton) findViewById(radioId);
// THIS IS YOUR CORRECT ANSWER
// if the correct answer is Africa then R.id.radio_four etc
RadioButton correctAnswerButton = (RadioButton) findViewById(R.id.radio_one);
// this gets the text of your correct answer
CharSequence correctAnswer = correctAnswerButton.getText();
// NOW compare the selected answer to the correct answer
if (selectedAnswer.getText() == correctAnswer) {
textView.setText("Your answer is right");
} else {
textView.setText( "Your answer is wrong " );
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.