Android dialog使用
翻译自:开发->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使用的更多相关文章
- Android Dialog使用举例
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...
- Android Dialog 创建上下文菜单
Android Dialog中的listview创建上下文菜单 listView.setOnCreateContextMenuListener(new OnCreateContextMenuListe ...
- Android控件——7种形式的Android Dialog使用举例(转载)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...
- Android Dialog对话框的七种形式的使用
参考资料:http://www.oschina.net/question/54100_32486 注:代码进行了整理 在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询 ...
- 8种形式的Android Dialog使用举例
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...
- android Dialog实例
Dialog类 public class DialogUtil { public static Dialog EditDialog(Activity context,View view){ final ...
- android dialog
/** * @Title MenuTest.java * @package com.example.standardview * @since * @version 1.0.0 * @author V ...
- android dialog 有关token的问题
android中的dialog显示一般是显示在宿主context里面,但context有几种模式,我今天遇到问题就是在BroadcastReceiver广播里面构造对话框后显示出现的问题:androi ...
- android dialog 模拟新浪、腾讯title弹框效果
http://blog.csdn.net/jj120522/article/details/7764183 首先我们看一下新浪微博的效果(其它就是一个dialog): 点 ...
- Android Dialog用法
摘要: 创建对话框 一个对话框一般是一个出现在当前Activity之上的一个小窗口. 处于下面的Activity失去焦点, 对话框接受所有的用户交互. 对话框一般用于提示信息和与当前应用程序直接相关的 ...
随机推荐
- HP Gen8,9 型号系列服务器更换主板
更换主板前,记下如下信息,根据具体情况用于更换后的设置用.1.S/N (其实主机箱上会写有,更换后重置)2.ProductID (其实主机箱上会写有,更换后重置)3.iLO IP地址或者MAC地址(根 ...
- vue请求拦截
写了很多的vue项目,经常碰到需要做请求拦截的情况,从发请求前的token判断到对返回信息的响应,我自己在不同的阶段是用不同的方式处理的. 入门阶段 记得当时做的第一个项目,是需要在请求头部加入登录是 ...
- 自定义鼠标右键(层叠式菜单:cascading menu)
Author:KillerLegend From:http://www.cnblogs.com/killerlegend/p/3575391.html Date:Tuesday, February 1 ...
- [Chrome]点击页面元素后全屏
function isFullScreen() { return (document.fullScreenElement && document.fullScreenElement ! ...
- seo优化之域名泛解析优缺点分析
原文地址:http://www.phpddt.com/web/analysis-of-domain-name.html 自己也算半个SEOER,虽然没从事过优化工作,由于自己很感兴趣,每天还是会去看很 ...
- idea+dubbo+zookeeper项目访问html页面的方法
将js,html文件放入consumer的resource的static和template文件中 1 consumer的pom需要引入模板的jar包 <dependency> ...
- NPF or NPCAP service is not installed
"NPF or NPCAP service is not installed" "could not start local server process:GNS3&qu ...
- [UE4]区分敌我
第一方法是:使用Actor对象的Tag: 只要继承自Actor的对象,都会有Tags这个属性,这是一个数组,可以添加多个Tag. 在蓝图中可以使用“get Components by Tag”获得某个 ...
- ARCGIS 出图显示not map metafile into memory.Not enough memory
有许多的原因,比如系统有问题,磁盘空间不够,或虚拟内存设置不对等.再有就是输出地图时分辨率的设置是否太大等. not enough memory 这个问题是ESRI的一个所谓的“臭名昭著 ...
- USACO 2008 Running(贝茜的晨练)
[题解] 动态规划,dp[i][j]表示第i分钟疲劳度为j的最长距离. [代码] #include <iostream> #include <cstdlib> #include ...