Hướng dẫn này minh họa cách ngăn Dialog không tự đóng khi nhấn nút (Button) trong Android sử dụng Kotlin.
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: Thiết kế giao diện (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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
</RelativeLayout>
Bước 3: Viết code MainActivity.kt
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val dialog = AlertDialog.Builder(this)
.setTitle("My Alert Dialog")
.setMessage("Do you wanna dismiss this Dialog")
.setPositiveButton("Ok", null)
.setNegativeButton("Cancel", null)
.show()
val positiveButton: Button = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
positiveButton.setOnClickListener {
Toast.makeText(this, "Không đóng dialog", Toast.LENGTH_SHORT).show()
}
}
}
Bước 4: Cấu hình AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.q11">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Chạy ứng dụng
Kết nối thiết bị, nhấn Run. Dialog hiện ra ngay khi app khởi động. Nhấn nút "Ok" sẽ không đóng dialog mà chỉ hiển thị Toast "Không đóng dialog". Nhấn "Cancel" vẫn đóng dialog như bình thường.
Giải thích kỹ thuật
Mặc định, AlertDialog tự động đóng khi nhấn bất kỳ button nào được set qua setPositiveButton/setNegativeButton. Để ngăn hành vi này:
- Gọi
.show()để lấy instanceAlertDialog - Dùng
dialog.getButton(AlertDialog.BUTTON_POSITIVE)để lấy reference đến button thực tế - Gán
setOnClickListenermới ghi đè hành vi mặc định
Đóng dialog thủ công khi cần
positiveButton.setOnClickListener {
if (validateInput()) {
dialog.dismiss() // Đóng dialog khi điều kiện thỏa mãn
} else {
Toast.makeText(this, "Vui lòng nhập đúng", Toast.LENGTH_SHORT).show()
}
}