ภาพรวม
ตัวอย่างนี้สาธิตวิธีสร้าง AlertDialog ที่มี 3 ปุ่ม: Positive (Yes), Negative (No), และ Neutral (Cancel) พร้อมจัดการ Event แต่ละปุ่ม
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
สร้างโปรเจกต์ Android ใหม่ใน Android Studio
ขั้นตอนที่ 2: เพิ่ม Layout XML
ไฟล์ res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/parent"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/customDialog"
android:text="Custom Dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ขั้นตอนที่ 3: โค้ด Java
ไฟล์ src/MainActivity.java:
package com.example.andy.myapplication;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.customDialog).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Alert Dialog");
builder.setMessage("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s");
// Positive Button (Yes)
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You have clicked Yes", Toast.LENGTH_LONG).show();
}
});
// Negative Button (No)
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You have clicked No", Toast.LENGTH_LONG).show();
}
});
// Neutral Button (Cancel) - ปุ่มกลาง
builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You have clicked Neutral", Toast.LENGTH_LONG).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
}
}
ลำดับปุ่มใน AlertDialog
| ปุ่ม | ตำแหน่ง (LTR) | ความหมายทั่วไป | ค่าคงที่ |
|---|---|---|---|
| Positive | ขวาสุด | ยืนยัน/ตกลง/บันทึก | BUTTON_POSITIVE (-1) |
| Neutral | กลาง | ยกเลิก/ภายหลัง/อาจจะ | BUTTON_NEUTRAL (-3) |
| Negative | ซ้ายสุด | ยกเลิก/ปฏิเสธ/ไม่ | BUTTON_NEGATIVE (-2) |
🔧 การปรับแต่งเพิ่มเติม
1. เปลี่ยนสีปุ่ม
AlertDialog dialog = builder.create(); dialog.show(); // เปลี่ยนสีปุ่มหลัง show() dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.RED); dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.BLUE); dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(Color.GRAY);
2. ใช้ Custom Theme
// ใน styles.xml
<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="colorAccent">#FF5722</item> // สีปุ่ม
<item name="android:textColorPrimary">#333</item>
</style>
// ใช้งาน
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.CustomAlertDialog);
3. กำหนดปุ่มให้ไม่ปิด Dialog อัตโนมัติ
AlertDialog dialog = builder.create();
dialog.show();
// Override onClick เพื่อควบคุมการปิดเอง
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(v -> {
// ตรวจสอบเงื่อนไขก่อนปิด
if (validateInput()) {
dialog.dismiss();
}
});
เมื่อไรควรใช้ Neutral Button?
- Positive: ยืนยันการกระทำหลัก (Save, Confirm, OK, Delete)
- Negative: ยกเลิกการกระทำหลัก (Cancel, No, Discard)
- Neutral: ตัวเลือกที่ไม่ใช่ Yes/No เช่น "Later", "Remind me", "Skip", "More info", "Settings"
วิธีทดสอบ
- รันแอป กดปุ่ม "Custom Dialog"
- จะแสดง Dialog มี 3 ปุ่ม: Yes (ขวา), Cancel (กลาง), No (ซ้าย)
- กดแต่ละปุ่ม จะแสดง Toast ตามที่กำหนด
📱 Material Design 3 (Material You) - ใช้ MaterialAlertDialogBuilder
// ต้องเพิ่ม dependency: implementation 'com.google.android.material:material:1.8.0+'
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
new MaterialAlertDialogBuilder(this)
.setTitle("Title")
.setMessage("Message")
.setPositiveButton("Yes", (dialog, which) -> { /* ... */ })
.setNegativeButton("No", (dialog, which) -> { /* ... */ })
.setNeutralButton("Later", (dialog, which) -> { /* ... */ })
.show();