这个例子的主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样式的更多相关文章

  1. 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01

    本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...

  2. 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程

    本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...

  3. 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener

    前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...

  4. 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面

    我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...

  5. 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState

    Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...

  6. 【起航计划 003】2015 起航计划 Android APIDemo的魔鬼步伐 02 SimpleAdapter,ListActivity,PackageManager参考

    01 API Demos ApiDemos 详细介绍了Android平台主要的 API,android 5.0主要包括下图几个大类,涵盖了数百api示例:

  7. 【起航计划 035】2015 起航计划 Android APIDemo的魔鬼步伐 34 App->Service->Local Service Controller

    Local Service Controller 是将LocalService当作“Started”Service来使用,相对于”Bound” Service 来说,这种模式用法要简单得多,Local ...

  8. 【起航计划 034】2015 起航计划 Android APIDemo的魔鬼步伐 33 App->Service->Local Service Binding 绑定服务 ServiceConnection Binder

    本例和下列Local Service Controller 的Activity代码都定义在LocalServiceActivities.Java 中,作为LocalServiceActivities ...

  9. 【起航计划 033】2015 起航计划 Android APIDemo的魔鬼步伐 32 App->Service->Foreground Service Controller service使用,共享service,前台服务,onStartCommand

    Android系统也提供了一种称为“Service”的组件通常在后台运行.Activity 可以用来启动一个Service,Service启动后可以保持在后台一直运行,即使启动它的Activity退出 ...

随机推荐

  1. 【三支火把】--- 关于BIOS&UEFI查阅资料网站总结

    UEFI和BIOS的水太深,网上能找到的资料是那么的少,各个组织之间互有交叉,难弄的很,总结了下常用的BIOS资料网站,仅供参考,如果遗漏之处,请指出,博主将继续完善补充……

  2. CSS3圆角圆边 支持IE6-IE8浏览器

    CSS3圆角圆边样式,支持各大版本浏览器,包括支持IE6-IE9浏览器的圆边圆角. 本文我们主要是讲解如果用CSS 3样式表来实现圆角效果,值得注意的是,IE6/7/8这三个IE浏览器版本都不支持CS ...

  3. [WebShow系列] 需求及环境的确定

    需求及环境确定: 主题标语 选手人数 评委人数 打分方式 合计算法 名次算法 大屏数量 大屏分辨率 电脑配置 切换设备 评委人员配备 技术人员配备 现场网速情况 评委移动端配备 场次数量 场次时间 选 ...

  4. shell操作字符串案例

    #!/bin/bash name="Shell" url="http://cxy.com/" str1=$name$url #中间不能有空格 str2=&quo ...

  5. js 遍历tree的一个例子(全遍历)

    全遍历 亲测真是有效. 工作中遇到的问题应该算是比较有价值的问题. <!DOCTYPE html> <html lang="en"> <head> ...

  6. js-带操作的赋值表达式

    带操作的赋值表达式为: a op= b 这里op代表一个运算符,这个表达式等价于: a = a op b 这里需要特别注意:在第一个表达式中,表达式a计算了一次,而在第二个表达式中,表达式a计算了两次 ...

  7. 小乐乐打游戏(BFS+曼哈顿距离)

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit IO Format: %lld 题目描述 小乐乐觉得学习太简单了,剩下那么多的时间好无聊 ...

  8. 2.Spring的Bean生命周期和组装方式

    1.Spring IoC容器概述 Spring IoC容器: Spring容器即体现了IoC原理    Spring容器通过读取配置元数据负责对Beans实例化.配置和装配     配置元数据可以用X ...

  9. 信息领域热词分析系统--python过滤

    利用python过滤去没用的词语,过滤的词语存储在停用文件中. #创建停用词表 def stopwordlist(): stopwords=[line.strip() for line in open ...

  10. java——快排、冒泡、希尔、归并

    直接贴代码 快排: public class Test { private static void sort(int[] nums){ if(nums == null || nums.length = ...