这个例子的主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. Hbase0.98.0完全分布式搭建---【使用外部zookeeper】

    Hbase是一个分布式的实时数据库,他可以基于hadoop的hdfs,S3等分布式存储系统.而且使用zookeeper来通信(查询元数据和获取数据所在位置等功能) 本文的Hbase使用的是hadoop ...

  2. 阿里云服务器部署Tornado应用指南

    本篇详细介绍tornado应用部署到阿里云服务器上的全过程. Tornado程序地址:github https://github.com/ddong8/ihasy.git 准备工作:阿里云服务器Cen ...

  3. js获取复选框内容

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script sr ...

  4. mybatis逆向工程工具

    mybatis逆向工程 package com.cxy; import java.io.File; import java.util.*; import org.mybatis.generator.a ...

  5. 【Cracking the Code Interview(5th edition)】一、数组与字符串(C++)

    1.1 实现一个算法,确定一个字符串的所有字符是否全都不同.不允许使用额外的数据结构. 解答:这里假定字符集为ASCII码,可以与面试官沟通确认字符串使用的字符集.由于字符集是有限的,建立一个数组模拟 ...

  6. ie浏览器(Internet Explorer)不播放背景音乐

    这里就不说bgsound了,支持格式较少 一个网站要加背景音乐,好些年没加背景音乐了,用embed标签把背景音乐加上了,Mozilla Firefox,Google Chrome,Safari都正常, ...

  7. Diophantus of Alexandria

    Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of the first ma ...

  8. esper(2)-事件类型

    1.pojo package com.ebc.eventtype.pojo.pojo1; import cn.hutool.core.collection.CollUtil; import com.e ...

  9. Smarty保留变量信息

    对php里边的超级全局数组变量信息的使用 例如:$_GET.$_POST.$_SESSION.$_COOKIE.$_REQUEST.$_SERVER.$_ENV.$GLOBALS.$_FILES.常量 ...

  10. shematool -initschema -dbtype mysql error org.apache.hadoop.hive.metastore.hivemetaexception:Failed to get schema version

    命令:schematool -initSchema -dbType mysql Fix the issue: edit /etc/mysql/my.cnf change bind-address   ...