Learn how to write RadioGroup and Radio in xml file
In Java code use function to get which radio button been selected.
// sex.xml <RadioGroup android:id="@+id/radioGroupSex" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radioPatSex1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/sex_val1" /> <RadioButton android:id="@+id/radioPatSex2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sex_val2" /> </RadioGroup>
// sex.java
public static int getRadioIndex(RadioGroup g)
{
int radioButtonID = g.getCheckedRadioButtonId();
View radioButton = g.findViewById(radioButtonID);
int idx = g.indexOfChild(radioButton);
Log.v("debug","idx="+idx);
return idx;
}
RadioGroup gRadio = (RadioGroup) findViewById(R.id.radioGroup1);
int selected = getRadioIndex(gRadio);

0 意見:
張貼留言