RadioGroup中:如何检查编程RadioGroup

由网友(重来只怕我们都没有力气)分享简介:我从XML创建一个RadioGroup中I create a RadioGroup from XML我从XML创建一个RadioGroup中

I create a RadioGroup from XML

    <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的code,我以编程方式检查第一个上的活动创造(的onCreate())如下:

In Java code, I programmatically check the first one on activity creation (onCreate()) as following:

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

但是活性示时,没有无线电按钮被选中。任何帮助?

But when the activity is shown, no radio button is checked. Any help?

推荐答案

在你的布局,您可以添加安卓检查=真复选框要进行选择。

In your layout you can add android:checked="true" to CheckBox you want to be selected.

或编程,您可以使用在可检查的接口中定义的setChecked方法:

Or programmatically, you can use the setChecked method defined in the checkable interface:

单选B =(单选)findViewById(R.id.option1); b.setChecked(真);

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

阅读全文

相关推荐

最新文章