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

วิธีตั้ง OnClickListener ให้ Radio Button ใน Android

ภาพรวม

ตัวอย่างนี้แสดงวิธีการจัดการเหตุการณ์คลิก (OnClickListener) สำหรับ Radio Button ใน Android โดยใช้แอตทริบิวต์ android:onClick ใน XML

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

สร้างโปรเจกต์ Android ใหม่ใน Android Studio

ขั้นตอนที่ 2: จัดทำ Layout (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/radioGroup"
        android:layout_marginBottom="20sp"
        android:layout_centerInParent="true"
        android:textSize="16sp"
        android:textStyle="bold|italic"
        android:text="เลือกยี่ห้อรถที่ชอบ..."/>
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BMW"
            android:onClick="onClickedBMW"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="NISSAN GTR"
            android:onClick="onClickedNissan"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Lamborghini"
            android:onClick="onClickedLamborghini"/>
    </RadioGroup>
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity.java

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

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClickedBMW(View view) {
        Toast.makeText(this, "BMW ถูกเลือกสำหรับขับ!", Toast.LENGTH_SHORT).show();
    }

    public void onClickedNissan(View view) {
        Toast.makeText(this, "Nissan GTR ถูกเลือกสำหรับขับ!", Toast.LENGTH_SHORT).show();
    }

    public void onClickedLamborghini(View view) {
        Toast.makeText(this, "Lamborghini ถูกเลือกสำหรับขับ!", Toast.LENGTH_SHORT).show();
    }
}

วิธีการอื่น: ใช้ setOnCheckedChangeListener

RadioGroup radioGroup = findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        RadioButton rb = findViewById(checkedId);
        String text = rb.getText().toString();
        Toast.makeText(MainActivity.this, text + " ถูกเลือก", Toast.LENGTH_SHORT).show();
    }
});

เปรียบเทียบวิธีการ

วิธี優點缺點
android:onClickเขียนง่าย ไม่ต้อง findViewByIdยากต่อการ debug ต้องมีเมธอด public
setOnCheckedChangeListenerจัดการกลุ่มได้ครั้งเดียว รองรับ logic ซับซ้อนโค้ดมากกว่า ต้อง findViewById

การทดสอบ

รันแอป เลือก Radio Button ใดก็ได้ จะแสดง Toast แจ้งยี่ห้อที่เลือก