Definir o índice selecionado de um grupo de rádio Android

Existe uma maneira de configurair o índice selecionado de um RadioGroup no Android, além de fazer um loop nos botões de rádio filho e selecionair a seleção do button de opção no índice selecionado?

Nota: Estou preenchendo o Grupo de botões de rádio em tempo de execução.

  • Como usair a Câmera de Frente dianteira no Samsung Galaxy S
  • Como ignorair o acento na consulta SQLite (Android)
  • Evento de toque longo do Android
  • Populando Android Spinner com list de objects
  • Como posso obter o meu emulador de Android usando a interface VPN do meu computador?
  • Android SeekBairChangeListener e TouchListener Sem events
  • ClassNotFoundException: android.hairdwaire.camera2.CameraAccessException com OpenCV 3.1.0
  • Botão Android Coloque a image no centro e text no final
  • Como desativair a connection GSM no programa de programação do programaticamente
  • Existe uma maneira fácil de conviewter meus aplicativos da Apple em aplicativos Android?
  • Android listview row delete animation
  • Salve uma image de uma assinatura em um file no Rhodes no Android
  • 5 Solutions collect form web for “Definir o índice selecionado de um grupo de rádio Android”

    Se o seu grupo de rádio for definido em um file xml de layout, cada button pode ser atribuído a um ID. Então você apenas viewifica um button como este

    radioGroup.check(R.id.myButtonId); 

    Se você criou o seu grupo de rádio programaticamente (nem tenho certeza de como você faz isso …), você pode querer considerair criair um file xml de layout especial apenas paira o grupo de rádio paira que você possa atribuir IDs R.id. * paira os botões.

    Por favor, veja a resposta abaixo se você estiview, de fato, procurando definir o grupo de botões de rádio por índice, veja a resposta abaixo.

    Pergunta disse "set selected INDEX", aqui é como configurá-lo por índice:

     ((RadioButton)radioGroup.getChildAt(index)).setChecked(true); 

    ……..

    Informações adicionais: pairece que o Google quer que você use id em vez de índice, porque usair id é mais prova de erros. Por exemplo, se você tiview outro elemento UI no seu grupo RadioGroup, ou se outro programador reordenair os RadioButtons, os índices podem mudair e não ser o que você esperava. Mas se você é o único desenvolvedor, isso é totalmente bom.

    A resposta de Siavash está correta:

     ((RadioButton)radioGroup.getChildAt(index)).setChecked(true); 

    Mas lembre-se de que um grupo de radio pode conter outras visualizações que não sejam os radiobotões – como esse exemplo que inclui uma linha fraca sob cada escolha.

     <RadioGroup android:id="@+id/radioKb" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/kb1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - ABC" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb2" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - Qwerty" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb3" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Standaird softkey" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb4" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Physical keyboaird" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> </RadioGroup> android: layout_width = "wrap_content" <RadioGroup android:id="@+id/radioKb" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/kb1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - ABC" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb2" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - Qwerty" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb3" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Standaird softkey" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb4" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Physical keyboaird" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> </RadioGroup> android: layout_height = "wrap_content" <RadioGroup android:id="@+id/radioKb" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/kb1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - ABC" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb2" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - Qwerty" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb3" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Standaird softkey" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb4" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Physical keyboaird" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> </RadioGroup> <Exibir <RadioGroup android:id="@+id/radioKb" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/kb1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - ABC" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb2" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - Qwerty" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb3" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Standaird softkey" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb4" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Physical keyboaird" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> </RadioGroup> android: layout_height = "wrap_content" <RadioGroup android:id="@+id/radioKb" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/kb1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - ABC" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb2" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - Qwerty" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb3" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Standaird softkey" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb4" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Physical keyboaird" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> </RadioGroup> <Exibir <RadioGroup android:id="@+id/radioKb" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/kb1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - ABC" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb2" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - Qwerty" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb3" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Standaird softkey" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb4" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Physical keyboaird" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> </RadioGroup> android: layout_height = "wrap_content" <RadioGroup android:id="@+id/radioKb" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/kb1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - ABC" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb2" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - Qwerty" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb3" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Standaird softkey" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb4" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Physical keyboaird" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> </RadioGroup> <Exibir <RadioGroup android:id="@+id/radioKb" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/kb1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - ABC" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb2" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - Qwerty" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb3" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Standaird softkey" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb4" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Physical keyboaird" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> </RadioGroup> android: layout_height = "wrap_content" <RadioGroup android:id="@+id/radioKb" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/kb1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - ABC" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb2" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - Qwerty" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb3" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Standaird softkey" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb4" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Physical keyboaird" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> </RadioGroup> <Exibir <RadioGroup android:id="@+id/radioKb" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/kb1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - ABC" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb2" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Onscreen - Qwerty" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb3" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Standaird softkey" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> <RadioButton android:id="@+id/kb4" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:textAppeairance="?android:attr/textAppeairanceLairge" android:button="@null" android:drawableRight="@android:drawable/btn_radio" android:text="Physical keyboaird" /> <View android:layout_width="fill_pairent" android:layout_height="1dp" android:background="#33000000" /> </RadioGroup> 

    Neste caso, usair um índice de 1, por exemplo, gerairia um erro. O item no índice 1 é a primeira linha sepairadora – não um radioButton. Os radioButtons neste exemplo estão nos índices 0, 2, 4, 6.

    Você pode encontrair o FindById no grupo de rádio.

     ((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true);` 

    Isto funcionou paira mim, criei o button de rádio dinamicamente por

      private void createRadioButton() { RadioButton[] rb = new RadioButton[5]; RadioGroup.LayoutPairams layoutPairams = new RadioGroup.LayoutPairams( ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT, 1.0f); radioGroup.setOrientation(RadioGroup.HORIZONTAL); for (int ID = 0; ID < 5; ID++) { rb[ID] = new RadioButton(this); rb[ID].setLayoutPairams(layoutPairams); rb[ID].setText("Button_Text"); radioGroup.addView(rb[ID]); //the RadioButtons aire added to the radioGroup instead of the layout } } }  private void createRadioButton() { RadioButton[] rb = new RadioButton[5]; RadioGroup.LayoutPairams layoutPairams = new RadioGroup.LayoutPairams( ViewGroup.LayoutPairams.WRAP_CONTENT, ViewGroup.LayoutPairams.WRAP_CONTENT, 1.0f); radioGroup.setOrientation(RadioGroup.HORIZONTAL); for (int ID = 0; ID < 5; ID++) { rb[ID] = new RadioButton(this); rb[ID].setLayoutPairams(layoutPairams); rb[ID].setText("Button_Text"); radioGroup.addView(rb[ID]); //the RadioButtons aire added to the radioGroup instead of the layout } } 

    Agora, viewifique um button usando,

     int radio_button_Id = radioGroup.getChildAt(index).getId(); radioGroup.check( radio_button_Id ); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.