“This is my 27th day of participating in the First Challenge 2022. For more details: First Challenge 2022.”

👉 About the author

As we all know, life is a long process of constantly overcoming difficulties and reflecting on progress. In this process, there will be a lot of questions and thoughts about life, so I decided to share my thoughts, experiences and stories to find resonance!!

Focus on Android/Unity and various game development tips, as well as various resource sharing (websites, tools, materials, source code, games, etc.)

Welcome to pay attention to the public account [Mr. Empty name] for more resources and communication!

👉 premise

This is small empty insist to write Android novice series, welcome to taste.

Big guy (×)

Novice (√)

👉 Practice

😜 Preview

😜 Basic use

Previously we learned a lot of controls [Button], [EditText], [ImageView], [TextView], [Switch], [Chip] and other components, the above components can achieve a lot of user interaction, but this is not enough for the scenario. Sometimes we have one or more options for the user to check the requirements (e.g. when agreeing to the APP’s protocol).

These are the main characters of today’s talk [RadioGroup] and [RadioButton].

RadioButton is one of the most common UI components, inheriting from the Button class and directly using the various properties and methods supported by Button.

RadioGroup inherits to LinearLayout, so the properties of LinearLayout can be used by RadioGroup, such as landscape or vertical layout.

The relationship between RadioGroup and RadioButton:

  • A RadioButton represents a single circular checkbox, and a RadioGroup is a control that can hold multiple RadioButtons

  • The Radiobuttons in each RadioGroup are mutually exclusive and only one can be selected at a time

  • Because each RadioGroup is independent, the Radiobuttons in different radiogroups are irrelevant to each other. For example, if one of group A is selected, one of group B can still be selected

Android: Button =”@null” if you want to hide the circle, implement a custom style.

Layout code


      
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingTop="400dp">

    <RadioGroup
        android:id="@+id/rb_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/rb_km"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mr. Empty Name" />

        <RadioButton
            android:id="@+id/rb_zm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sesame seeds" />
    </RadioGroup>

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:drawableTop="@drawable/radiobutton"
            android:gravity="center"
            android:text="WeChat" />

         <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:drawableTop="@drawable/radiobutton"
            android:gravity="center"
            android:text="QQ" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:drawableTop="@drawable/radiobutton"
            android:gravity="center"
            android:text="Zhihu" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:drawableTop="@drawable/radiobutton"
            android:gravity="center"
            android:text="Trill" />
    </RadioGroup>
</LinearLayout>
Copy the code

radiobutton.xml


      
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <! -- Unselected image -->
    <item android:drawable="@mipmap/icon_xin_no" android:state_checked="false" />
    <! -- Selected image -->
    <item android:drawable="@mipmap/icon_xin_yes" android:state_checked="true" />
</selector>
Copy the code

😜 Click events

Listeners are registered to the RadioGroup, and the incoming callback is the OnCheckedChangeListener interface in the RadioGroup class, So the realization of the Activity is RadioGroup OnCheckedChangeListener interface, and then according to the different processing of id contrast to do

public class RadioActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio);
        RadioGroup radioGroup = findViewById(R.id.rb_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int id) {
                switch (id){
                    case R.id.rb_km:
                        Toast.makeText(RadioActivity.this."You clicked on an empty name.", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.rb_zm:
                        Toast.makeText(RadioActivity.this."You clicked on sesame.", Toast.LENGTH_SHORT).show();
                        break; }}}); }}Copy the code

👉 other

📢 author: Kom and Kom in Kom

📢 reprint instructions – be sure to specify the source: Zhim Granular’s personal home page – column – Nuggets (juejin. Cn)

📢 the road friend please stay ☁️, I see you extraordinary, talk between the faint king imperious 💚, in the future will have a great as 📝!! Next to a little like 👍 collect 🌟 pass you today, point it, in the future you successful ☀️, I do not take a cent, if not successful ⚡ 🌟, or come back to me.