动态添加时如何检查RadioButton是否被选中?

How to check RadioButton is checked when added dynamically?

在我的应用程序中,我根据需要动态添加 RadioButton。 下面是代码:

RadioGroup rg = new RadioGroup(StartExamActivity.this);
            View v = new View(StartExamActivity.this);
            v.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2));
            v.setBackgroundColor(Color.parseColor("#3593D4"));
            rg.setLayoutParams(new RadioGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            rg.setOrientation(LinearLayout.VERTICAL);
            for (int i = 0; i < list.size(); i++) {
                rdbtn[i] = new RadioButton(StartExamActivity.this);
                rdbtn[i].setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT, 1f));
                rdbtn[i].setId(i);
                rdbtn[i].setText(list.get(i));
                final String value = rdbtn[i].getText().toString();

                rdbtn[i].setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        if (value.equalsIgnoreCase(answer)) {
                            marks = marks + 2;
                        }
                    }
                });
                rg.addView(rdbtn[i]);
            }
            questionContainer.addView(rg);

现在我想将 marks 的值增加 2 个,即使 RadioButton 被选中多次也是如此。为此,我想知道 RadioButton 是否被选中。 如何做到这一点??

请帮忙!!

简单的东西,

你为什么不使用 OnClickListener 而不是 onCheckedChanged>

rdbtn[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                        if(isChecked) {
                           // Write your logic here...
                        }
                    });