Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách thiết lập OnClickListener cho RadioButton trong Android

Giới thiệu

Ví dụ này minh họa cách xử lý sự kiện click cho từng RadioButton trong một RadioGroup bằng thuộc tính android:onClick trong XML.

Các bước thực hiện

Bước 1: Tạo dự án mới

Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.

Bước 2: Layout với RadioGroup (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="Chọn xe yêu thích để lái..."/>
    <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>

Bước 3: Xử lý sự kiện (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 được chọn để lái!", Toast.LENGTH_SHORT).show();
    }

    public void onClickedNissan(View view) {
        Toast.makeText(this, "Nissan GTR được chọn để lái!", Toast.LENGTH_SHORT).show();
    }

    public void onClickedLamborghini(View view) {
        Toast.makeText(this, "Lamborghini được chọn để lái!", Toast.LENGTH_SHORT).show();
    }
}

Bước 4: Manifest (AndroidManifest.xml)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Chạy ứng dụng

Kết nối thiết bị, nhấn Run. Chọn từng RadioButton để xem Toast hiển thị.

Cách thiết lập OnClickListener cho RadioButton trong Android

Cách thiết lập OnClickListener cho RadioButton trong Android