我正在创建一个活动,其中包含 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()));
答案 0 :(得分:1)
如果您的电台组内有按钮,则可以执行以下操作:
RadioGroup lvl_mon = (RadioGroup) findViewById(R.id.lvl_monday);
String selected_mon = ((RadioButton) findViewById(lvl_mon .getCheckedRadioButtonId())).getText().toString();
答案 1 :(得分:1)
我想出了问题,我在每个RadioGroup中都有相同的Radio按钮ID,所以我更改了ID并清理了项目。这解决了问题
答案 2 :(得分: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:
.......
}
}
});
答案 3 :(得分:0)
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
这可能会有所帮助!!