设置Android RadioGroup的选定索引

Has*_*dad 81 android android-radiogroup android-radiobutton

有没有办法在android中设置RadioGroup的选定索引,除了循环子单选按钮并选择检查所选索引处的单选按钮?

注意:我在运行时填充单选按钮组.

jjm*_*jjm 183

如果您的广播组是在布局xml文件中定义的,则可以为每个按钮分配一个ID.然后你只需检查这样一个按钮

radioGroup.check(R.id.myButtonId);
Run Code Online (Sandbox Code Playgroud)

如果您以编程方式创建了无线电组(我甚至不确定您是如何做到这一点的......),您可能需要考虑为无线电组创建一个特殊的布局xml文件,以便您可以分配R.id.*ID按钮.

实际上,如果您希望按索引设置单选按钮组,请参阅下面的答案,请参阅下面的答案.

  • 嗨,我使用以下代码以编程方式创建它们:for(int i = 0; i <lookupTypes.size(); i ++){// RadioButton rbt = new RadioButton(this); //,null,// R.style. RadioGroupItem); RadioButton rbt =(RadioButton)getLayoutInflater().inflate(R.layout.tmpltradiobutton,null); rbt.setId(ⅰ); rbt.setText(lookupTypes.get(ⅰ).getValue()); rbt.setTag(lookupTypes.get(I)); rbtnGroup.addView(RBT); } (2认同)
  • 老实说,我感到有点尴尬,因为这是我投票最高的 SO 答案。 (2认同)

Sia*_*ash 85

问题说"设置选定的INDEX",这里是如何通过索引设置它:

((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
Run Code Online (Sandbox Code Playgroud)

........

附加信息:似乎Google希望您使用id而不是index,因为使用id更具有bug证明.例如,如果您的RadioGroup中有另一个UI元素,或者另一个开发人员重新命令RadioButtons,则索引可能会更改而不是您所期望的.但如果你是唯一的开发者,那就完全没问题了.


小智 5

您可以从广播组中找到 findViewById。

((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true);`
Run Code Online (Sandbox Code Playgroud)


小智 5

Siavash的答案是正确的:

((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
Run Code Online (Sandbox Code Playgroud)

但是请注意,radioGroup可以包含除radioButtons之外的视图-像此示例一样,每个选项下都包含一条淡淡的线。

<RadioGroup
    android:id="@+id/radioKb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/kb1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:button="@null"
        android:drawableRight="@android:drawable/btn_radio"
        android:text="Onscreen - ABC" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#33000000" />

    <RadioButton
        android:id="@+id/kb2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:button="@null"
        android:drawableRight="@android:drawable/btn_radio"
        android:text="Onscreen - Qwerty" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#33000000" />

    <RadioButton
        android:id="@+id/kb3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:button="@null"
        android:drawableRight="@android:drawable/btn_radio"
        android:text="Standard softkey" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#33000000" />

    <RadioButton
        android:id="@+id/kb4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:button="@null"
        android:drawableRight="@android:drawable/btn_radio"
        android:text="Physical keyboard" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#33000000" />

</RadioGroup>
Run Code Online (Sandbox Code Playgroud)

例如,在这种情况下,使用索引1将产生错误。索引1处的项目是第一个分隔线-不是radioButton。本例中的单选按钮位于索引0、2、4、6。