[英]How do i get the last radio button that was checked from a radio group in Android?
I have created programmatically 4 radio groups with 4 radio buttons.我以编程方式创建了 4 个带有 4 个单选按钮的单选组。 Each radio button represents an answer to a question.
每个单选按钮代表一个问题的答案。 Let's say that we have in the first radio group, 3 wrong answers and only one correct answer.
假设我们在第一个无线电组中有 3 个错误答案,只有一个正确答案。 The first, the second and the third are wrong and the fourth is correct.
第一个、第二个和第三个是错误的,第四个是正确的。 When someone checks only one radio button, let say for example the first one, which is a wrong one, this goes to the
wrongAnswersRadios
array list.当有人只检查一个单选按钮时,例如第一个是错误的,这将转到
wrongAnswersRadios
数组列表。 But if someone checks the first one and then is changing his mind and checks the last one (the correct one) than the first one that was checked goes to wrongAnswersRadios
array list and the last one that was checked, goes to correctAnswerRadios array list.但是,如果有人检查第一个然后改变主意并检查最后一个(正确的),那么第一个检查到
wrongAnswersRadios
数组列表,而最后一个检查的则转到正确的wrongAnswersRadios
数组列表。 I think onClick
fires every time a radio button is checked.我认为每次选中单选按钮时都会触发
onClick
。
How do i manage to add in the array lists only the last checked radio button from each radio group?我如何设法在数组中只添加每个单选组中最后一个选中的单选按钮? Here is my code:
这是我的代码:
correctAnswerRadios = new ArrayList<>();
wrongAnswersRadios = new ArrayList<>();
radioGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int j = 0; j < 4; j++) {
checkedRadioButton[j] = ((RadioButton) v);
if (checkedRadioButton[j].isChecked() & CorrectAnswer == 1) {
correctAnswerRadios.add(checkedRadioButton[j]);
} else {
wrongAnswersRadios.add(checkedRadioButton[j]);
}
}
}
});
You can do something like this:你可以这样做:
correctAnswerRadios = new ArrayList<>();
wrongAnswersRadios = new ArrayList<>();
radioGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int j = 0; j < checkedRadioButton.length; j++) {
correctAnswerRadios.remove(checkedRadioButton[j]);
wrongAnswersRadios.remove(checkedRadioButton[j]);
}
for (int j = 0; j < checkedRadioButton.length; j++) {
if (checkedRadioButton[j].isChecked() & CorrectAnswer == 1) {
correctAnswerRadios.add(checkedRadioButton[j]);
} else {
wrongAnswersRadios.add(checkedRadioButton[j]);
}
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.