DialogFragment
DialogFragment
从Android 3.0 (API level 11)开始引入,如果想在低于该版本的系统上使用,需用android.support.v4.app.DialogFragment来代替android.app.DialogFragment
0.Dialog的布局结构:

1:Title部分,该部分不是必须的(可以显示,也可以隐藏)
2:内容区域,该部分用来显示你定义的message,list,或者你的自定义布局
3:按钮区域,显示确定取消按钮的(其实中间还有个按钮,不过一般不用)。
1.建立一个普通的Dialog
只需要重写DialogFragment的onCreateDialog方法,返回一个Dialog对象即可
例子:
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("这是个title");
builder.setMessage("如你所见,这是个message");
builder.setPositiveButton("确定",null);
builder.setNegativeButton("取消",null);
return builder.create();
}
2.显示List样式的Dialog
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("这是个title");
builder.setItems(new String[]{"blue","yellow","green"},null);
return builder.create();
}
注意:dialog无法同时显示message跟list
你也可以使用builder的setAdapter方法来使用ListAdapter动态显示list
3.显示多选(checkbox)或单选(radiobutton)list的Dialog
可分别调用AlertDialog.Builder的 setMultiChoiceItems()方法,或setSingleChoiceItems()方法
@Override
public 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();
}
4.使用自定义布局
使用AlertDialog.Buidler的setView()方法来设置自定义布局。默认情况下,自定义布局会填满Dialog的窗口,当然,也可以继续设置title和button
先上效果图:

layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="64dp"
android:gravity="center"
android:textStyle="bold"
android:textSize="26dp"
android:background="#FFFFBB33"
android:text="Android App"
/>
<EditText
android:id="@+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="username" />
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="password"/>
</LinearLayout>
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(R.layout.login_dialog);
builder.setNegativeButton("cancle",null);
builder.setPositiveButton("Sign in",null);
return builder.create();
}
5.显示dialog
public void confirmFireMissiles() {
DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(getSupportFragmentManager(), "missiles");
}
DialogFragment的更多相关文章
- 详细解读DialogFragment
原博客地址:http://www.cnblogs.com/tianzhijiexian/p/4161811.html 相信看这篇文章的人都应该知道android中的Dialog了吧,如果对于Dialo ...
- Android 官方推荐 : DialogFragment 创建对话框
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37815413 1. 概述 DialogFragment在android 3.0时 ...
- DialogFragment is gone after returning back from another activity
基本情景如下: 在DialogFragment中单击一个按钮跳转到another Activity做一些逻辑处理,然后将返回的结果回显到该DialogFragment上. 处理逻辑是: 在Dialog ...
- Momo自定义DialogFragment
在Fragnment弹窗提示 XML <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- [Android Pro] Android 官方推荐 : DialogFragment 创建对话框
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37815413 1. 概述 DialogFragment在android 3.0时 ...
- DialogFragment 自定义弹窗
layout文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:a ...
- Android屏幕底部弹出DialogFragment(3)
Android屏幕底部弹出DialogFragment(3) 附录文章1,2的DialogFragment是常规的DialogFragment,但是现在的一些Android开发中,往往需要从底部 ...
- Android 程式开发:(十三)特殊碎片 —— 13.2 DialogFragment
Android 程式开发:(十三)特殊碎片 —— 13.2 DialogFragment 原文地址 我们也可以创建另外一种碎片——DialogFragment.顾名思义,DialogFragment就 ...
- Android控件大全(一)——DialogFragment创建对话框
DialogFragment在android 3.0时被引入.是一种特殊的Fragment,用于在Activity的内容之上展示一个模态的对话框.典型的用于:展示警告框,输入框,确认框等等. 在Dia ...
- Android 自定义dialogfragment
在用dialogfragment的时候我们可能会不喜欢系统自带的黑色边框,那怎么办呢? dialofragment提供可供修改样式的方法setStyle(style,R.style.MyTryUseD ...
随机推荐
- [置顶] a+=1/a=+1/a-=1区别-c语言
1.解释 a+=1/a=+1/a-=1 含义 a+=1 实质等于 a += 1,也就是等于 a = a + 1: a=+1 实质等于 a = +1:[因为运算符中没有=+,很多人误以为是 a =+ 1 ...
- 用ConfigurationManager读取和修改配置文件
为了方便有时我们会把一些简单的配置的信息放入web.config文件里. 放到appSettings添加key value等信息. ConfigurationManager.AppSettings ...
- c# 实现IComparable、IComparer接口、Comparer类的详解
在默认情况下,对象的Equals(object o)方法(基类Object提供),是比较两个对象变量是否引用同一对象.我们要必须我自己的对象,必须自己定义对象比较方式.IComparable和ICom ...
- xxx-servlet.xml vs applicationContext.xml
Spring lets you define multiple contexts in a parent-child hierarchy. The applicationContext.xml def ...
- RT-Thread学习笔记(2)
这段时间稍微折腾了一下stm32,稍微知道了一点stm32程序的编写方法,所以再次拿起了rtt,因为这个东西确实很强大. 随手记录一下rtt的一些知识: 1.关于finsh 这是一个命令行系统,很好玩 ...
- [GIF] GIF Loop Coder Single Mode
We'll take a look at Single Mode animations and strategies for making this type of animation smoothl ...
- [ES6] 18. Map
ES6 provides Map, it is a set of k-v pair. Key can be number, string, object, function and even unde ...
- Linux下的简单好用的计算器bc
1. 关于bc bc是随意精度计算器语言,通常在linux下当计算器用,简单好用.相当于windows下的计算器. 2. 支持的运算符 主要的数学运算: + 加法 - 减法 * 乘法 / 除法 ^ 指 ...
- MapReduce原理讲解
简介 本文主要介绍MapReduce V2的基本原理, 也是笔者在学习MR的学习笔记整理. 本文首先大概介绍下MRV2的客户端跟服务器交互的两个协议, 然后着重介绍MRV2的核心模块MRAppMast ...
- python源码解析
http://blog.csdn.net/balabalamerobert/article/category/168910