Giới thiệu
Bài viết này hướng dẫn sử dụng RadioButton và RadioGroup để tạo nhóm lựa chọn đơn (single choice) trong Android. Chỉ một RadioButton trong một RadioGroup được chọn tại một thời điểm.
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à tạo dự án.
Bước 2: Thiết kế giao diện (res/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"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/radioGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioMale"
android:checked="true" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radioFemale" />
</RadioGroup>
<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/radioGender"
android:layout_centerInParent="true"
android:text="@string/btnDisplay" />
</RelativeLayout>
Bước 3: Chuỗi ký tự (res/values/strings.xml)
<resources>
<string name="app_name">RadioButton Demo</string>
<string name="radioMale">Nam</string>
<string name="radioFemale">Nữ</string>
<string name="btnDisplay">Hiển thị lựa chọn</string>
</resources>
Bước 4: Viết code logic (src/MainActivity.java)
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 {
RadioGroup radioGroup;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = findViewById(R.id.radioGender);
button = findViewById(R.id.btnDisplay);
button.setOnClickListener(v -> {
int selectedId = radioGroup.getCheckedRadioButtonId();
if (selectedId != -1) {
RadioButton selectedRadioButton = findViewById(selectedId);
String selectedText = selectedRadioButton.getText().toString();
Toast.makeText(this, "Bạn chọn: " + selectedText, Toast.LENGTH_SHORT).show();
}
});
// Hoặc lắng nghe thay đổi trực tiếp
radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
RadioButton rb = findViewById(checkedId);
// Xử lý realtime khi user chọn
});
}
}
Bước 5: Cấu hình 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. RadioGroup hiển thị 2 lựa chọn "Nam" (được chọn mặc định) và "Nữ". Chọn một, nhấn "Hiển thị lựa chọn" -> Toast hiện lựa chọn đã chọn.
Các thuộc tính RadioButton/RadioGroup quan trọng
| Thuộc tính | Mô tả |
|---|---|
| android:checked | Mặc định được chọn (true/false) |
| android:button | Drawable tùy chỉnh cho nút tròn (mặc định hệ thống) |
| android:buttonTint | Màu nút tròn (API 21+) |
| android:layout_width/height | Kích thước |
| RadioGroup: android:orientation | "vertical" hoặc "horizontal" |
| RadioGroup: setOnCheckedChangeListener | Listener khi lựa chọn thay đổi |
Tùy chỉnh giao diện RadioButton
1. Đổi icon nút tròn (drawable selector)
<RadioButton
android:button="@drawable/custom_radio_button"
... />
<!-- res/drawable/custom_radio_button.xml -->
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/ic_radio_on" />
<item android:drawable="@drawable/ic_radio_off" />
</selector>
2. RadioButton trong RecyclerView (Single Choice)
// Adapter
private int selectedPosition = -1;
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.radioButton.setChecked(position == selectedPosition);
holder.radioButton.setOnClickListener(v -> {
selectedPosition = position;
notifyDataSetChanged();
});
}
Lưu ý
- RadioButton phải nằm trong RadioGroup để hoạt động chế độ single choice
- Nested RadioGroup (RadioGroup trong RadioGroup) có thể gây xung đột – tránh dùng
- Để clear lựa chọn:
radioGroup.clearCheck()(selectedId = -1) - Material Design: dùng
MaterialRadioButtontừ thư viện Material Components