Creating a Dialog Fragment


You can accomplish a wide variety of dialog designs—including custom layouts and those described in theDialogs design guide—by extending DialogFragment and creating a AlertDialog in the onCreateDialog()callback method.

For example, here's a basic AlertDialog that's managed within a DialogFragment:

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();
}
}

Now, when you create an instance of this class and callshow() on that object, the dialog appears as shown in figure 1.

Figure 1. A dialog with a message and two action buttons.

The next section describes more about using theAlertDialog.Builder APIs to create the dialog.

Depending on how complex your dialog is, you can implement a variety of other callback methods in theDialogFragment, including all the basic fragment lifecycle methods.

Android Dialogs(2)最好用DialogFragment创建Dialog的更多相关文章

  1. Android控件大全(一)——DialogFragment创建对话框

    DialogFragment在android 3.0时被引入.是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框.典型的用于:展示警告框,输入框,确认框等等. 在Dia ...

  2. DialogFragment创建默认dialog

    代码地址如下:http://www.demodashi.com/demo/12228.html 记得把这几点描述好咯:代码实现过程 + 项目文件结构截图 + 演示效果 前言 在我们项目的进行中不可避免 ...

  3. Android 官方推荐 : DialogFragment 创建对话框

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37815413 1. 概述 DialogFragment在android 3.0时 ...

  4. [Android Pro] Android 官方推荐 : DialogFragment 创建对话框

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37815413 1. 概述 DialogFragment在android 3.0时 ...

  5. 转帖:Android 官方推荐 : DialogFragment 创建对话框

    转: Android 官方推荐 : DialogFragment 创建对话框 复制内容,留作备份 1. 概述 DialogFragment在android 3.0时被引入.是一种特殊的Fragment ...

  6. 【转】 Pro Android学习笔记(四五):Dialog(2):DialogFragment

    [-] 重写onCreateView 通过onCreateView设置UI和按键反馈 信息保存 重写onCreateDialog DialogFragment的实例newInstance()已经在上一 ...

  7. Android Dialogs(1)Dialog简介及Dialog分类

    Dialogs A dialog is a small window that prompts the user to make a decision or enter additional info ...

  8. Android开发:使用DialogFragment实现dialog自定义布局

    使用DialogFragment实现dialog的自定义布局最大的好处是可以更好控制dialog的生命周期. TestFragment的代码: public class TestFragment ex ...

  9. 使用DialogFragment创建对话框总结

    回调Activity中的函数 http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents 在DialogFragme ...

随机推荐

  1. [Angular] Modify User Provided UI with Angular Content Directives

    If we’re going to make our toggle accessible, we’ll need to apply certain aria attributes to the con ...

  2. “约定优于配置”与Magento改造尝试四之block、helper和model载入

    暂定本章为这个系列最后一章,还是继续沿用模块的别名(alias)概念 <modules> <Mage_Wishlist> <version>1.6.0.0</ ...

  3. jquery获取input值的各种情况

    jQuery获取多种input值的方法 获取input的checked值是否为true: 第一种: if($("input[name=item][value='val']").at ...

  4. codeforces 394E Lightbulb for Minister 简单几何

    题目链接:点我点我 题意:给定n个点. 以下n行给出这n个点坐标. 给定m个点,以下m行给出这m个点坐标. 这m个点是一个凸包,顺时针给出的. 问:在凸包上随意找一个点(x, y) 使得这个点距离n个 ...

  5. MySQL运行计划不准确 -概述

    为毛 MySQL优化器的运行计划 好多时候都不准确,不是最优的呢(cpu+io)??? 因素太多了:: 存在information_schema的信息是定期刷新上去的,好多时候不是最真的,甚至相差好大 ...

  6. mac WebStorm 破解

    摘要:因为想要学习HTML所以需要一个工具,同事推荐了webstorm.下载以后再网上搜破解方法.搜索到一个很简单的. 一.下载链接https://www.jetbrains.com/webstorm ...

  7. Tomcat最多支持并发多少用户?

    当一个进程有 500 个线程在跑的话,那性能已经是很低很低了.Tomcat 默认配置的最大请求数是 150,也就是说同时支持 150 个并发,当然了,也可以将其改大.当某个应用拥有 250 个以上并发 ...

  8. SQL Server 多库操作 库名.dbo.表名 出错的问题!

    SQL Server 多库操作 库名.dbo.表名 出错的问题! 数据库名不要用数字开头. 例如:343934.dbo.user 这就会出错.md a343934.dbo.user 就没问题!! 记住 ...

  9. 如何获取板子上独有的ID号EXYNOS4412/Imx6ul【转】

    本文转载自:http://blog.csdn.net/u010871058/article/details/75637175 每个CPU,都有它固定的ID号,ID号就是这个CPU唯一的标识,它可能隐含 ...

  10. Ruby的一些常用全局变量

    $! latesterror message $@ locationof error $_ stringlast read by gets $. linenumber last read by int ...