How to get the selected index of a RadioGroup in Android
Как получить выбранный индекс радиогруппы в Android
Есть ли простой способ получить выбранный индекс радиогруппы в Android или я должен использовать OnCheckedChangeListener для прослушивания изменений и иметь что-то, что содержит последний выбранный индекс?
Вы могли бы иметь ссылку на радиогруппу и использовать getCheckedRadioButtonId () для получения идентификатора отмеченного переключателя. Взгляните сюда
Затем, когда вам нужно получить выбранную опцию радио.
int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId(); if (checkedRadioButtonId == -1) { // No item selected } else{ if (checkedRadioButtonId == R.id.radio_button1) { // Do something with the button } }
Ответ 4
попробуйте это
RadioGroup group= (RadioGroup) getView().findViewById(R.id.radioGroup); group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override publicvoidonCheckedChanged(RadioGroup radioGroup, int i) { View radioButton = radioGroup.findViewById(i); int index = radioGroup.indexOfChild(radioButton); } });