2011-06-22 9 views
166

Czy istnieje prosty sposób na uzyskanie wybranego indeksu grupy radiowej w systemie Android lub czy muszę używać OnCheckedChangeListener do odsłuchiwania zmian i mieć coś, co zawiera ostatni wybrany indeks?Jak uzyskać wybrany indeks grupy radiowej w systemie Android?

przykład xml:

<RadioGroup android:id="@+id/group1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> 
    <RadioButton android:id="@+id/radio1" android:text="option 1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
    <RadioButton android:id="@+id/radio2" android:text="option 2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
    <RadioButton android:id="@+id/radio3" android:text="option 3" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
    <RadioButton android:id="@+id/radio4" android:text="option 4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
    <RadioButton android:id="@+id/radio5" android:text="option 5" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
</RadioGroup> 

jeśli użytkownik wybierze option 3 chcę uzyskać indeks, 2.

Odpowiedz

395

Powinieneś być w stanie zrobić coś takiego:

int radioButtonID = radioButtonGroup.getCheckedRadioButtonId(); 
View radioButton = radioButtonGroup.findViewById(radioButtonID); 
int idx = radioButtonGroup.indexOfChild(radioButton); 

Jeśli RadioGroup zawiera inne widoki (jak TextView) to metoda indexOfChild() powróci niewłaściwy indeks.

aby wybrany RadioButton Tekst na radiogroup

RadioButton r = (RadioButton) radioButtonGroup.getChildAt(idx); 
String selectedtext = r.getText().toString(); 
+7

Ale co, jeśli te przyciski nie mają ustawionych atrybutów "Android: id"? – neuront

+0

@ BP Mam takie same wątpliwości w dostępie do przycisków opcji, gdy nie jest ustawiony żaden z nadrzędnych lub ID przycisków radiowych. – Killer

+0

@neuront Tak długo, jak robisz radioGroup.findViewById (radioButtonID), będzie działać. RadioGroup ma ustawione 1, 2, 3, 4, i tak dalej jako identyfikatory widok, więc jeśli nie szukać ich w terminie to kontekst, to uda – Reinherd

49

Można mieć odniesienie do grupy radiowej i użyj getCheckedRadioButtonId(), aby uzyskać zaznaczony identyfikator przycisku radiowego. Spójrz here

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radio_group); 

Wtedy, gdy trzeba uzyskać wybraną opcję radiową.

int checkedRadioButtonId = radioGroup.getCheckedRadioButtonId(); 
if (checkedRadioButtonId == -1) { 
    // No item selected 
} 
else{ 
    if (checkedRadioButtonId == R.id.radio_button1) { 
     // Do something with the button 
    } 
} 
+0

tak, jest to identyfikator sprawdzanego przycisku radiowego, ale co z indeksem przycisku radiowego w grupie radiowej? –

+0

mogę uzyskać identyfikator, chcę indeks, są różne. –

+0

co masz na myśli przez indeks? czy jest to widok listy? –

97

To powinno działać,

int index = myRadioGroup.indexOfChild(findViewById(myRadioGroup.getCheckedRadioButtonId())); 
+2

Użyj funkcji getActivity(). FindViewById(), jeśli używasz w Fragmentie. –

9

Można użyć:

RadioButton rb = (RadioButton) findViewById(rg.getCheckedRadioButtonId()); 
5

// użyć, aby uzyskać identyfikator wybranego elementu

int selectedID = myRadioGroup.getCheckedRadioButtonId(); 

// uzyskać widok wybranego elementu

View selectedView = (View)findViewById(selectedID); 
2
int index_selected = radio_grp.indexOfChild(radio_grp 
       .findViewById(radio_grp.getCheckedRadioButtonId())); 
18

spróbować tej

 RadioGroup group= (RadioGroup) getView().findViewById(R.id.radioGroup); 
     group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup radioGroup, int i) { 
       View radioButton = radioGroup.findViewById(i); 
       int index = radioGroup.indexOfChild(radioButton); 
      } 
     }); 
1

można zrobić

findViewById 

z grupy radiowej.

Tu jest próbka:

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

To działało idealnie dla mnie w ten sposób:

RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group); 
    int radioButtonID = radioGroup.getCheckedRadioButtonId(); 
    RadioButton radioButton = (RadioButton) radioGroup.findViewById(radioButtonID); 
    String selectedtext = (String) radioButton.getText(); 
3

co potrzebne jest, aby ustawić wartości do RadioButton, na przykład:

RadioButton radioButton = (RadioButton)findViewById(R.id.radioButton);  
radioButton.setId(1);  //some int value 

, a następnie za każdym razem, gdy wybrany zostanie ten spacial radioButton, można pobrać jego wartość przez identyfikator, który mu podałeś z

RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);      
int whichIndex = radioGroup.getCheckedRadioButtonId(); //of course the radioButton 
                 //should be inside the "radioGroup" 
                 //in the XML 

Pozdrawiam!

1

użyć po prostu:

int index = 2; 
    boolean option3Checked = radioGroup.getCheckedRadioButtonId() == radioGroup.getChildAt(2).getId(); 
3

radioSexGroup = (RadioGroup) findViewById (R.id.radioGroup);

btnDisplay=(Button)findViewById(R.id.button); 

    btnDisplay.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     int selectedId=radioSexGroup.getCheckedRadioButtonId(); 
     radioSexButton=(RadioButton)findViewById(selectedId); 
     Toast.makeText(MainActivity.this,radioSexButton.getText(),Toast.LENGTH_SHORT).show(); 
    } 
    }); 
Powiązane problemy