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

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

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

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

Android Studio → File → New Project → กรอกรายละเอียด

ขั้นตอนที่ 2: Layout หลัก (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/RelativeLayout01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Dialog">

    <EditText
        android:id="@+id/EditText01"
        android:text="AlertDialog Demo"
        android:layout_height="50dip"
        android:layout_width="180dip">
    </EditText>

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/EditText01"
        android:text="Clear Data">
    </Button>

</RelativeLayout>

ขั้นตอนที่ 3: MainActivity.java (Dialog.java)

package com.example.sample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Dialog extends AppCompatActivity {
    Button Clear;
    EditText edit_data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Clear = findViewById(R.id.Button01);
        edit_data = findViewById(R.id.EditText01);

        Clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Dialog.this);
                
                // ตั้งค่าหัวข้อ (ถ้าต้องการไม่มีหัวข้อ ให้ลบบรรทัดนี้ออก)
                builder.setTitle("Confirm");
                
                builder.setMessage("Do you want to Clear?");
                
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        edit_data.setText("");
                        Toast.makeText(getApplicationContext(), 
                            "Your text has been cleared", Toast.LENGTH_LONG).show();
                    }
                });

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                AlertDialog alertDialog = builder.create();
                alertDialog.show();
                
                // วิธีซ่อน Title: ใช้ requestWindowFeature ก่อน create()
                // alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            }
        });
    }
}

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".Dialog">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

หมายเหตุสำคัญ: วิธีซ่อน Title จริงๆ

หากต้องการ Dialog ไม่มี Title เลย ให้เพิ่มโค้ดนี้ก่อน builder.create():

// ต้องเรียกก่อน create()
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

หรือใช้ธีมที่ไม่มี Title: android:theme="@android:style/Theme.Dialog.NoTitleBar"

การทดสอบ

รันแอป → กดปุ่ม Clear Data → จะแสดง Dialog (มี Title "Confirm" ตามโค้ดตัวอย่าง)