Android 对话框和浮动Activity
2010-08-14 00:32:00 来源:WEB开发网核心提示:AlertDialog dialog = new AlertDialog.Builder(context).create(); dialog.setMessage(”Do you play cricket?”); dialog.setButton(”Yes”, myOnClickListener); dialog.se
AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.setMessage(”Do you play cricket?”); dialog.setButton(”Yes”,
myOnClickListener); dialog.setButton2(”No”, myOnClickListener); dialog.show();
myOnClickListener这个事件监听器的代码可以类似如下: public void onClick(DialogInterface dialog,
int i) { switch (i) { case AlertDialog.BUTTON1: break; case AlertDialog.BUTTON2:
break; } } AlertDialog.Builder: AlertDialog类中有一个内部类,名为
‘Builder’,Builder类提供了为对话框添加多选或单选列表,以及为这些列表添加事件处理的功能。另外,这个Builder类将
AlertDialog对话框上的3个按钮按照他们的位置分别称呼为:PositiveButton, NeutralButton, NegativeButton
下列代码是一个多选列表的例子: new AlertDialog.Builder(context) .setIcon(R.drawable.icon)
.setTitle(R.string.alert_dialog_multi_choice) .setMultiChoiceItems(
R.array.select_dialog_items, new boolean[]{false, true, false, true, false}, new
DialogInterface.OnMultiChoiceClickListener() { public void
onClick(DialogInterface dialog, int whichButton, boolean isChecked) { }
}).setPositiveButton(R.string.alert_dialog_ok, new
DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,
int whichButton) { } }).setNegativeButton(R.string.alert_dialog_cancel, new
DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,
int whichButton) { } } ).create(); Activity托管对话框:
Android也提供了创建对话框的快捷方式,在Activity中可以通过如showDialog(), onCreateDialog(),
onPrepareDialog(),dismissDialog(), removeDialog()等方法来创建和管理对话框。
Activity的onCreateDialog方法用于在创建并显示对话框的时候调用,比如: @Override protected Dialog
onCreateDialog(int id) { return new AlertDialog.Builder(this).setMessage(”How
are you?”).setPositiveButton( ”Fine”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { }
}).setNegativeButton(”Not so good”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { } } ).create(); }
[]
赞助商链接