2

我正在创建一个包含7 个 RadioGroups的活动,每个带有5 个 RadioButtons。我的问题是我只获得了一个 RadioGroup (lvl_mon) 的选定索引,而其他 6 个返回-1,但是它们被选中。我的代码是:

lvl_mon = (RadioGroup)findViewById(R.id.lvl_monday);
lvl_tue = (RadioGroup)findViewById(R.id.lvl_tuesday);
lvl_wed = (RadioGroup)findViewById(R.id.lvl_wednesday);
lvl_thu = (RadioGroup)findViewById(R.id.lvl_thursday);
lvl_fri = (RadioGroup)findViewById(R.id.lvl_friday);
lvl_sat = (RadioGroup)findViewById(R.id.lvl_saturday);
lvl_sun = (RadioGroup)findViewById(R.id.lvl_sunday);

int index_mon = lvl_mon.indexOfChild(findViewById(lvl_mon.getCheckedRadioButtonId()));
int index_tue = lvl_tue.indexOfChild(findViewById(lvl_tue.getCheckedRadioButtonId()));
int index_wed = lvl_wed.indexOfChild(findViewById(lvl_wed.getCheckedRadioButtonId()));
int index_thu = lvl_thu.indexOfChild(findViewById(lvl_thu.getCheckedRadioButtonId()));
int index_fri = lvl_fri.indexOfChild(findViewById(lvl_fri.getCheckedRadioButtonId()));
int index_sat = lvl_sat.indexOfChild(findViewById(lvl_sat.getCheckedRadioButtonId()));
int index_sun = lvl_sun.indexOfChild(findViewById(lvl_sun.getCheckedRadioButtonId()));

在此处输入图像描述

4

4 回答 4

1

如果您的收音机组中有按钮,那么您可以执行以下操作:

RadioGroup lvl_mon = (RadioGroup) findViewById(R.id.lvl_monday);
String selected_mon = ((RadioButton) findViewById(lvl_mon .getCheckedRadioButtonId())).getText().toString();
于 2014-09-08T12:21:55.343 回答
1

我发现了问题,我在每个 RadioGroup 中都有相同的 RadioButtons id,所以我更改了 id 并清理了项目。这解决了问题

于 2014-09-08T14:00:07.673 回答
0

你试过这样

lvl_mon = (RadioGroup)findViewById(R.id.lvl_monday);
RadioButton lvl_mon1;

     lvl_mon.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub

           lvl_mon1= (RadioButton)findViewById(checkedId);

           switch(checkedId){
           case R.id.radio0:
              // do somthings
           break;

           case R.id.radio1:
           .......

          }
        }
    });
于 2014-09-08T12:22:09.860 回答
0
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);

这可能会有所帮助!

于 2014-09-08T12:29:20.457 回答