在一个Activity中使用多个Dialog
来源:互联网 发布:软件测试压力测试 编辑:程序博客网 时间:2024/05/22 00:59
Android Dev-Guide 推荐重写Activity.onCreateDialog()方法来创建Dialog,这样Dialog就归属于这个Activity了。使用方法是这样的,Activity.showDialog()激发Activity.onCreateDialog()创建Dialog,然后显示之,便于多个Dialog的统一管理。注意,以后再用Activity.showDialog()显示同一个Dialog时,则不会调用Activity.onCreateDialog(),而是调用Activity.onPrepareDialog(),使用上一次显示Dialog时的状态。即
第一次:showDialog() -> onCreatedialog()
以后: showDialog() -> onPrepareDialog()
在示例代码中,分别用createExitDialog(),createListDialog(),createRadioDialog(),createCheckboxDialog(),创建4种Dialog,并在Activity中显示。示例代码如下:
- package com.ipjmc.dialog;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.Toast;
-
- public class ShowDialogActivity extends Activity {
- /** Called when the activity is first created. */
- public static final String TAG = "ShowDialog";
- public static final int ID_EXIT_DIALOG = 1;
- public static final int ID_LIST_DIALOG = 2;
- public static final int ID_RADIO_DIALOG = 3;
- public static final int ID_CHECKBOX_DIALOG = 4;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- showDialog(ID_EXIT_DIALOG);
- showDialog(ID_LIST_DIALOG);
- showDialog(ID_RADIO_DIALOG);
- showDialog(ID_CHECKBOX_DIALOG);
- }
-
- @Override
- protected Dialog onCreateDialog(int id) {
- // TODO Auto-generated method stub
- Dialog dialog = null;
- switch(id) {
- case ID_EXIT_DIALOG :
- dialog = createExitDialog();
- break;
- case ID_LIST_DIALOG :
- dialog = createListDialog();
- break;
- case ID_RADIO_DIALOG :
- dialog = createRadioDialog();
- break;
- case ID_CHECKBOX_DIALOG :
- dialog = createCheckboxDialog();
- break;
- default :
- break;
- }
- if (dialog != null) {
- Log.i(TAG, dialog.toString());
- } else {
- Log.i(TAG, "dialog = null");
- }
- return dialog;
- }
-
- @Override
- protected void onPrepareDialog(int id, Dialog dialog) {
- // TODO Auto-generated method stub
- super.onPrepareDialog(id, dialog);
- }
-
- //创建简单Dialog
- public Dialog createExitDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setMessage("Are you sure you want to exit?")
- .setCancelable(false)
- .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- ShowDialogActivity.this.finish();
- }
- })
- .setNegativeButton("No", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.cancel();
- }
- });
- return builder.create();
- }
-
- //创建ListDialog
- public Dialog createListDialog() {
- final CharSequence[] items = {"Red", "Green", "Blue"};
-
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Pick a color");
- builder.setItems(items, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int item) {
- Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
- }
- });
- return builder.create();
- }
-
- //创建单选Dialog
- public Dialog createRadioDialog() {
- final CharSequence[] items = {"Red", "Green", "Blue"};
-
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Pick a color");
- builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int position) {
- Toast.makeText(getApplicationContext(), position + " -> " + items[position], Toast.LENGTH_SHORT).show();
- dialog.dismiss();
- }
- });
- return builder.create();
- }
-
- //创建多选Dialog
- public Dialog createCheckboxDialog() {
- final CharSequence[] items = {"Red", "Green", "Blue"};
- final boolean [] checked = new boolean [] {false, false, false};
-
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Pick a color");
- builder.setMultiChoiceItems(items, checked, new DialogInterface.OnMultiChoiceClickListener() {
-
- @Override
- public void onClick(DialogInterface dialog, int which, boolean isChecked) {
- // TODO Auto-generated method stub
-
- }
- });
-
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- ShowDialogActivity.this.finish();
- }
- })
- .setNegativeButton("取消", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.cancel();
- }
- });
-
- return builder.create();
-
- }
- }
- package com.ipjmc.dialog;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.Toast;
- public class ShowDialogActivity extends Activity {
- /** Called when the activity is first created. */
- public static final String TAG = "ShowDialog";
- public static final int ID_EXIT_DIALOG = 1;
- public static final int ID_LIST_DIALOG = 2;
- public static final int ID_RADIO_DIALOG = 3;
- public static final int ID_CHECKBOX_DIALOG = 4;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- showDialog(ID_EXIT_DIALOG);
- showDialog(ID_LIST_DIALOG);
- showDialog(ID_RADIO_DIALOG);
- showDialog(ID_CHECKBOX_DIALOG);
- }
- @Override
- protected Dialog onCreateDialog(int id) {
- // TODO Auto-generated method stub
- Dialog dialog = null;
- switch(id) {
- case ID_EXIT_DIALOG :
- dialog = createExitDialog();
- break;
- case ID_LIST_DIALOG :
- dialog = createListDialog();
- break;
- case ID_RADIO_DIALOG :
- dialog = createRadioDialog();
- break;
- case ID_CHECKBOX_DIALOG :
- dialog = createCheckboxDialog();
- break;
- default :
- break;
- }
- if (dialog != null) {
- Log.i(TAG, dialog.toString());
- } else {
- Log.i(TAG, "dialog = null");
- }
- return dialog;
- }
- @Override
- protected void onPrepareDialog(int id, Dialog dialog) {
- // TODO Auto-generated method stub
- super.onPrepareDialog(id, dialog);
- }
- //创建简单Dialog
- public Dialog createExitDialog() {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setMessage("Are you sure you want to exit?")
- .setCancelable(false)
- .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- ShowDialogActivity.this.finish();
- }
- })
- .setNegativeButton("No", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.cancel();
- }
- });
- return builder.create();
- }
- //创建ListDialog
- public Dialog createListDialog() {
- final CharSequence[] items = {"Red", "Green", "Blue"};
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Pick a color");
- builder.setItems(items, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int item) {
- Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
- }
- });
- return builder.create();
- }
- //创建单选Dialog
- public Dialog createRadioDialog() {
- final CharSequence[] items = {"Red", "Green", "Blue"};
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Pick a color");
- builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int position) {
- Toast.makeText(getApplicationContext(), position + " -> " + items[position], Toast.LENGTH_SHORT).show();
- dialog.dismiss();
- }
- });
- return builder.create();
- }
- //创建多选Dialog
- public Dialog createCheckboxDialog() {
- final CharSequence[] items = {"Red", "Green", "Blue"};
- final boolean [] checked = new boolean [] {false, false, false};
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("Pick a color");
- builder.setMultiChoiceItems(items, checked, new DialogInterface.OnMultiChoiceClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which, boolean isChecked) {
- // TODO Auto-generated method stub
- }
- });
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- ShowDialogActivity.this.finish();
- }
- })
- .setNegativeButton("取消", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.cancel();
- }
- });
- return builder.create();
- }
- }
- 在一个Activity中使用多个Dialog
- 在一个Activity中使用多个Dialog
- 在一个Activity中使用多个Dialog
- 在一个Activity中使用多个Dialog
- Dialog基础(在一个Activity中创建多个Dialog)
- Android中一个Activity关闭另一个Activity或者在一个Activity中关闭多个Activity
- 在一个Activity中使用多个View (可以借鉴的一种思路)
- Android中在一个应用程序中定义多个Activity,及启动多个Activity
- 在一个页面中显示多个activity
- 在一个activity中添加多个listview
- 在一个页面中显示多个activity
- 在一个页面中显示多个activity
- 在一个activity中添加多个listview
- 在一个Activity中切换多个layout
- 在某个Activity中关闭其他Activity页面,一个或多个
- 在广播中启动activity或者dialog
- Activity中使用Dialog的方式
- 在Dialog中使用Toolbar
- C++ next_permutation 源码分析
- 《玩转.NET Micro Framework 移植-基于STM32F10x处理器》资源汇总
- Linux 文件内容大小写转换
- UBUNTU添加开机自动启动程序方法
- Objective-C的self.用法的一些总结
- 在一个Activity中使用多个Dialog
- ShoppingMall客户端改进计划
- windows 2003无法播放flv
- java.util.Queue接口add()和remove(),add()和remove(),element()或者peek()区别
- Linux 文件编码转换
- 信息化时代下的我们----弄潮儿
- 改进MySQL Order By Rand()的低效率
- VS2010设置VC6的字体样式及背景色、选中字高亮
- squid digest storage 之Bloom Filter概念和原理