bài 7: toast – dialog, listview & binding

19
Toast – Dialog, ListView & Binding ANDROID APPLICATION DEVELOPMENT hoccungdoanhnghiep.com

Upload: hoccungdoanhnghiep

Post on 12-Jul-2015

4.526 views

Category:

Education


5 download

TRANSCRIPT

Page 1: Bài 7: Toast – Dialog, ListView & Binding

Toast – Dialog, ListView & Binding

ANDROID APPLICATION DEVELOPMENT

hoccungdoanhnghiep.com

Page 2: Bài 7: Toast – Dialog, ListView & Binding

Nội dung

Toast Dialog

AlertDialog ProgressDialog DatePickerDialog TimePickerDialog

ListView Binding data to ListView

hoccungdoanhnghiep.com

Page 3: Bài 7: Toast – Dialog, ListView & Binding

Toast Notifications

Là một dạng popup message hiển thị trên màn

hình Tùy thuộc vào message mà popup toast sẽ có

kích cỡ tương ứng khi hiển thị Tự động fade-in & fade-out theo thời gian được

set trong duration Trong lúc hiển thị, không ảnh hưởng đến activity

# Không bắt các sự kiện của người dùng

hoccungdoanhnghiep.com

Page 4: Bài 7: Toast – Dialog, ListView & Binding

Cách sử dụng Toast

Khởi tạo một object từ class Toast bằng hàm

makeText(): Toast t = Toast.makeText(context,

message, duration) context: application context message: Nội dung message duration: Thời gian hiển thị

Để hiển thị, gọi đến hàm show: t.show()

hoccungdoanhnghiep.com

Page 5: Bài 7: Toast – Dialog, ListView & Binding

Toast tùy biến với layout

Toast có thể được tùy biến giao diện bằng cách

gán layout đã được tạo sẵn cho nó: LayoutInflater li = getLayoutInflater(); View v = li.inflate(R.layout.toast_layout,

(ViewGroup)findViewById(R.id.my_toast_layout_root)); Toast t = new Toast(getApplicationContext()); t.setView(v); t.setDuration(Toast.LENGTH_LONG); t.setGravity(Gravity.CENTER_VERTICAL, 0, 0); t.show();

hoccungdoanhnghiep.com

Page 6: Bài 7: Toast – Dialog, ListView & Binding

Dialog

Là một cửa sổ nhỏ xuất hiện bên trên activity

hiện tại Người dùng bắt buộc phải xử lý thông tin trên

dialog trước khi có thể quay lại được activity Dialog nên được sử dụng dưới 1 trong các dạng

sau: AlertDialog: chứa thông tin (text, list item) &

buttons để hỏi ý kiến người dùng ProgressDialog: hiển thị thanh tiến trình DatePickerDialog: Cho phép người dùng chọn

date TimePickerDialog: cho phép người dùng chọn

timehoccungdoanhnghiep.com

Page 7: Bài 7: Toast – Dialog, ListView & Binding

AlertDialog

Có thể chứa các thông tin sau: Tiêu đề Nội dung message Tối đa 3 buttons List các item có thể chọn lựa (kèm theo

checkboxes hoặc radio buttons)

Để khởi tạo AlertDialog, ta phải sử dụng lớp con

là AlertDialog.Builder: AlertDialog.Builder build = new

AlertDialog.Builder(context)

Sau khi build xong, thì gọi create để tạo object

của AlertDialog từ builder đó AlertDialog ad = build.create()

Để hiển thị dialog thì gọi hàm show() Để ngưng hiển thị, gọi hàm dismiss()

hoccungdoanhnghiep.com

Page 8: Bài 7: Toast – Dialog, ListView & Binding

AlertDialog.Builder

Các method quan trọng cần ghi nhớ của

AlertDialog.Builder setTitle: Dùng để set title cho dialog setMassage: dùng để hiển thị nội dung message.

Chú ý: Khi hiển thị message thì không set với các

loại nội dung khác setCancelable: false để vô hiệu hóa nút back khi

đang hiển thị dialog setPositiveButton: Hiển thị nút action thứ nhất bên

trái setNegativeButton: Hiển thị nút action thứ 3 bên

phải setNeutralButton: Hiển thị nút action ở giữa setMultiChoiceItems: đưa vào list các items có

checkbox cho phép người dùng lựa chọn setSingleChoiceItems: đưa vào các

item+radiobutton để người dùng lựa chọn

hoccungdoanhnghiep.com

Page 9: Bài 7: Toast – Dialog, ListView & Binding

DatePicker Dialog

Cung cấp cho người dùng một công cụ để chọn:

ngày-tháng-năm tiện lợi và dễ nhìn Để show, khởi tạo object từ class

DatePickerDialog với các tham số: current activity context, this callback – là object của OnDateSetListener year: năm khởi tạo month: tháng khởi tạo day: ngày khởi tạo

hoccungdoanhnghiep.com

Page 10: Bài 7: Toast – Dialog, ListView & Binding

TimePicker Dialog

Cung cấp cho người dùng một công cụ để chọn:

giờ-phút tiện lợi và dễ nhìn

hoccungdoanhnghiep.com

Page 11: Bài 7: Toast – Dialog, ListView & Binding

Progress Dialog

Hiển thị progress animation ở dạng: Progress bar: với những task đã define được quá

trình bắt đầu – kết thúc Spinning wheel: với những task không xác định

được điểm kết thúc

ProgressBar mặc định là loại spinning wheel Để hiển thị loại progress bar thì cần code nhiều

hơn

hoccungdoanhnghiep.com

Page 12: Bài 7: Toast – Dialog, ListView & Binding

ProgressBar – spinning wheel

Khởi tạo: ProgressDialog pb =

ProgressDialog.show(ActivityContext.this, title,

msg, indeterminate ) ActivityContext.this title Msg indeterminate: set true để chỉ ra là loại không xác

định

hoccungdoanhnghiep.com

Page 13: Bài 7: Toast – Dialog, ListView & Binding

Progress bar dialog

Khởi tạo: ProgressDialog pd = new

ProgressDialog(this) Cài đặt các properties cho pd:

pd.setProgressStyle(ProgressDialog.STYLE_HOR

IZONTAL): hiển thị progress dạng thanh nằm

ngang pd.setMessage("Loading...”): nội dung message

hiển thị pd.setCancelable(true): cho phép nhấn nút back

Ngoài ra, cần phải có 1 background thread xử lý

task và update progress thông qua handler

hoccungdoanhnghiep.com

Page 14: Bài 7: Toast – Dialog, ListView & Binding

Dialog

Nếu trong màn hình có nhiều loại dialog khác

nhau, override method onCreateDialog(int id) và

dựa vào id đó để xác định dialog nào sẽ hiển thị Khi cần hiển thị dialog tương ứng với id đó, gọi

vào hàm showDialog(id) Để tùy biến Dialog, khởi tạo object: Dialog d =

new Dialog(this) Gán layout: d.setContentView(layout);

Với các loại dialog có sẵn, để tùy biến layout thì

sử dụng cách như với tùy biến Toast: LayoutInflater li = getLayoutInflater(); View v = li.inflate(layout,

(ViewGroup)findViewById(layout_root)); dialog.setView(v);

hoccungdoanhnghiep.com

Page 15: Bài 7: Toast – Dialog, ListView & Binding

Xử lý sự kiện trên dialog

Các button trên dialog được xử lý sự kiện như

các button bình thường Các item trên dialog cũng xử lý như bình

thường Khi build dialog, tùy theo callback được gán mà

sự kiện sẽ được bắt và xử lý tùy theo ý của LTV

hoccungdoanhnghiep.com

Page 16: Bài 7: Toast – Dialog, ListView & Binding

ListView

Là một ViewGroup có chứa 1 danh sách các

items bên trong, và nó có thể cuộn lên xuống Danh sách các items được tự động đưa vào

dựa vào ListAdapter

hoccungdoanhnghiep.com

Page 17: Bài 7: Toast – Dialog, ListView & Binding

Binding data to ListView

hoccungdoanhnghiep.com

Page 18: Bài 7: Toast – Dialog, ListView & Binding

Bài tập

Tự code lại toàn bộ các loại AlertDialog để kiểm

tra các tham số của builder

hoccungdoanhnghiep.com

Page 19: Bài 7: Toast – Dialog, ListView & Binding

Tham khảo

http://developer.android.com/reference/android/widget/Toast.html http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog http://developer.android.com/reference/android/app/AlertDialog.Builder.html http://developer.android.com/resources/tutorials/views/hello-datepicker.html http://developer.android.com/resources/tutorials/views/hello-timepicker.html http://developer.android.com/resources/tutorials/views/hello-listview.html http://developer.android.com/guide/topics/ui/binding.html

hoccungdoanhnghiep.com