1. Trang chủ
  2. » Thể loại khác

alert dialog trong android

9 141 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Nội dung

http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     Alert Dialog Android Một Dialog cửa sổ nhỏ gợi ý người dùng để định nhập thông tin bổ sung Đôi ứng dụng mình, bạn muốn u cầu người dùng lựa chọn định yes no để phản hồi action cụ thể nào, mà giữ ngun Activity khơng thay đổi hình tại, bạn sử dụng Alert Dialog Để tạo Alert Dialog, bạn cần tạo đối tượng AlertDialogBuilder mà lớp nội AlertDialog Cú pháp sau: AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); Bây bạn phải thiết lập nút yes no sử dụng đối tượng lớp AlertDialogBuilder Cú pháp là: alertDialogBuilder.setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener) alertDialogBuilder.setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener) Ngoài phương thức này, bạn sử dụng hàm khác cung cấp lớp Builder để tùy chỉnh Alert Dialog Bảng liệt kê phương thức này: Stt Phương thức type & Miêu tả setIcon(Drawable icon) Phương thức thiết lập icon Alert Dialog setCancelable(boolean cancel able) Phương thức thiết lập thuộc tính mà Dialog bị cancel không setMessage(CharSequence message) Phương thức thiết lập thông điệp để hiển thị Alert Dialog setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     DialogInterface.OnMultiChoiceClickListener listener) Phương thức thiết lập danh sách item để hiển thị Dialog Tùy chọn thông báo Listener setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) Phương thức thiết lập hàm callback gọi Dialog bị cancel setTitle(CharSequence title) Phương thức thiết lập Title xuất Dialog Sau tạo thiết lập Dialog Builder, bạn tạo Alert Dialog gọi phương thức create() lớp Builder Cú pháp là: AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); Điều tạo Alert Dialog hiển thị hình Dialog Fragment Trước vào ví dụ cụ thể, cần biết Dialog Fragment Dialog Frament Fragment mà hiển thị fragment hộp thoại Dialog public class DialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { toast.makeText(this,"enter a text here",Toast.LENTH_SHORT).show(); } }) setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { finish(); }); // Create the AlertDialog object and return it return builder.create(); } } } List dialog Nó sử dụng để hiển thị danh sách mục Dialog Giả sử người dùng cần lựa chọn item từ danh sách cần click vào item từ danh sách Lúc này, sử dụng List Dialog sau: public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     builder.setTitle(Pick a Color) setItems(R.array.colors_array, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // The 'which' argument contains the index position // of the selected item } }); return builder.create(); } List Dialog dạng Single-choice Dạng sử dụng để thêm danh sách dạng single-choice tới hộp Dialog Chúng ta kiểm tra khơng kiểm tra lựa chọn người dùng public Dialog onCreateDialog(Bundle savedInstanceState) { mSelectedItems = new ArrayList(); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("This is list choice dialog box"); setMultiChoiceItems(R.array.toppings, null,new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { // If the user checked the item, add it to the selected items mSelectedItems.add(which); } else if (mSelectedItems.contains(which)) { // Else, if the item is already in the array, remove it mSelectedItems.remove(Integer.valueOf(which)); } } }) // Set the action buttons setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // User clicked OK, so save the mSelectedItems results somewhere // or return them to the component that opened the dialog } }) setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { } }); return builder.create(); } Ví dụ Ví dụ sau minh họa sử dụng Alert Dialog Android Để thử nghiệm ví dụ, bạn cần chạy hình mơ Emulator thiết bị thực Đây nội dung sửa đổi src/MainActivity.java package com.example.sairamkrishna.myapplication; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void open(View view){ AlertDialog.Builder alertDialogBuilder = new http://vietjack.com/                                                                                                                              Trang  chia  sẻ  các  bài  học  online  miễn  phí  Page  1   http://vietjack.com/android/index.jsp                                                                                                              Copyright  ©  vietjack.com     AlertDialog.Builder(this); alertDialogBuilder.setMessage("Are you sure,You wanted to make decision"); alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(MainActivity.this,"You clicked yes button",Toast.LENGTH_LONG).show(); } }); alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } Đây nội dung sửa đổi res/layout/activity_main.xml Và nội dung Strings.xml My Application name="hello_world">Hello world! Settings

Ngày đăng: 02/12/2017, 07:13

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN