在Android Studio中设置默认单选按钮

5
我想在Android Studio中设置默认单选按钮。我已经在XML文件中设置好了我的按钮,但是我对制作应用程序很陌生。我只想知道如何在打开应用程序时设置默认按钮。
我在互联网上搜索过,但我不理解该怎么做。我想要像这样的东西:http://cloud.addictivetips.com/wp-content/uploads/2012/05/SoShare-Android-Share.jpg 如果您发布一些代码,请注释一下,以便我知道正在发生什么。

1
获取按钮的视图ID,并将其设置为选中状态,如radiobutton.setChecked(true); - Madhukar Hebbar
how? i am new to this - user4919188
请粘贴与单选按钮相关的代码。 - Madhukar Hebbar
我没有任何代码。我只是在XML中使用了android:checked=true,但与按钮相关的Java代码不起作用。对于代码来说,它仍然是未选中状态。 - user4919188
你可以在RadioGroup视图中添加以下行:android:checkedButton="RadioButton的id" - Sabir Syed
4个回答

9

像这样放置...

使用XML。

                         <RadioGroup
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:id="@+id/radiogroup"
                            android:orientation="horizontal">
                            <RadioButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="RadioButton1"
                                android:checked="true"
                                android:id="@+id/radio1"
                                />
                            <RadioButton
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="RadioButton2"
                                android:id="@+id/radio2"
                                />
                        </RadioGroup>

使用Java:

   RadioGroup radiogroup;

       radiogroup=(RadioGroup)findViewById(R.id.radiogroup)

       radiogroup.check(R.id.radio1);

1

假设您有以下包含单选按钮的 XML:

<RadioGroup
        android:id="@+id/radiogroup"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RadioButton
        android:id="@+id/radiobutton1"
        android:checked="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

        <RadioButton
        android:id="@+id/radiobutton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />    
</RadioGroup>

你可以简单地写下面的代码:

radiobutton1 =(RadioButton)findViewById(R.id.radiobutton1);
radiobutton1.setChecked(true);

否则,您也可以在xml级别上进行操作。请注意此行:android:checkedButton="@+id/radiobutton1"
<RadioGroup
            android:id="@+id/radiogroup"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checkedButton="@+id/radiobutton1"
                                                >

            <RadioButton
            android:id="@+id/radiobutton1"
            android:checked="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

            <RadioButton
            android:id="@+id/radiobutton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />    
    </RadioGroup>

不对。出现了错误。它说找不到RadioButton。我应该放在onCreate里面吗?我还是有错误。 - user4919188

1
您可以通过使用default-RadioButton进行设置,
通过XML,
android:checked="true"

通过Java,
radiobutton1.setChecked(true);

这可能会对你有所帮助。

如果我使用Java版本,会出现错误。使用XML时,Java不知道它是否被检查。 - user4919188

1
将您的布局中的RadioButton设置为以下内容-
<RadioGroup>
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/radio_group"
    android:orientation="horizontal">

    <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="1"
         android:textSize="12dp"
         android:id="@+id/r1"
         android:checked="true"/>

    <RadioButton
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="2"
         android:id="@+id/r2"
         android:checked="false"
         android:textSize="12dp" />
</RadioGroup>

像这样的activity中-

    RadioGrop rg= (RadioGroup)v.findViewById(R.id.radio_group);

    // get selected radio button from radioGroup
    int selectedId = radioPassanger.getCheckedRadioButtonId();

    // find the radiobutton by returned id
    RadioButton mRadioButton = (RadioButton) v.findViewById(selectedId);

我认为这将会有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接