Android Dialog总结
来源:互联网 发布:身边低调的有钱人知乎 编辑:程序博客网 时间:2024/05/02 04:24
一.Dialog基础
Dialog 类是对话框的基类,但您应该避免直接实例化 Dialog,而是使用下列子类之一:
1.AlertDialog
此对话框可显示标题、最多三个按钮、可选择项列表或自定义布局。
2.DatePickerDialog 或 TimePickerDialog
此对话框带有允许用户选择日期或时间的预定义 UI。
二.创建对话框片段
public class FireMissilesDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(R.string.dialog_fire_missiles) .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // FIRE ZE MISSILES! } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } }); // Create the AlertDialog object and return it return builder.create(); }}
三.构建提醒对话框
1.创建
2.添加按钮
调用 setPositiveButton() 和 setNegativeButton() 方法
3.添加列表
例如添加单选列表:
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.pick_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();}
您应该通过 setTitle() 为对话框设置标题。要想指定列表项,请调用setItems() 来传递一个数组。或者,您也可以使用 setAdapter() 指定一个列表。 这样一来,您就可以使用 ListAdapter 以动态数据(如来自数据库的数据)支持列表。
如果您选择通过 ListAdapter 支持列表,请务必使用 Loader,以便内容以异步方式加载。使用适配器构建布局和加载程序指南中对此做了进一步描述
4.添加永久性多选列表或单选列表
要想添加多选项(复选框)或单选项(单选按钮)列表,请分别使用 setMultiChoiceItems() 或 setSingleChoiceItems() 方法。
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) { mSelectedItems = new ArrayList(); // Where we track the selected items AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Set the dialog title builder.setTitle(R.string.pick_toppings) // Specify the list array, the items to be selected by default (null for none), // and the listener through which to receive callbacks when items are selected .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();}
5.创建自定义布局
要扩展 DialogFragment 中的布局,请通过 getLayoutInflater() 获取一个 LayoutInflater 并调用 inflate(),其中第一个参数是布局资源 ID,第二个参数是布局的父视图。然后,您可以调用 setView() 将布局放入对话框。
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Get the layout inflater LayoutInflater inflater = getActivity().getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the parent view because its going in the dialog layout builder.setView(inflater.inflate(R.layout.dialog_signin, null)) // Add action buttons .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { // sign in the user ... } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { LoginDialogFragment.this.getDialog().cancel(); } }); return builder.create();}
提示:如果您想要自定义对话框,可以改用对话框的形式显示 Activity,而不是使用 Dialog API。 只需创建一个 Activity,并在 清单文件元素中将其主题设置为 Theme.Holo.Dialog:
就这么简单。Activity 现在会显示在一个对话框窗口中,而非全屏显示
6.全屏显示对话框或将其显示为嵌入式片段
(待续)
7.将 Activity 显示为大屏幕上的对话框
(待续)
- Android Dialog用法总结
- Android常用Dialog总结
- Android Dialog用法总结
- Android Dialog用法总结
- Android Dialog用法总结
- android dialog使用总结
- Android Dialog总结
- android dialog 总结
- Android Dialog总结
- Android自定义Dialog总结
- Android Dialog总结
- android dialog用法总结
- Android Dialog的使用方法总结
- android中的dialog的总结
- Android 之 Dialog 使用总结
- Android Dialog,DialogFragment对话框总结
- android之dialog控件总结
- Android Dialog相关问题总结
- Linux学习笔记——例说makefile 增加宏定义
- HDU 4287 Intelligent IME hash
- Android并发编程之图文解析volatile关键字
- ConstraintLayout使用浅谈
- leetcode 93. Restore IP Addresses
- Android Dialog总结
- QT学习链接
- 写的一个Arduino控制超声波模块的例子
- javascriptAPI-高德地图(点击选地址)
- C#枚举类型的常用操作总结
- 判断某个窗口是否是活动窗口(当前正在使用、操作的窗口)
- iOS开发:整理UITextField属性的基本用法(部分)
- tcp socket文件句柄泄漏
- 交换瓶子