Alert Dialog là hộp thoại bật lên (pop-up) cho phép người dùng chọn hành động thông qua các nút như "OK", "Cancel", "Yes", "No". Đây là cách phổ biến để xác nhận hành động quan trọng hoặc hiển thị thông báo cần tương tác.
Các phương thức chính của AlertDialog.Builder
- setTitle(CharSequence) - Đặt tiêu đề dialog
- setMessage(CharSequence) - Đặt nội dung thông báo
- setIcon(int resId) - Đặt icon cho dialog
- setView(View) - Đặt custom view
- setPositiveButton(CharSequence, OnClickListener) - Nút xác nhận (Yes/OK)
- setNegativeButton(CharSequence, OnClickListener) - Nút từ chối (No/Cancel)
- setNeutralButton(CharSequence, OnClickListener) - Nút trung tính (Maybe/Later)
- create() - Tạo AlertDialog object
- show() - Hiển thị dialog
Ví dụ cơ bản
Bước 1: Tạo project mới
Bước 2: Layout activity_main.xml - Chỉ cần một Button
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnShowDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hiển thị Dialog"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Bước 3: MainActivity.java - Xử lý sự kiện
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnShowDialog).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btnShowDialog) {
showAlertDialog();
}
}
private void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Xác nhận")
.setMessage("Bạn có chắc chắn muốn thực hiện hành động này?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Đồng ý", (dialog, which) ->
Toast.makeText(this, "Đã chọn Đồng ý", Toast.LENGTH_SHORT).show())
.setNegativeButton("Hủy", (dialog, which) ->
Toast.makeText(this, "Đã chọn Hủy", Toast.LENGTH_SHORT).show())
.setCancelable(false) // Không tắt khi click ngoài
.show();
}
}
Alert Dialog với Custom Layout
// Tạo custom view
View customView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
EditText input = customView.findViewById(R.id.etInput);
new AlertDialog.Builder(this)
.setTitle("Nhập thông tin")
.setView(customView)
.setPositiveButton("OK", (dialog, which) -> {
String text = input.getText().toString();
// Xử lý text
})
.setNegativeButton("Hủy", null)
.show();
Chạy ứng dụng
Build và chạy. Nhấn nút "Hiển thị Dialog" sẽ hiện AlertDialog với 2 lựa chọn. Chọn từng nút sẽ hiển thị Toast tương ứng.
Best Practices
- Luôn setCancelable(false) cho các dialog quan trọng cần bắt buộc chọn
- Sử dụng string resources thay vì hardcode text
- Đối với dialog phức tạp, hãy dùng DialogFragment
- Tránh lạm dụng dialog - làm phiền người dùng