简体   繁体   English

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

[英]Set default radio button in Android Studio

I wanted to set the default radio button on my Android app in Android Studio. 我想在Android Studio中的Android应用程序上设置默认单选按钮。 I have my buttons set up in the XML file and I am very new to making apps. 我在XML文件中设置了按钮,我对制作应用程序非常陌生。 I just want to know how to set the default button when the app is opened. 我只是想知道在打开应用程序时如何设置默认按钮。

I have looked on the internet but I cannot understand what to do. 我看过互联网,但我不明白该怎么做。 I want something like this: http://cloud.addictivetips.com/wp-content/uploads/2012/05/SoShare-Android-Share.jpg 我想要这样的东西: http//cloud.addictivetips.com/wp-content/uploads/2012/05/SoShare-Android-Share.jpg

If you post some code, can you please comment it so I know what is happening? 如果你发布一些代码,请你评论一下,以便我知道发生了什么?

Put like this.. 像这样......

Using Xml. 使用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>

Using Java: 使用Java:

   RadioGroup radiogroup;

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

       radiogroup.check(R.id.radio1);

Say if you have this xml consist of radio buttons like below 假如你有这个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>

You can simply write below code: 你可以简单地写下面的代码:

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

Otherwise you can do this in xml level also.Note this line android:checkedButton="@+id/radiobutton1" 否则你也可以在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>

You can set by default-RadioButton using, 你可以default-RadioButton使用,

By XML, 通过XML,

android:checked="true"

By Java, 通过Java,

radiobutton1.setChecked(true);

this may helps you. 这可能对你有所帮助。

Set RadioButton in your layout like this- 在您的布局中设置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>

and in activity like this- 在这样的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);

I think it will help. 我认为这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM