【问题标题】:How to get text from checked radio button in a group [duplicate]如何从组中选中的单选按钮获取文本[重复]
【发布时间】:2021-10-11 21:08:34
【问题描述】:

我正在尝试从组中选定的单选按钮中获取文本。我已经搜索过同样的问题,但我得到的解决方案仍然让我遇到同样的错误。

2021-08-07 11:14:14.464 7297-7297/com.findplaceslaxco.ecommerce W/System.err:     at com.findplaceslaxco.ecommerce.Adapters.SecondCartAdapter$ViewHolder$2$1.onCheckedChanged(SecondCartAdapter.java:276)

这是代码:

mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId != -1){
            MaterialRadioButton radioButton = (MaterialRadioButton) group.findViewById(checkedId);
            boolean isChecked = radioButton.isChecked();//this is where the error appears
        }

        }
    }
});

【问题讨论】:

  • 试试看这个StackOverflow链接它会帮助你
  • 谢谢大家的回复,我发现虽然仍然显示单选按钮,但 childrenCount 组是零。

标签: java android


【解决方案1】:

通过 id 查找 RadioButton 并获取文本

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
       @Override
       public void onCheckedChanged(RadioGroup group, int checkedId)
       {
           radioButton = (RadioButton) findViewById(checkedId);
           Toast.makeText(getBaseContext(), radioButton.getText(), Toast.LENGTH_SHORT).show();
       }
   }
   );

【讨论】:

    猜你喜欢
    • 2018-11-25
    • 2013-12-06
    • 1970-01-01
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 2011-05-16
    相关资源
    最近更新 更多