Hướng dẫn này minh họa cách tạo hộp thoại (Dialog) tùy chỉnh không hiển thị tiêu đề mặc định trong Android, sử dụng AlertDialog.Builder.
Bước 1: Tạo dự án mới
Mở Android Studio, chọn File → New Project và điền thông tin cần thiết.
Bước 2: Giao diện chính (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" />
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/EditText01"
android:text="Xóa dữ liệu" />
</RelativeLayout>
Bước 3: Xử lý sự kiện (src/MainActivity.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 btnClear;
EditText editData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnClear = findViewById(R.id.Button01);
editData = findViewById(R.id.EditText01);
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(Dialog.this);
// Đặt tiêu đề (có thể bỏ qua nếu không muốn tiêu đề)
builder.setTitle("Xác nhận");
builder.setMessage("Bạn có muốn xóa không?");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
editData.setText("");
Toast.makeText(getApplicationContext(), "Đã xóa văn bản", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Hủy", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
}
}
Bước 4: Manifest (Manifests/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>
Mẹo: Tạo Dialog hoàn toàn không tiêu đề
Nếu muốn dialog hoàn toàn không có thanh tiêu đề (không chỉ bỏ trống text), hãy sử dụng:
AlertDialog alertDialog = builder.create(); alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // Gọi trước show() alertDialog.show();
Chạy ứng dụng
Kết nối thiết bị, nhấn Run và kiểm tra hộp thoại hiển thị khi nhấn nút "Xóa dữ liệu".