如何选择RadioButton

时间:2016-10-29 20:41:32

标签: java android radio-button android-radiogroup

我有一个API,并且有一个广播的值,选项是公共的,私有的,登录的和不公开的。如果广播是公开的,我希望将radiobutton本身选为公共。

这是我的XML代码:

<TextView
    android:textColor="#060606"
    android:layout_marginTop="10dp"
    android:textSize="15sp"
    android:text="Broadcast Options"
    android:id="@+id/broadcast_head"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

    <RadioGroup
        android:orientation="vertical"
        android:id="@+id/radio_group_broadcast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
            android:textSize="16sp"
            android:text="Public"
            android:id="@+id/radio_button_public"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <RadioButton
            android:textSize="16sp"
            android:text="Private"
            android:id="@+id/radio_button_private"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <RadioButton
            android:textSize="16sp"
            android:text="Unlisted"
            android:id="@+id/radio_button_unlisted"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <RadioButton
            android:textSize="16sp"
            android:text="Logged Only"
            android:id="@+id/radio_button_logged_only"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RadioGroup>
</TextView>

这是我的Java代码; 我不知道这段代码是否正确。

rbPub = (RadioButton)findViewById(R.id.radio_button_public);
rbPri = (RadioButton)findViewById(R.id.radio_button_private);
rbUnl = (RadioButton)findViewById(R.id.radio_button_unlisted);
rbOnl = (RadioButton)findViewById(R.id.radio_button_logged_only);

radioGroup1 = (RadioGroup) findViewById(R.id.radio_group_broadcast);
VideoDetail videoDetailBroadcast = null; //this is the problem, can't fix(null)
if (videoDetailBroadcast.isPublic()) {
    rbPub.setChecked(true);
}
else if (videoDetailBroadcast.isPrivate()) {
    rbPri.setChecked(true);
}
else if (videoDetailBroadcast.isUnlisted()) {
        rbUnl.setChecked(true);
    }
else if (videoDetailBroadcast.isLoggedOnly()) {
        rbOnl.setChecked(true);
}

1 个答案:

答案 0 :(得分:0)

你应该以这种方式初始化videoDetailBroadcast:VideoDetail videoDetailBroadcast = new VideoDetail()