这个例子的主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();