69

我从 XML 创建一个 RadioGroup

    <RadioGroup android:id="@+id/option" 
        android:layout_width="match_parent"
        android:orientation="horizontal" 
        android:checkedButton="@+id/block_scenario_off"
        android:layout_height="wrap_content">
        <RadioButton 
            android:layout_width="0dip"
            android:layout_weight="1" 
            android:text="@string/option1" 
            android:layout_height="wrap_content" 
            android:id="@+id/option1"
            android:layout_gravity="center|left" 
            android:onClick="@string/on_click"/>
        <RadioButton 
            android:layout_width="0dip"
            android:layout_weight="1" 
            android:text="@string/option2" 
            android:onClick="@string/on_click"
            android:layout_height="wrap_content"
            android:layout_gravity="center" 
            android:id="@+id/option2"/>
        <RadioButton 
            android:layout_width="0dip"
            android:layout_weight="1" 
            android:text="@string/option3"
            android:onClick="@string/on_click" 
            android:layout_height="wrap_content"
            android:layout_gravity="center|right" 
            android:id="@+id/option3" />
    </RadioGroup>

在 Java 代码中,我以编程方式检查第一个活动创建 (onCreate()),如下所示:

    mOption = (RadioGroup) findViewById(R.id.option);
    mOption.check(R.id.option1);

但是当显示活动时,没有选中单选按钮。有什么帮助吗?

4

11 回答 11

86

在您的布局中,您可以添加您android:checked="true"想要CheckBox被选中的内容。

或者以编程方式,您可以使用 checkable 接口中定义的 setChecked 方法:

RadioButton b = (RadioButton) findViewById(R.id.option1); b.setChecked(true);

于 2011-10-12T16:30:47.717 回答
41

如果您希望根据某些变量的值(例如“genderStr”)检查单选按钮,请尝试此操作,然后您可以使用以下代码片段

    if(genderStr.equals("Male"))
       genderRG.check(R.id.maleRB);
    else 
       genderRG.check(R.id.femaleRB);
于 2012-02-21T13:41:42.840 回答
27

您可能需要在代码的 onCreate 方法中声明单选按钮并使用它们。

RadioButton rb1 = (RadioButton) findViewById(R.id.option1);
rb1.setChecked(true);
于 2011-10-12T19:58:07.020 回答
27

小心!检查单选按钮setChecked()不会改变 RadioGroup 内的状态。例如,radioGroup 中的这个方法将返回错误的结果:getCheckedRadioButtonId().

始终检查 radioGroup

mOption.check(R.id.option1);

您已被警告;)

于 2016-05-08T13:55:42.667 回答
25

我在处理无线电组的索引时使用了这段代码:

radioGroup.check(radioGroup.getChildAt(index).getId());
于 2017-07-18T10:49:23.287 回答
14

您也可以使用 getChildAt() 方法。像这样:

mOption = (RadioGroup) findViewById(R.id.option);
((RadioButton)mOption.getChildAt(0)).setChecked(true);
于 2016-01-29T09:32:08.367 回答
13

抓住广播组,看看孩子们,看看是否有任何未选中的。

RadioGroup rg = (RadioGroup) view;
int checked = savedInstanceState.getInt(wrap.getAttributeName());

if(checked != -1) {
    RadioButton btn = (RadioButton) rg.getChildAt(checked);
    btn.toggle();
}
于 2012-07-30T12:11:37.840 回答
12

我在为无线电组使用 getId() 时使用了这段代码:

radiogroup.check(radiogroup.getChildAt(0).getId());

根据您的 RadioGroup 设计设置位置

希望对你有帮助!

于 2018-08-18T08:26:29.493 回答
8

如果您将 方法mOption.check(R.id.option1); 放入onAttachedToWindow 方法或像这样,它将起作用:

view.post(new Runnable()
{
    public void run() 
    {
        // TODO Auto-generated method stub
        mOption.check(R.id.option1); 
    }
});

原因可能是 check 方法仅在实际渲染无线电组时才有效。

于 2012-09-06T08:58:07.953 回答
2

我更喜欢使用

RadioButton b = (RadioButton) findViewById(R.id.option1);
b.performClick();

而不是使用公认的答案。

RadioButton b = (RadioButton) findViewById(R.id.option1);
b.setChecked(true);

原因setChecked(true)只是改变了单选按钮的选中状态。它不会触发onClick()添加到单选按钮的方法。有时这可能会浪费您的时间来调试与onClick()不工作相关的操作的原因。

于 2020-01-09T23:13:07.957 回答
2

在科特林。选择第一个元素。您需要在onViewCreated或更高版本中执行此操作。

radioGroup.apply { check(getChildAt(0).id) }
于 2021-05-24T16:29:21.240 回答