默认情况下显示已检查单选按钮的内容,android

时间:2014-10-19 11:24:05

标签: android radio-button

在我的Activity中,我有三个水平设置的RadioButtons。对于每个radioButton,我设置了一些复选框和一个按钮,每个单选按钮都有不同的选项。

 <RadioGroup
                    android:id="@+id/selection"
            android:layout_width="fill_parent"
            android:weightSum="3"
            android:layout_height="fill_parent" android:orientation="horizontal">
        <RadioButton

                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Sensation"
                android:id="@+id/sens" android:checked="true"/>
        <RadioButton
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Location"
                  android:layout_weight="1"
                android:id="@+id/loc" android:checked="false"/>
        <RadioButton
                android:layout_width="fill_parent"
                  android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Modalation"
                android:id="@+id/mod" android:checked="false"/>
    </RadioGroup>

现在,我希望默认情况下检查我的第一个radiobutton,并在此活动开始时显示其相关内容。但我默认无法显示相关内容。它只会在我选择不同的单选按钮并重新选择时显示。我猜它是因为“OnCheckChangeListener”,我们还有一些其他的radioButton事件,默认情况下,被检查的radioButton的内容可以通过这个事件显示吗?

1 个答案:

答案 0 :(得分:0)

您使用什么来显示相关内容? TextView

我认为您应该为视图设置默认内容,每当选中单选按钮时,您都可以通过onRadioButtonClicked功能更新视图内容。

编辑:

1。 将默认内容设置为textView,并将默认选中三个单选按钮之一。我想你已经做到了。

2。 为每个单选按钮设置onclick事件,如下所示:

...
<RadioButton android:id="@+id/radio_btn_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="radio_btn_1"
        android:onClick="onRadioButtonClicked"/>
...

3。 在您的活动中放置单选按钮检查事件处理程序,并使用setText()相应地更新textview:

...
public void onRadioButtonClicked(View view) {
    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();
    TextView textView = (TextView) findViewById(R.id.textView);

    // Check which radio button was clicked
    switch(view.getId()) {
        case R.id.radio_btn_1:
            if (checked)
                textView.setText('your content for btn 1');
            break;
        case R.id.radio_btn_2:
            if (checked)
                textView.setText('your content for btn 2');
            break;
    }
}
...