Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

Radio Group ใน Android คืออะไร และการใช้งาน

บทนำ

RadioGroup เป็น ViewGroup ที่จัดกลุ่ม RadioButton ให้เลือกได้เพียงตัวเดียวในกลุ่ม (Mutually Exclusive) เมื่อเลือกตัวหนึ่ง ตัวอื่นจะถูกยกเลิกอัตโนมัติ

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio เลือก File → New Project กรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: ออกแบบ Layout (res/layout/activity_main.xml)

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

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select your favourite subject"
        android:textSize="20sp"
        android:padding="10dp" />

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp">

        <RadioButton
            android:id="@+id/adroid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android" />

        <RadioButton
            android:id="@+id/java"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Java" />

        <RadioButton
            android:id="@+id/cpp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C++" />

        <RadioButton
            android:id="@+id/clan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="C Programming" />

    </RadioGroup>

    <Button
        android:id="@+id/buton"
        android:text="OK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp" />

</LinearLayout>

ขั้นตอนที่ 3: เขียนโค้ด Java (src/MainActivity.java)

package com.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button = findViewById(R.id.buton);
        final RadioGroup radioGroup = findViewById(R.id.radioGroup);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // ดึง ID ของ RadioButton ที่ถูกเลือก
                int checkedId = radioGroup.getCheckedRadioButtonId();

                if (checkedId == -1) {
                    // ไม่มีตัวไหนถูกเลือก
                    button.setError("Please select an option");
                } else {
                    button.setError(null);
                    RadioButton selectedRadioButton = findViewById(checkedId);
                    String selectedText = selectedRadioButton.getText().toString();
                    Toast.makeText(MainActivity.this, "Selected: " + selectedText, Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

การทดสอบแอปพลิเคชัน

รันแอปบนอุปกรณ์จริง

Radio Group ใน Android คืออะไร และการใช้งาน

เลือกตัวเลือก เช่น Java

Radio Group ใน Android คืออะไร และการใช้งาน

กด OK - จะแสดง Toast ชื่อตัวเลือกที่เลือก

Radio Group ใน Android คืออะไร และการใช้งาน

เมธอดสำคัญของ RadioGroup

เมธอด คำอธิบาย
getCheckedRadioButtonId() คืน ID ของ RadioButton ที่ถูกเลือก หรือ -1 หากไม่มี
check(int id) ตั้งค่าให้ RadioButton ที่ระบุ ID ถูกเลือก
clearCheck() ยกเลิกการเลือกทั้งหมด
setOnCheckedChangeListener() ตั้ง Listener ตรวจจับการเปลี่ยนแปลงการเลือก

การใช้ OnCheckedChangeListener (ทางเลือก)

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        RadioButton rb = findViewById(checkedId);
        if (rb != null) {
            // ทำงานทันทีเมื่อผู้ใช้เลือก
            Toast.makeText(MainActivity.this, "Selected: " + rb.getText(), Toast.LENGTH_SHORT).show();
        }
    }
});

สรุป

RadioGroup ใช้จัดกลุ่ม RadioButton ให้เลือกได้อย่างเดียว ใช้ getCheckedRadioButtonId() ดึงตัวที่เลือก เหมาะสำหรับแบบสอบถาม เมนูตั้งค่า ฯลฯ