Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีปิด Dialog เมื่อคลิกนอกพื้นที่ Dialog ใน Android

ภาพรวม

ตัวอย่างนี้แสดงวิธีควบคุมการปิด AlertDialog เมื่อคลิกนอกพื้นที่ Dialog โดยใช้ setCanceledOnTouchOutside()

ขั้นตอนการสร้าง

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio ไปที่ File → New Project และกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 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:gravity="center"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/click"
        android:layout_width="wrap_content"
        android:textSize="30sp"
        android:layout_height="wrap_content"
        android:text="Click"/>
</LinearLayout>

ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/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 {
    TextView text;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        text = findViewById(R.id.click);
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAlertDialog();
            }
        });
    }

    private void showAlertDialog() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
        alertDialog.setTitle("AlertDialog");
        alertDialog.setMessage("ต้องการปิด Dialog หรือไม่?");
        alertDialog.setIcon(R.drawable.logo);
        
        alertDialog.setPositiveButton("ใช่", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "คุณกดปุ่มใช่", Toast.LENGTH_LONG).show();
            }
        });
        
        alertDialog.setNegativeButton("ไม่", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // ไม่ทำอะไร
            }
        });
        
        AlertDialog alert = alertDialog.create();
        alert.setCanceledOnTouchOutside(false); // false = ไม่ปิดเมื่อคลิกนอกพื้นที่
        alert.show();
    }
}

คำอธิบายสำคัญ

  • setCanceledOnTouchOutside(true) - ปิด Dialog เมื่อคลิกนอกพื้นที่ (ค่าเริ่มต้น)
  • setCanceledOnTouchOutside(false) - ไม่ปิด Dialog เมื่อคลิกนอกพื้นที่ ต้องกดปุ่มเท่านั้น

การทดสอบ

รันแอป คลิก TextView เพื่อเปิด Dialog ลองคลิกนอกพื้นที่ Dialog จะพบว่า Dialog ไม่ปิด เพราะตั้งค่าเป็น false