当前位置:操作系统 > 安卓/Android >>

Android ApiDemos示例解析(22):App->Dialog

这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的:

显示消息给用户,并可提供一到三个按钮(OK, Cancel ,Yes ,No)用于选择或是显示警告。
显示一个列表以供用户选择,列表中可以是Radio Button  (单选),Check button (多选)
显示文本框来接受用户输入等。
创建AlertDialog一般是通过AlertDialog.Builder来构造:

 

[java] 
AlertDialog.Builder ad=new AlertDialog.Builder(context); 
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。

[java] view plaincopyprint?
private static final int DIALOG_YES_NO_MESSAGE = 1; 
private static final int DIALOG_YES_NO_MESSAGE = 1;

然后重载onCreateDialog,参数id为Dialog ID,可以根据id来创建需要的Dialog实例。

[java]
@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(); 
  
 ... 
@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

[java] 
showDialog(DIALOG_YES_NO_MESSAGE); 
showDialog(DIALOG_YES_NO_MESSAGE);

 

App->Dialog通过八个例子来说明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 来显示这些选项:
[java] 
1. case DIALOG_LIST: 
2.  return new AlertDialog.Builder(AlertDialogSamples.this) 
3.  .setTitle(R.string.select_dialog) 
4.  .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() { 
5.  public void onClick(DialogInterface dialog, int which) { 
6.   
7.  /* User clicked so do some stuff */ 
8.  String[] items = getResources().getStringArray(R.array.select_dialog_items); 
9.  new AlertDialog.Builder(AlertDialogSamples.this) 
10.  .setMessage("You selected: " + which + " , " + items[which]) 
11.  .show(); 
12.  } 
13.  }) 
14.  .create(); 
 
 

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


-(。

注: 这个例子还使用里Handler 来更新进度条,Handler将在后面的例子介绍。
[java] 
1. case DIALOG_PROGRESS: 
2.  mProgressDialog = new ProgressDialog(AlertDialogSamples.this); 
3.  mProgressDialog.setIcon(R.drawable.alert_dialog_icon); 
4.  mProgressDialog.setTitle(R.string.select_dialog); 
5.  mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
6.  mProgressDialog.setMax(MAX_PROGRESS); 
7.  mProgressDialog.setButton(getText(R.string.alert_dialog_hide), 
8.  new DialogInterface.OnClickListener() { 
9.  public void onClick(DialogInterface dialog, int whichButton) { 
10.   
11.  /* User clicked Yes so do some stuff */ 
12.  } 
13.  }); 
14.  mProgressDialog.setButton2(getText(R.string.alert_dialog_cancel), 
15.  new DialogInterface.OnClickListener() { 
16.  public void onClick(DialogInterface dialog, int whichButton) { 
17.   
18.  /* User clicked No so do some stuff */ 
19.  } 
20.  }); 
21.  return mProgressDialog; 
 
Single choice list
AlertDialog 显示列表时,可以指定为单选或是多选。将前面List中的setItems方法改成setSingleChoiceItems可以使用RadioButton来显示列表:
 
 
[java] 
1. case DIALOG_SINGLE_CHOICE: 
2.  return new AlertDialog.Builder(AlertDialogSamples.this) 
3.  .setIcon(R.drawable.alert_dialog_icon) 
4.  .setTitle(R.string.alert_dialog_single_choice) 
5.  .setSingleChoiceItems(R.array.select_dia

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,