翻译自:开发->API 指南->User Interface & Navigation->Dialogs

注意:

dialog是一个基类,但是我们应该尽可能避免直接使用dialog,而是应该使用其子类,比如AlertDialog,DatePickerDialog或者TimePickerDialog等。

使用DialogFragment去管理dialog,这会让你的app在用户点击返回键和旋转屏幕时,正确的控制dialog的生命周期。DialogFragment与传统的Fragment基本一致。

Creating a Dialog Fragment

使用DialogFragment创建一个基本的AlertDialog

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

如果你创建了以上的dialog,并且调用了show()方法,那你会看到如下所示的dialog。

Building an Alert Dialog

Alert Dialog包括三部分,标题,内容和按钮,如下图所示

创建一个Alert Dialog

// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title); // 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();

添加按钮

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Set other dialog properties
... // Create the AlertDialog
AlertDialog dialog = builder.create();

dialog里也可以添加list,具体的可以查看官方文档。

Creating a Custom Layout

If you want a custom layout in a dialog, create a layout and add it to an AlertDialog by calling setView() on your AlertDialog.Builder object.

如果想使用自定义布局,可以在AlertDialog.Builder中调用setView()方法,将自定义布局添加到AlertDialog中。

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

小提示:如果你想创建一个自定义dialog,你可以显示一个Activity去代替dialog,只需要在 manifest中设置activity的主题为Theme.Holo.Dialog即可。如下所示

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

搞定,这样activity就会显示成 一个dialog,而不是全屏显示啦。

Android dialog使用的更多相关文章

  1. Android Dialog使用举例

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...

  2. Android Dialog 创建上下文菜单

    Android Dialog中的listview创建上下文菜单 listView.setOnCreateContextMenuListener(new OnCreateContextMenuListe ...

  3. Android控件——7种形式的Android Dialog使用举例(转载)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...

  4. Android Dialog对话框的七种形式的使用

    参考资料:http://www.oschina.net/question/54100_32486 注:代码进行了整理 在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询 ...

  5. 8种形式的Android Dialog使用举例

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...

  6. android Dialog实例

    Dialog类 public class DialogUtil { public static Dialog EditDialog(Activity context,View view){ final ...

  7. android dialog

    /** * @Title MenuTest.java * @package com.example.standardview * @since * @version 1.0.0 * @author V ...

  8. android dialog 有关token的问题

    android中的dialog显示一般是显示在宿主context里面,但context有几种模式,我今天遇到问题就是在BroadcastReceiver广播里面构造对话框后显示出现的问题:androi ...

  9. android dialog 模拟新浪、腾讯title弹框效果

    http://blog.csdn.net/jj120522/article/details/7764183 首先我们看一下新浪微博的效果(其它就是一个dialog):                点 ...

  10. Android Dialog用法

    摘要: 创建对话框 一个对话框一般是一个出现在当前Activity之上的一个小窗口. 处于下面的Activity失去焦点, 对话框接受所有的用户交互. 对话框一般用于提示信息和与当前应用程序直接相关的 ...

随机推荐

  1. 开始使用GoJS

    GoJS是一个用于实现交互式图表的JavaScript库.本页将向您展示使用GoJS的必要条件. 由于GoJS是一个依赖于HTML5功能的JavaScript库,因此您需要确保您的页面声明它是一个HT ...

  2. Js之事件循环(执行机制)

    js的执行机制是事件循环 什么是事件循环? js引擎在执行代码时,首先会将同步代码加入到主线程中,异步代码会放到event table中注册回调函数, 当主线程空闲之后,event table中的回调 ...

  3. Hive格式各种格式下不同压缩算法的比较

    原始Text格式的hive分区大小为119.2G. 压缩算法 Text格式 Parquet格式 ORC RCFile 不压缩 119.2G 54.1G 20.0G 98G Snappy压缩 30.2 ...

  4. go语言学习--语法糖

    语法糖(Syntactic sugar),也译为糖衣语法,是由英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语,指计算机语言中添加的某种语法,这种语法对语言的功能并没有 ...

  5. [UE4]对象

    类型和类 1.类型,Type,代表了数据含义,程序员可以对数据进行哪些操作.如果是整数,就可以进行加减乘除:如果是字符串,可以进行打印.连接.但不能对字符串进行加减乘除. 2.类,class,自定义类 ...

  6. scheduler定时器相关

    定时器官网: http://www.quartz-scheduler.org/

  7. SCCM 2012 R2实战系列之八:OSD(上)--分发全新Windows7系统

    今天将跟大家一起分享SCCM 中最为重要的一个功能---操作系统分发(OSD),在此文章中会讨论到OSD的初始化配置.镜像的导入.任务序列的创建编辑.并解决大家经常遇到的分发windows7系统分区盘 ...

  8. Vue百度搜索

    <!DOCTYPE html><html lang="en"><head> <meta charset="GBK"&g ...

  9. (转)驱动程序安装类(C#)

    原文地址:http://www.cnblogs.com/BoyXiao/archive/2011/03/31/2001535.html 回忆刚进公司那会儿,手头的第一个工作就是完成虚拟键盘,也就是通过 ...

  10. CS229 4.Logistic Regression