Android 中 RadioButton 的基本使用

Android 中 RadioButton 的基本使用

1. 基本属性

在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:padding="15dp"
    tools:context=".RadioButtonActivity">

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

        <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/man"
            android:textColor="@color/orange_low"
            android:textSize="18sp" />

        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/woman"
            android:textColor="@color/orange_low"
            android:textSize="18sp" />

    </RadioGroup>

</RelativeLayout>

2. 自定义样式

在这里插入图片描述

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

        <RadioButton
            android:id="@+id/rb_3"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:layout_marginEnd="15dp"
            android:background="@drawable/bg_rb"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:text="@string/man"
            android:textColor="@color/orange_low"
            android:textSize="18sp" />

        <RadioButton
            android:id="@+id/rb_4"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:layout_marginEnd="15dp"
            android:background="@drawable/bg_rb"
            android:button="@null"
            android:gravity="center"
            android:text="@string/woman"
            android:textColor="@color/orange_low"
            android:textSize="18sp" />
    </RadioGroup>

state_checked: 选中事件.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true">
        <shape android:shape="rectangle">
            <corners android:radius="5dp" />
            <solid android:color="@color/blue" />
        </shape>
    </item>

    <item android:state_checked="false">
        <shape android:shape="rectangle">
            <corners android:radius="5dp" />
            <solid android:color="@color/blue_low" />
        </shape>
    </item>

</selector>

3. 监听事件

在这里插入图片描述

package com.example.hello;

import androidx.appcompat.app.AppCompatActivity;

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

public class RadioButtonActivity extends AppCompatActivity {
    
    

    // 声明
    private RadioGroup rg2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);
        // 获取 RadioGroup
        rg2 = findViewById(R.id.rg_2);
        // 监听
        rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    
    
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
    
    
                // 获取选中的 RadioButton
                RadioButton radioButton  = group.findViewById(checkedId);
                // 展现内容
                Toast.makeText(RadioButtonActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });

    }
}

猜你喜欢

转载自blog.csdn.net/YKenan/article/details/112661046