设置Android RadioGroup的选定索引

时间:2012-03-23 16:02:54

标签: android android-radiogroup android-radiobutton

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

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

7 个答案:

答案 0 :(得分:171)

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

radioGroup.check(R.id.myButtonId);

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

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

答案 1 :(得分:82)

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

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

........

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

答案 2 :(得分:4)

Siavash的回答是正确的:

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

但要注意,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>

例如,在这种情况下,使用索引1会产生错误。索引1处的项是第一个分隔线 - 而不是radioButton。此示例中的radioButtons位于索引0,2,4,6。

答案 3 :(得分:2)

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

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

答案 4 :(得分:1)

这对我来说,我通过

动态创建单选按钮
  private void createRadioButton() {

    RadioButton[] rb = new RadioButton[5];

    RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            1.0f);

    radioGroup.setOrientation(RadioGroup.HORIZONTAL);

    for (int ID = 0; ID < 5; ID++) {
        rb[ID] = new RadioButton(this);
        rb[ID].setLayoutParams(layoutParams);
        rb[ID].setText("Button_Text");
        radioGroup.addView(rb[ID]); //the RadioButtons are added to the radioGroup instead of the layout
    }
}

现在使用

检查按钮
int radio_button_Id = radioGroup.getChildAt(index).getId();

radioGroup.check( radio_button_Id );

答案 5 :(得分:0)

在onBindViewHolder内部,将标签设置为“按钮组”

@Override
public void onBindViewHolder(final CustomViewHolder holder, final int position) {
   ...
   holder.ButtonGroup.setTag(position);
}

和ViewHolder

ButtonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        ...
        int id = radioGroup.getCheckedRadioButtonId();

        RadioButton radioButton = (RadioButton) radioGroup.findViewById(id);

        int clickedPos = (Integer) radioGroup.getTag();

        packageModelList.get(clickedPos).getPoll_quest()
    }

答案 6 :(得分:0)

使用Kotlin,您可以做到

  (radio_group_id.getChildAt(index) as RadioButton).isChecked = true

radio_group_id.check(R.id.radio_button_id

相关问题