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

วิธีสร้าง Dialog Box ไม่มีหัวข้อ (Title) ใน Android ด้วย Kotlin

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการสร้าง AlertDialog โดยไม่มีหัวข้อ (Title) ใน Android โดยใช้ AlertDialog.Builder และ Kotlin

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

สร้างโปรเจกต์ Android ใหม่ใน Android Studio

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอหลัก (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">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Alert Dialog without title"
        android:textAlignment="center"
        android:textColor="@android:color/holo_orange_dark"
        android:textSize="24sp"
        android:textStyle="bold" />
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ด MainActivity.kt

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        val builder = AlertDialog.Builder(this)
        builder.setMessage("Do you want to close the alert dialog without title?")
        builder.setPositiveButton("Yes") { dialog, _ ->
            dialog.cancel()
            Toast.makeText(applicationContext, "Your dialog has been closed", Toast.LENGTH_LONG).show()
        }
        builder.setNegativeButton("No") { _, _ -> }

        val alertDialog: AlertDialog = builder.create()
        alertDialog.show()
    }
}

ขั้นตอนที่ 4: ตั้งค่า AndroidManifest.xml

ใช้การตั้งค่ามาตรฐาน

ทดสอบการทำงาน

รันแอป จะแสดง Dialog ทันทีที่เปิดแอป มีข้อความและปุ่ม Yes/No โดยไม่มีหัวข้อ (Title)

ข้อมูลเพิ่มเติม

  • ไม่ต้องเรียก setTitle(): AlertDialog จะไม่แสดง Title หากไม่ได้เรียก setTitle() หรือ setCustomTitle()
  • Custom Layout: ใช้ builder.setView(R.layout.custom_dialog) สำหรับ Dialog ปรับแต่งเต็มรูปแบบ
  • DialogFragment: แนะนำใช้ DialogFragment แทน AlertDialog โดยตรง เพื่อจัดการ Lifecycle ได้ดีกว่า (ป้องกัน Memory Leak, Configuration Changes)
  • MaterialAlertDialogBuilder: ใช้จาก Material Components library สำหรับ UI ที่ทันสมัยและสอดคล้องกับ Material Design