Showing a Dialog


  When you want to show your dialog, create an instance of your DialogFragment and call show(), passing theFragmentManager and a tag name for the dialog fragment.

  You can get the FragmentManager by calling getSupportFragmentManager() from the FragmentActivity orgetFragmentManager() from a Fragment. For example:

public void confirmFireMissiles() {
DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(getSupportFragmentManager(), "missiles");
}

  The second argument, "missiles", is a unique tag name that the system uses to save and restore the fragment state when necessary. The tag also allows you to get a handle to the fragment by callingfindFragmentByTag().

Showing a Dialog Fullscreen or as an Embedded Fragment


  You might have a UI design in which you want a piece of the UI to appear as a dialog in some situations, but as a full screen or embedded fragment in others (perhaps depending on whether the device is a large screen or small screen). The DialogFragment class offers you this flexibility because it can still behave as an embeddableFragment.

However, you cannot use AlertDialog.Builder or other Dialog objects to build the dialog in this case. If you want the DialogFragment to be embeddable, you must define the dialog's UI in a layout, then load the layout in the onCreateView() callback.

Here's an example DialogFragment that can appear as either a dialog or an embeddable fragment (using a layout named purchase_items.xml):

public class CustomDialogFragment extends DialogFragment {
/** The system calls this to get the DialogFragment's layout, regardless
of whether it's being displayed as a dialog or an embedded fragment. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout to use as dialog or embedded fragment
return inflater.inflate(R.layout.purchase_items, container, false);
} /** The system calls this only when creating the layout in a dialog. */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
}

show a fragment as a dialog or a fullscreen UI

And here's some code that decides whether to show the fragment as a dialog or a fullscreen UI, based on the screen size:

public void showDialog() {
FragmentManager fragmentManager = getSupportFragmentManager();
CustomDialogFragment newFragment = new CustomDialogFragment(); if (mIsLargeLayout) {
// The device is using a large layout, so show the fragment as a dialog
newFragment.show(fragmentManager, "dialog");
} else {
// The device is smaller, so show the fragment fullscreen
FragmentTransaction transaction = fragmentManager.beginTransaction();
// For a little polish, specify a transition animation
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// To make it fullscreen, use the 'content' root view as the container
// for the fragment, which is always the root view for the activity
transaction.add(android.R.id.content, newFragment)
.addToBackStack(null).commit();
}
}

For more information about performing fragment transactions, see the Fragments guide.

In this example, the mIsLargeLayout boolean specifies whether the current device should use the app's large layout design (and thus show this fragment as a dialog, rather than fullscreen). The best way to set this kind of boolean is to declare a bool resource value with an alternative resource value for different screen sizes. For example, here are two versions of the bool resource for different screen sizes:

res/values/bools.xml

<!-- Default boolean values -->
<resources>
<bool name="large_layout">false</bool>
</resources>

res/values-large/bools.xml

<!-- Large screen boolean values -->
<resources>
<bool name="large_layout">true</bool>
</resources>

Then you can initialize the mIsLargeLayout value during the activity's onCreate() method:

boolean mIsLargeLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mIsLargeLayout = getResources().getBoolean(R.bool.large_layout);
}

Showing an activity as a dialog on large screens

  Instead of showing a dialog as a fullscreen UI when on small screens, you can accomplish the same result by showing an Activity as a dialog when on large screens. Which approach you choose depends on your app design, but showing an activity as a dialog is often useful when your app is already designed for small screens and you'd like to improve the experience on tablets by showing a short-lived activity as a dialog.

To show an activity as a dialog only when on large screens, apply the Theme.Holo.DialogWhenLarge theme to the <activity> manifest element:

<activity android:theme="@android:style/Theme.Holo.DialogWhenLarge" >

For more information about styling your activities with themes, see the Styles and Themes guide.

Dismissing a Dialog


  When the user touches any of the action buttons created with an AlertDialog.Builder, the system dismisses the dialog for you.

The system also dismisses the dialog when the user touches an item in a dialog list, except when the list uses radio buttons or checkboxes. Otherwise, you can manually dismiss your dialog by calling dismiss() on yourDialogFragment.

  In case you need to perform certain actions when the dialog goes away, you can implement the onDismiss()method in your DialogFragment.

You can also cancel a dialog. This is a special event that indicates the user explicitly left the dialog without completing the task. This occurs if the user presses the Back button, touches the screen outside the dialog area, or if you explicitly call cancel() on the Dialog (such as in response to a "Cancel" button in the dialog).

As shown in the example above, you can respond to the cancel event by implementing onCancel() in yourDialogFragment class.

  Note: The system calls onDismiss() upon each event that invokes the onCancel() callback. However, if you call Dialog.dismiss() or DialogFragment.dismiss(), the system calls onDismiss() but notonCancel(). So you should generally call dismiss() when the user presses the positive button in your dialog in order to remove the dialog from view.

Android Dialogs(5)[正常显示dlg,将Fragment显示为dialog,将Aty显示为dlg,嵌入],关闭Dialog的更多相关文章

  1. android自定义dialog中点击listview的item事件关闭dialog

    import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; ...

  2. Android Dialogs(6)Dialog类使用示例:用系统theme和用自定义的theme

    使用dialog时有很多 方法,其中一个就是直接 使用基类Dialog,可用来作一个没有按钮的非模态提示框,它可以直接从系统的主题构造也可从自定义的主题构造. 基本步骤: a,构造 b,调用dialo ...

  3. Android开发 侧边滑动菜单栏SlidingMenu结合Fragment

    SlidingMenu是一个开源项目, https://github.com/jfeinstein10/SlidingMenu .功能是创建侧边滑动菜单栏,效果类似人人Android客户端,可点击按钮 ...

  4. Android ActionBar通过Tab进行不同的Fragment之间的交换

    ActionBar的使用常见于4.0系统,其Tab的使用挺广泛的. 在ActionBar中添加标签(Tabs),每个标签对应的是一个Fragment,点击不同的Tab时,就会切换到对应的Fragmen ...

  5. 《Android进阶》之第五篇 Fragment 的使用

    http://blog.csdn.net/lmj623565791/article/details/37970961 1.Fragment的产生与介绍 Android运行在各种各样的设备中,有小屏幕的 ...

  6. 【转】Pro Android学习笔记(三六):Fragment(1):基本概念

    目录(?)[-] 为何引入Fragment 大小屏幕的适配 横屏竖屏切换 返回键 什么是Fragment 为何引入Fragment 我们之前的Activity都是都是全屏处理较为简单的单一事务功能,适 ...

  7. Android - 用Fragments实现动态UI - 创建Fragment

    你可以把fragment当作activity中的一个活动模块,它有自己的生命周期,自己接收输入消息,可以在activity运行的时候添加和删除(就像可以在其他activity中重用的"子ac ...

  8. 《Android进阶》之第六篇 Fragment 的使用2

    最近通过学习,对fragment的使用有了新的认识. 一开始接触android的时候,很是受不了这个fragment,总感觉它把一个简单的事情搞复杂啦,所以每次新建工程的时候总是固执的选择empty ...

  9. 【Android Developers Training】 20. 创建一个Fragment

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

随机推荐

  1. Linux 思维导图

    1.Linux学习路径: 2.Linux桌面介绍: 3.FHS(文件系统目录标准): 以上三张图,都是在学习实验楼上的课程--Linux 基础入门,教程里面看到的. 4.Linux需要特别注意的目录: ...

  2. ln 软连接 & 硬连接

    创建软连接的方式 #ln -s soure /file object 创建软连接是连接文件本身,可以跨分区建立软连接,不会应为不同分区而出现不能使用的问题. 在创建软连接的文件中,修改一处文件另一处同 ...

  3. Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡

     <Android ViewPager实现Tabhost选项卡底部滑块动态滑动过渡> 之前基于github上的第三方开源控件ViewPagerIndicator的UnderlinePa ...

  4. udhcp源码详解(三)上 之配置信息的读取

    上节介绍了存储管理配置信息的结构体struct server_config_t,该结构体贯穿整个server端程序的运行. 在dhcpd.c里的用该结构体定义个一个全局的变量: struct serv ...

  5. 给GridView设置行高

    近期在工作中遇到了这样一个问题,使用一个GridView展示数据,item中仅仅是一个TextView,可是里面显示的文字多少不固定多少,必须所有展示出来. 遇到的问题: 1.把item中的宽和高设置 ...

  6. handsontable整理

    hansontable简介 hansontable是一个在线类似Excel的表格编辑器,支持丰富的展现和交互,有多样的单元格类型供配置. 核心是由原生JavaScript构建,充分模块化,支持自定义b ...

  7. Win10快捷键总结

    微软自发布Windows10以来,大部分的电脑系统都已更新.除了大量的新功能和界面改进,Windows 10中同样包含了一批新的键盘快捷键.熟练使用这些快捷键可以大大提高操作效率,很实用,推荐大家收藏 ...

  8. uboot中关于LCD的代码分析【转】

    本文转载自:http://blog.csdn.net/oqqHuTu12345678/article/details/72236117 以下内容源于朱有鹏<物联网大讲坛>课程的学习,如有侵 ...

  9. 第一个Java程序示例——Hello World!【转】

    本文转载自: 跟随世界潮流,第一个Java程序输出“Hell World!”. 通过Eclipse运行程序 启动Eclipse,在菜单中选择“文件 --> 新建 --> Java项目”,弹 ...

  10. beyond compare 比较文本 standard alignment VS unaligned

    在Rules里面 Standard Alignment 这种方式会比较找出相同的部分,可能会跨行找相同的 Unaligned 这种比较直接每一行之间相互比较,不跨行找相同的