Giới thiệu
Dialog trong Android là 1 hộp thoại hiển thị dùng để thông báo cho người dùng hoặc cho phép người dùng thực hiện 1 hành động, có nhiều loại Dialog tùy thuộc nhu cầu như:
- Thông báo cho người dùng.
- Cho người dùng lựa chọn.
- Các loại Dialog hiển thị tùy chỉnh khác, ...
Dialog trong Android
Tất cả các Dialog trong Android đều kế thừa từ lớp abstract Dialog
, lớp này định nghĩa 2 phương thức chính:
show()
: dùng để mở Dialog.dismiss()
: dùng để đóng Dialog.
AlertDialog trong Android
AlertDialog
là class để xây dựng Dialog, gồm 3 thành phần chính:
Title
Là phần tiêu đề của Dialog, ngoài ra phần tiêu đề có thể thêm setIcon()
.
Content
Là thành phần quan trọng nhất của Dialog dùng để hiển thị nội dung thông báo của Dialog:
message
,ListView
(single selection, multiple selection).- 1
View
tự custom, ...
Action
Thành phần Action
của AlertDialog
gồm 3 button:
NegativeButton
PositiveButton
NeutralButton
Sử dụng AlertDialog trong Android
Để sử dụng Dialog
trong Android phải thông qua 1 AlertDialog
, cần thiết lập các thành phần cho Dialog sau đó sử dụng phương thức create()
để trả về đối tượng Dialog.
* Builder Pattern được sử dụng trong trường hợp này.
Dialog có content là Message
AlertDialog Dialog = new AlertDialog.Builder(this)
.setTitle("This is title")
.setMessage("Nguyen Van Nghia")
.setNegativeButton("OK", new DialogInterface.OnClick() {
@Override
public void onClick(DialogInterface DialogInterface, int i) {
Log.e(TAG, "OK");
}
}).setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface DialogInterface, int i) {
Log.e(TAG, "Cancle");
}
})
.setNeutralButton("Normal", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface DialogInterface, int i) {
Log.e(TAG, "Normal");
}
})
.create();
Dialog.show();
Trong đó:
setTitle()
dùng để đặt tiêu đề cho Dialog.setMessage()
dùng để đặt nội dung cho Dialog.
3 phương thức setXButton()
dùng để đặt listener cho 3 button đã giới thiệu ở trên với 2 đối số là:
- Đối số thứ nhất: Text hiển thị trên Button.
- Đối số thứ hai: Interface xử lý event khi người dùng click vào Button.
Sau khi gọi phương thức show()
Dialog ở trên, sẽ thấy kết quả như dưới đây:

Có thể dùng thêm phương thức dismiss()
để đóng Dialog.
Khi chạm ngoài Dialog thì Dialog sẽ đóng, nếu không muốn đóng Dialog khi chạm ở ngoài Dialog thì làm như sau:
.setCancelable(false)
Lưu ý: không nhất thiết phải đặt đủ 3 thành phần của AlertDialog.
Dialog có content là custom view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/edit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/lbl_your_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_horizontal_margin"
android:text="Your name" />
<EditText
android:id="@+id/txt_your_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/activity_horizontal_margin"
android:imeOptions="actionDone"
android:inputType="text" />
</LinearLayout>
Và sử dụng view này để làm nội dung cho Dialog thông qua phương thức setView()
như sau:
android.support.v7.app.AlertDialog DialogCustom = new android.support.v7.app.AlertDialog.Builder(this)
.setTitle("Custom Dialog")
.setView(R.layout.edit_name_layout)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface DialogInterface, int i) {
}
})
.create();
DialogCustom.show();
Dialog có content là 1 ListView
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, new String[]{"Nguyen Van Nghia", "Nguyen Hoang Minh", "Pham Nguyen Tam Phu", "Tran Van Phuc", "Nguyen Ngoc Tien", "Nguyen Xuan Vien"});
AlertDialog DialogListView = new AlertDialog.Builder(this)
.setTitle("Dialog ListView")
.setIcon(R.drawable.favicon)
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface Dialog, int which) {
Log.e(TAG, "onClick: " + adapter.getItem(which));
}
})
.create();
DialogListView.show();
Dialog có content là ListView Single Selection
Để hiển thị Dialog kiểu Single Selection viết như sau:
.setTitle("SingleSelect Dialog")
.setIcon(R.drawable.favicon)
.setSingleChoiceItems(new CharSequence[]{"Nguyen Nghia", "Hoang Minh", "Pham Phu", "Tran Phuc"}, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface DialogInterface, int i) {
Log.e(TAG, "onClick: " + i);
}
})
.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface DialogInterface, int i) {
}
})
.create();
singleDialog.show();
Dialog có content là ListView multiple selection
final android.support.v7.app.AlertDialog multiDialog = new android.support.v7.app.AlertDialog.Builder(this)
.setTitle("Multi Dialog")
.setIcon(R.drawable.favicon)
.setMultiChoiceItems(new CharSequence[]{"Nguyen Nghia", "Hoang Minh", "Pham Phu", "Tran Phuc"}, new boolean[]{false, true, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface DialogInterface, int i, boolean b) {
}
})
.setNeutralButton("Neutral", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface DialogInterface, int i) {
}
})
.create();
multiDialog.show();
Ngoài ra có 1 số listener khi mở ra hay đóng Dialog như dưới đây:
Khi show Dialog:
Dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface Dialog) {
}
});
Khi dismiss Dialog:
Dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface Dialog) {
}
});
Khi cancel Dialog:
Dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface Dialog) {
}
});
Khi nhấn phím trên Dialog:
Dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface Dialog, int keyCode, KeyEvent event) {
return false;
}
});
Ngoài ra còn 1 số Dialog cũng được sử dụng phổ biến như ProgressDialog
, DatePickerDialog
, TimePickerDialog
.
Tải source code
Tải source code của bài viết bằng 1 trong 2 cách sau:
- Tải trực tiếp: DialogAndroid-master.zip.
- Tải thông qua Github.