RadioGroup中:如何检查编程 [英] RadioGroup: How to check programmatically

查看:189
本文介绍了RadioGroup中:如何检查编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从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);

这篇关于RadioGroup中:如何检查编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆