Detailed explanation of how to use the basic Android control radiogroup

This article shares the use of the basic Android control radiogroup for your reference. The specific contents are as follows

1. Brief introduction

Radiogroup can provide users with several options, but only one of them can be selected. Several RadioButtons can be hung horizontally or vertically below it, or other controls (such as textview) can be mounted. The corresponding events of radiogroup are generally not responded by the following RadioButton, but directly responded by radiogroup. Implement the radiogroup.oncheckedchangelister interface to listen to radiogroup. RadioButton is also derived from compoundbutton. You can also modify the icon by modifying the button attribute, but modifying the button attribute often makes the text close to the icon. At this time, we can set the drawableleft property and drawablepadding property of RadioButton to make the icon and text closer (and set the button property to @ null). The following figure shows the effect of radiogroup.

2. Simple use

The following is a simple implementation code of radiogroup.

radio_ group_ selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!--选中-->
  <item android:state_checked="true" android:drawable="@drawable/radio_choose"/>

  <!--普通状态-->
  <item android:drawable="@drawable/radio_unchoose"/>
</selector>

activity_ radio_ group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".RadioGroupActivity"
  android:orientation="vertical">

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:textColor="#000000"
    android:text="这是横着放的RadioGroup"/>

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

    <RadioButton
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:checked="false"
      android:text="好"
      android:textSize="18sp"
      android:id="@+id/rb_horizontal_good"
      android:textColor="#000000"/>

    <RadioButton
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:checked="false"
      android:text="很好"
      android:textSize="18sp"
      android:id="@+id/rb_horizontal_very_good"
      android:textColor="#000000"/>
  </RadioGroup>

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:textColor="#000000"
    android:text="这是竖着放的RadioGroup"/>

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

    <RadioButton
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:checked="false"
      android:text="好"
      android:textSize="18sp"
      android:id="@+id/rb_vertical_good"
      android:textColor="#000000"/>

    <RadioButton
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:checked="false"
      android:text="很好"
      android:textSize="18sp"
      android:id="@+id/rb_vertical_very_good"
      android:textColor="#000000"/>
  </RadioGroup>

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:textColor="#000000"
    android:text="这是改了图标竖着放的RadioGroup"/>

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

    <RadioButton
      android:button="@drawable/radio_button_selector"
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:checked="false"
      android:text="这个是直接设置button的RadioButton"
      android:textSize="18sp"
      android:id="@+id/rb_vertical_custom_good"
      android:textColor="#000000"/>

    <RadioButton
      android:button="@null"
      android:drawableLeft="@drawable/radio_button_selector"
      android:drawablePadding="10dp"
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:checked="false"
      android:text="这个是设置drawableLeft属性的RadioButton"
      android:textSize="18sp"
      android:id="@+id/rb_vertical_custom_very_good"
      android:textColor="#000000"/>
  </RadioGroup>
</LinearLayout>

RadioGroupActivity.java

package xyz.straSAE.androidlearn.my;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class RadioGroupActivity extends AppCompatActivity {
  RadioGroup rg_horizontal_demo;
  RadioGroup rg_vertical_demo;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_radio_group);
    rg_horizontal_demo = findViewById(R.id.rg_horizontal_demo);
    rg_vertical_demo = findViewById(R.id.rg_vertical_demo);
    rg_horizontal_demo.setOnCheckedchangelistener(new RadioGroup.OnCheckedchangelistener() {
      @Override
      public void onCheckedChanged(RadioGroup radioGroup,int i) {
        RadioButton rb_temp = findViewById(radioGroup.getCheckedRadioButtonId());
        Toast.makeText(RadioGroupActivity.this,String.format("你选择了%s",rb_temp.getText().toString()),Toast.LENGTH_SHORT).show();
      }
    });
    rg_vertical_demo.setOnCheckedchangelistener(new RadioGroup.OnCheckedchangelistener() {
      @Override
      public void onCheckedChanged(RadioGroup radioGroup,Toast.LENGTH_SHORT).show();
      }
    });
  }
}

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>