【问题标题】:Radiogroup checked id getting wrong value -1Radiogroup 检查 id 得到错误值 -1
【发布时间】:2018-09-24 21:30:14
【问题描述】:

我想从RadioGroup 访问RadioButton

int selectedId = radioFeeGroup.getCheckedRadioButtonId();

radioFeeButton = (RadioButton) findViewById(selectedId);

我得到selectedId 值-1。这就是应用程序崩溃的原因。

【问题讨论】:

  • 确保您从组中选择了至少一个单选按钮。

标签: android radio-button


【解决方案1】:

确保您从组中选择了至少一个单选按钮。

See the Document

你的代码是正确的,

int selectedId = radioFeeGroup.getCheckedRadioButtonId();

radioFeeButton = (RadioButton) findViewById(selectedId);

如果您已经选择了RadioButton,那么清理项目并再次重建它如果您从 xml 正确访问RadioGroup,它应该可以工作。

【讨论】:

    【解决方案2】:

    检查一下

     radioFeeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        int radioButtonID = radioFeeGroup.getCheckedRadioButtonId();
                        View radioButton = radioFeeGroup.findViewById(radioButtonID);
                        int position = group.indexOfChild(radioButton);
                    }
                });
    

    查看此示例

    How to get the id of selected radio button in android?

    How to get id of selected radio button in a radio group

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      相关资源
      最近更新 更多