【问题标题】:How to check RadioButton is checked when added dynamically?动态添加时如何检查 RadioButton 是否被选中?
【发布时间】:2017-07-21 22:25:10
【问题描述】:

在我的应用程序中,我根据要求动态添加 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);

现在我想将 ma​​rks 的值增加 2 个,即使 RadioButton 被多次选中。为此,我想知道 RadioButton 是否被选中。 这个怎么办??

请帮忙!!

【问题讨论】:

标签: android radio-button radio-group


【解决方案1】:

简单的事情,

为什么不用OnClickListener 而不是onCheckedChanged>

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

【讨论】:

    猜你喜欢
    • 2016-02-19
    • 2016-07-03
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-12
    相关资源
    最近更新 更多