简体   繁体   English

如何获取从 Android 中的单选组检查的最后一个单选按钮?

[英]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.

相关问题 更改单选按钮选中状态时如何从单选按钮组中的单选按钮获取文本 - How to get the text from radio button in a radio group when radio button checked state is changed 我如何确保从单选按钮组中选择单选按钮会影响其他单选按钮组中单选按钮的选择。 - How do i ensure that the selection of a radio button from a radio group affects the selection of a radio button in a different radio group. 在 Android 中选中单选按钮时,如何取消选中单选组中的所有单选按钮? - How to uncheck all radio buttons in a radio group, when a radio button is checked in Android? 获取选中的单选按钮 - Get the checked radio button 如何设置选中的单选按钮状态。 不使用广播组 - How to set checked radio button state . not using radio group 如何获取在android中动态创建的单选按钮的id - How to get checked radio button's id created dynamically in android ANDROID - 如何从单选按钮获取文本并将其插入到 sqlite 数据库? - ANDROID - how do I get text from radio button and insert it to sqlite database? 我有一个广播组,在这个广播组中,我有两个单选按钮。 如何在Android中使单选按钮不可点击? - I have a radio group and in this radio group i have two radio buttons. how can i make Radio Button non-clickable in android? 如何在Android中的上一个按钮上设置选中的单选按钮? - How to set checked Radio Button on Previous Button in Android? 如何在android中更改单选按钮的值? - How do I change the radio button values in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM