AlertDialog với Single Choice (Radio Button)
Sử dụng AlertDialog.Builder.setSingleChoiceItems() để hiển thị danh sách radio button, cho phép chọn một mục.
Bước 1: Tạo dự án mới
Khởi tạo project Android Studio.
Bước 2: Layout (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to show dialog"
android:textSize="24sp"
android:padding="24dp" />
</LinearLayout>
Bước 3: MainActivity.java
package com.example.myapplication;
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private TextView text;
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = findViewById(R.id.click);
text.setOnClickListener(v -> showAlertDialog());
}
private void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Chọn ngôn ngữ lập trình");
final String[] items = {"Java", "Kotlin", "Python", "JavaScript", "C++"};
int checkedItem = 1; // Mặc định chọn Kotlin (index 1)
builder.setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// which = index của item được chọn
String selected = items[which];
Toast.makeText(MainActivity.this, "Đã chọn: " + selected, Toast.LENGTH_SHORT).show();
// dialog.dismiss(); // Tự dismiss nếu muốn, hoặc để user tự bấm nút
}
});
// Thêm nút xác nhận/hủy (tùy chọn)
builder.setPositiveButton("OK", (dialog, which) -> {
// Lấy lựa chọn hiện tại (cần lưu biến member hoặc dùng listView.getCheckedItemPosition())
Toast.makeText(this, "Đã xác nhận", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("Hủy", (dialog, which) -> dialog.dismiss());
AlertDialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
}
Lưu ý quan trọng về getCheckedItemPosition()
Để lấy lựa chọn khi bấm OK, cần truy cập ListView bên trong dialog:
AlertDialog dialog = builder.create();
dialog.show();
// Lấy ListView internal
ListView listView = dialog.getListView();
int checkedPos = listView.getCheckedItemPosition();
if (checkedPos != ListView.INVALID_POSITION) {
String selected = items[checkedPos];
}
Custom Layout cho Radio Button (nâng cao)
Nếu cần giao diện tùy chỉnh hoàn toàn:
// res/layout/dialog_radio_list.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<RadioGroup android:id="@+id/radioGroup" ...>
<RadioButton android:text="Option 1" />
<RadioButton android:text="Option 2" />
<RadioButton android:text="Option 3" />
</RadioGroup>
</LinearLayout>
// Java
View customView = getLayoutInflater().inflate(R.layout.dialog_radio_list, null);
RadioGroup radioGroup = customView.findViewById(R.id.radioGroup);
builder.setView(customView);
So sánh các loại danh sách trong AlertDialog
| Phương thức | Loại chọn | UI | Trường hợp dùng |
|---|---|---|---|
setItems() |
Single click | Text đơn giản | Menu hành động nhanh |
setSingleChoiceItems() |
Single choice | Radio button | Chọn 1 trong nhiều (cài đặt, ngôn ngữ) |
setMultiChoiceItems() |
Multiple choice | Checkbox | Chọn nhiều (filter, permission) |
setAdapter() |
Tùy chỉnh | Custom layout | Complex item layout |