【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式
这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的:
- 显示消息给用户,并可提供一到三个按钮(OK, Cancel ,Yes ,No)用于选择或是显示警告。
- 显示一个列表以供用户选择,列表中可以是Radio Button (单选),Check button (多选)
- 显示文本框来接受用户输入等。
创建AlertDialog一般是通过AlertDialog.Builder来构造:
AlertDialog.Builder ad=new AlertDialog.Builder(context);
之后可以为这个AlergDialog设置标题,显示的信息,需要显示的Buttons等。然后调用 ad.show来显示这个对话框。
为了避免每次显示对话框都新建一个Dialog对象,Android提供两个方法 onCreateDialog和onPrepareDialog事件来管理对话框的创建。
通过重载onCreateDialog,可以根据需要(如执行showDialog时)创建所需对话框实例。而在创建这个对话框实例后,在每次 showDialog之前,如果需要对这个对话框做些修改可以重载onPrepareDialog方法来实现。 原理和Android管理Menu的方法类似。
下面给出使用AlertDialog的一般步骤。因为在onCreateDialog可能创建多个Dialog示例,所以必须先定义一个Dialog的ID。
private static final int DIALOG_YES_NO_MESSAGE = 1;
private static final int DIALOG_YES_NO_LONG_MESSAGE = 2;
private static final int DIALOG_LIST = 3;
private static final int DIALOG_PROGRESS = 4;
private static final int DIALOG_SINGLE_CHOICE = 5;
private static final int DIALOG_MULTIPLE_CHOICE = 6;
private static final int DIALOG_TEXT_ENTRY = 7;
private static final int DIALOG_MULTIPLE_CHOICE_CURSOR = 8;
private static final int DIALOG_YES_NO_ULTRA_LONG_MESSAGE = 9;
private static final int DIALOG_YES_NO_OLD_SCHOOL_MESSAGE = 10;
private static final int DIALOG_YES_NO_HOLO_LIGHT_MESSAGE = 11;
private static final int DIALOG_YES_NO_DEFAULT_LIGHT_MESSAGE = 12;
private static final int DIALOG_YES_NO_DEFAULT_DARK_MESSAGE = 13;
private static final int DIALOG_PROGRESS_SPINNER = 14;
然后重载onCreateDialog,参数id为Dialog ID,可以根据id来创建需要的Dialog实例:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_YES_NO_MESSAGE:
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_two_buttons_title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { /* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Cancel so do some stuff */
}
})
.create(); ...
显示Dialog:
showDialog(DIALOG_YES_NO_MESSAGE);
App->Dialog目前通过14个例子来说明AlertDialog的多种用法:

OK Cancel dialog with a message
前面显示的代码就是这个例子的代码,使用AlertDialog.Builder创建一个AlertDialog示例 ,然后通过setIcon,setTitle,setPositiveButton,setNegativeButton设置这个对话框的图标,标 题,OK和Cancel Button。 标题也不知道是什么语言:-(。

OK Cancel dialog with a long message
这个例子中使用setMessage来显示一个很长的信息(可以有滚动条),并通过setNeutralButton 添加一个中间按钮。

List Dialog
AlertDialog 可以用来显示一组选项来获取用户选择。对应静态的选项可以先定义一个Array资源:
<!– Used in app/dialog examples –>
<string-array name=”select_dialog_items”>
<item>Command one</item>
< item>Command two</item>
< item>Command three</item>
< item>Command four</item>
< /string-array>
然后调用setItems 来显示这些选项:
case DIALOG_LIST:
return new AlertDialog.Builder(AlertDialogSamples.this)
.setTitle(R.string.select_dialog)
.setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { /* User clicked so do some stuff */
String[] items = getResources().getStringArray(R.array.select_dialog_items);
new AlertDialog.Builder(AlertDialogSamples.this)
.setMessage("You selected: " + which + " , " + items[which])
.show();
}
})
.create();

Progress dialog
这个例子显示了ProgressDialog的用法,ProgressDialog为AlertDialog的子类,ProgressDialog 无需通过AlertDialog.Builder 构造,可以直接通过构造函数来创建ProgressDialog的实例。ProgressDialog可以显示一个标题和一个进度条。因此比 AlertDialog多了几个方法:setProgressStyle ,setMax等来配置进度条的属性。

注: 这个例子还使用里Handler 来更新进度条,Handler将在后面的例子介绍。
case DIALOG_PROGRESS:
mProgressDialog = new ProgressDialog(AlertDialogSamples.this);
mProgressDialog.setIcon(R.drawable.alert_dialog_icon);
mProgressDialog.setTitle(R.string.select_dialog);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMax(MAX_PROGRESS);
mProgressDialog.setButton(getText(R.string.alert_dialog_hide),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Yes so do some stuff */
}
});
mProgressDialog.setButton2(getText(R.string.alert_dialog_cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { /* User clicked No so do some stuff */
}
});
return mProgressDialog;
Single choice list
AlertDialog 显示列表时,可以指定为单选或是多选。将前面List中的setItems方法改成setSingleChoiceItems可以使用RadioButton来显示列表:
case DIALOG_SINGLE_CHOICE:
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_single_choice)
.setSingleChoiceItems(R.array.select_dialog_items2, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { /* User clicked on a radio button do some stuff */
}
})
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Yes so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { /* User clicked No so do some stuff */
}
})
.create();

Repeat alarm
这个例子调用setMultiChoiceItems 方法使用CheckButton来显示列表,表示可以多选
case DIALOG_MULTIPLE_CHOICE:
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.ic_popup_reminder)
.setTitle(R.string.alert_dialog_multi_choice)
.setMultiChoiceItems(R.array.select_dialog_items3,
new boolean[]{false, true, false, true, false, false, false},
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int whichButton,
boolean isChecked) { /* User clicked on a check box do some stuff */
}
})
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { /* User clicked Yes so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { /* User clicked No so do some stuff */
}
})
.create();

Send Call to VoiceMail
上面例子中列表项都是通过定义Array资源来实现的,列表项还可以使用Content Provider作为数据源。这个例子使用通讯录中的记录作为数据源,如果在模拟器运行,请先添加几个Contacts。Content Provider介绍请参见Android ApiDemo示例解析(10):App->Activity->QuickContactsDemo
case DIALOG_MULTIPLE_CHOICE_CURSOR:
String[] projection = new String[] {
Contacts.People._ID,
Contacts.People.NAME,
Contacts.People.SEND_TO_VOICEMAIL
};
Cursor cursor = managedQuery(Contacts.People.CONTENT_URI,
projection, null, null, null);
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.ic_popup_reminder)
.setTitle(R.string.alert_dialog_multi_choice_cursor)
.setMultiChoiceItems(cursor,
Contacts.People.SEND_TO_VOICEMAIL,
Contacts.People.NAME,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int whichButton,
boolean isChecked) {
Toast.makeText(AlertDialogSamples.this,
"Readonly Demo Only
- Data will not be updated",
Toast.LENGTH_SHORT).show();
}
})
.create();

Text Entry Dialog
最后一个例子是使用文本框来接受用户输入。AlertDialog也允许自定义UI,可以使用自定义的Layout做为AlertDialog的 UI。 理论上可以使用任意的Layout,这样可以避免使用派生Dialog的方法来自定义对话框。本例使用alert_dialog_text_entry ,其中定义里两个TextView显示Name:和Password:标签 ,两个EditText接受用户输入。 然后使用setView为AlertDialog定义自定义的UI。
case DIALOG_TEXT_ENTRY:
// This example shows how to add a custom layout to an AlertDialog
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView
= factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_text_entry)
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { /* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { /* User clicked cancel so do some stuff */
}
})
.create();

【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式的更多相关文章
- 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01
本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...
- 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程
本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...
- 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener
前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...
- 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面
我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...
- 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState
Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...
- 【起航计划 003】2015 起航计划 Android APIDemo的魔鬼步伐 02 SimpleAdapter,ListActivity,PackageManager参考
01 API Demos ApiDemos 详细介绍了Android平台主要的 API,android 5.0主要包括下图几个大类,涵盖了数百api示例:
- 【起航计划 035】2015 起航计划 Android APIDemo的魔鬼步伐 34 App->Service->Local Service Controller
Local Service Controller 是将LocalService当作“Started”Service来使用,相对于”Bound” Service 来说,这种模式用法要简单得多,Local ...
- 【起航计划 034】2015 起航计划 Android APIDemo的魔鬼步伐 33 App->Service->Local Service Binding 绑定服务 ServiceConnection Binder
本例和下列Local Service Controller 的Activity代码都定义在LocalServiceActivities.Java 中,作为LocalServiceActivities ...
- 【起航计划 033】2015 起航计划 Android APIDemo的魔鬼步伐 32 App->Service->Foreground Service Controller service使用,共享service,前台服务,onStartCommand
Android系统也提供了一种称为“Service”的组件通常在后台运行.Activity 可以用来启动一个Service,Service启动后可以保持在后台一直运行,即使启动它的Activity退出 ...
随机推荐
- 资深专家深度剖析Kubernetes API Server第2章(共3章)
欢迎来到深入学习Kubernetes API Server的系列文章的第二部分.在上一部分中我们对APIserver总体,相关术语及request请求流进行探讨说明.在本部分文章中,我们主要聚焦于探究 ...
- Centos7安装MySQL8.0
请到这个地址看:https://www.cnblogs.com/kevingrace/p/10482469.html Centos7安装MySQL8.0 - 操作手册 一.yum安装方式: 卸载之前版 ...
- zabbix监控tcp连接数的脚本!!
#!/bin/bash #this script is used to get tcp and udp connetion status #tcp status metric=$ tmp_file=/ ...
- ubuntu14 上无法使用vim命令的解决方法
在ubuntu14.10终端输入vim: The program 'vim' can be found in the following packages: * vim * vim-gnome * v ...
- 需要了解的几个Java基础点
关键字 native:表示要调用非Java语言写函数,比如用C语言使用JNI实现的接口.比如windows环境的dll文件.举例:Object.hashcode() 位运算 << n:左移 ...
- 解决ifconfig没有网卡问题
ifconfig -a root@kali:~# ifup eth0 ifup: unknown interface eth0 vim /etc/network/interfaces #自行添加网卡 ...
- thinkphp控制器的使用
控制器的使用 2.1url如何调用控制器 以典型的URL为例 http://localhost/shop/index.php/home/index/test /*home/controller/in ...
- Apache重定向URL
(1)去除httpd.conf文件中"#LoadModule rewrite_module modules/mod_rewrite.so"前面的"#"号; (2 ...
- angularjs的ng-repeat回调
首先html代码是这样的: <label>Name des Leiters:</label><select name="leaderID" id=&q ...
- spring AOP注解
此段小代码演示了spring aop中@Around @Before @After三个注解的区别@Before是在所拦截方法执行之前执行一段逻辑.@After 是在所拦截方法执行之后执行一段逻辑.@A ...