Android Dialogs(2)最好用DialogFragment创建Dialog

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.

时间: 2024-08-03 23:28:01

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

Android为啥推荐用DialogFragment创建Dialog?

前言:这段时候有点忙,因为在赶项目,说忙也都是在敷衍,时间挤挤还是有的,从开始写博客的那一刻起就应该一直坚持下来,不要三天打鱼两天晒网,上次写过一个Android进阶之(dialog详解一),今天继续上次的内容探究一下,从Android3.0后Android引入的DailogFragment,我们一起从源码的角度去看看为啥谷歌推荐这样创建Dialog. DialogFragment对话框出现的意义 为什么android系统有AlertDialog,PopupWindow对话框,基本满足客户需求,

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

1. 概述 DialogFragment在android 3.0时被引入.是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框.典型的用于:展示警告框,输入框,确认框等等.在DialogFragment产生之前,我们创建对话框:一般采用AlertDialog和Dialog.注:官方不推荐直接使用Dialog创建对话框. 2. 好处与用法 使用DialogFragment来管理对话框,当旋转屏幕和按下后退键时可以更好的管理其声明周期,它和Fragment有着基本一致的

Android Dialogs(1)Dialog简介及dialog种类

Dialogs A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed. Dialog

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

转: Android 官方推荐 : DialogFragment 创建对话框 复制内容,留作备份 1. 概述 DialogFragment在android 3.0时被引入.是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框.典型的用于:展示警告框,输入框,确认框等等.在DialogFragment产生之前,我们创建对话框:一般采用AlertDialog和Dialog.注:官方不推荐直接使用Dialog创建对话框. 2. 好处与用法 使用DialogFragment

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

[-] 重写onCreateView 通过onCreateView设置UI和按键反馈 信息保存 重写onCreateDialog DialogFragment的实例newInstance()已经在上一次学习笔记中实现.我们创建dialog的UI,可以通过重写DialogFragment的两个函数当中的一个来实现,这两个函数是onCreateView()和onCreateDialog(),前者返回view,后者返回dialog,如同通过AlertDialog.Builder构造一样. 重写onCr

使用 DialogFragment 创建对话框

简介 在DialogFragment产生之前,我们创建对话框一般采用AlertDialog和Dialog,DialogFragment在android 3.0时被引入,是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框. 使用DialogFragment来管理对话框,当旋转屏幕和按下后退键时可以更好的管理其生命周期(自动重建),它和Fragment有着基本一致的生命周期.且DialogFragment也允许开发者把Dialog作为内嵌的组件进行重用,类似Fragm

【转】 Pro Android学习笔记(四四):Dialog(1):触发Dialog

目录(?)[-] 创建dialog fragment Activity显示对话框 Android提供alert.prompt.pick-list,单选.多选,progress.time-picker和date-picker对话框,并提供自定义的dialog.在Android 3.0后,dialog基于fragment,并对之前版本提供兼容支持库,也就是说对于开发者而言,dialog是基于DialogFragment的,但此时需要在应用中加入相关的兼容库. 和Windows或者网页JS的Dialo

android Dialogs

1.引言 Dialog是对话框的基类,可以实现以下子类: AlertDialog,DatePickerDialog,TimPickerDialog. 这些类为你定义了样式和结构,不过你可以使用DialogFragment作为对话框的内容.通过DialogFragment你可以自由控制你的对话框,而不是继承Dialog对象沿用Dialog对象的一些方法. 当用户按返回键或屏幕翻转的时候要注意DialogFragment的生命周期.DialogFragment也允许你把它的UI用在打的UI上. 如果

Android项目中使用类似菊花的Dialog

Android项目中使用类似菊花的Dialog 效果图一: 项目中使用的效果图二: 1.首先定义动画文件 <?xml version="1.0" encoding="utf-8"?> <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/loading2"