Dialog的创建
代码来自于APIDemo
?
1.最简单的OK/Cancel的弹出框
//创建新的弹出框 new AlertDialog.Builder(AlertDialogSamples.this) //设置弹出框的图标 .setIcon(R.drawable.alert_dialog_icon) //弹出框的标题 .setTitle(R.string.alert_dialog_two_buttons_title) //OK按钮的设置,以及对应的onClick事件 .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) {//onClick事件的具体实现 } }) //Cancel按钮的设置,以及对应的onClick事件 .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { //onClick事件的具体实现 } }) .create();?效果图:
?
2.带有List的弹出框
//创建带有Item的弹出框 return new AlertDialog.Builder(AlertDialogSamples.this) //设置弹出框的标题 .setTitle(R.string.select_dialog) //设置item的具体内容,R.array.select_dialog_items是定义在XML中的文件,也可以是new String[]{"a","b" ……}的数组 .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() { //选中Item时产生的事件 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();?
?对应的XML文件:
?
<xml version="1.0" encoding="utf-8"?><resources> <!-- 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></resources>
效果图:

?
?
3.进度条式的弹出框的实现//创建一个进度条式的弹出框
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) { } }); mProgressDialog.setButton2(getText(R.string.alert_dialog_cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } });效果图:

?
?
4.单选式弹出框的实现
?
//单选按钮式的弹出框 return new AlertDialog.Builder(AlertDialogSamples.this) .setIcon(R.drawable.alert_dialog_icon) .setTitle(R.string.alert_dialog_single_choice) //设置单选按钮的集合,同理该处可以来自于XML,也可以是String[]{}的实现 .setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .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();效果图:

?
?
?
5.多选弹出框的实现
?
//其道理与上相同,只是一个单选一个多选 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) { } }) .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();效果图:

?
?
?
6.自定义布局弹出框?
//用于加载布局文件
LayoutInflater factory = LayoutInflater.from(this); //从XML中加载布局 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();效果图:

?
?