`
zkh43javaeye
  • 浏览: 84493 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Dialog 2种显示方式

阅读更多

建议用onCreateDialog(int)和 showDialog(int)这种模式显示Dialog, 系统来管理dialog状态,第一次创建时候,调用 onCreateDialog(int),而 onPrepareDialog(int, Dialog)每次弹出都调用,用来动态改变dialog。

 

When you use this callback, the Android system automatically manages the state of each dialog and hooks them to the Activity, effectively making it the "owner" of each dialog.

 

When you want to show a dialog, call showDialog(int) and pass it an integer that uniquely identifies the dialog that you want to display.

When a dialog is requested for the first time, Android calls onCreateDialog(int) from your Activity, which is where you should instantiate the Dialog . This callback method is passed the same ID that you passed to showDialog(int) . After you create the Dialog, return the object at the end of the method.

Before the dialog is displayed, Android also calls the optional callback method onPrepareDialog(int, Dialog) . Define this method if you want to change any properties of the dialog each time it is opened. This method is called every time a dialog is opened, whereas onCreateDialog(int) is only called the very first time a dialog is opened. If you don't define onPrepareDialog() , then the dialog will remain the same as it was the previous time it was opened. This method is also passed the dialog's ID, along with the Dialog object you created in onCreateDialog() .

 

 

 

 

Example1:
public class ActivityMain extends Activity {
    private static final int DIALOG1 = 1;
    private static final int DIALOG2 = 2;
    private static final int DIALOG4 = 4;
    private static final int DIALOG3 = 3;

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG1:
            return buildDialog1(ActivityMain.this);
        case DIALOG2:
            return buildDialog2(ActivityMain.this);
        case DIALOG3:
            return buildDialog3(ActivityMain.this);
        case DIALOG4:
            return buildDialog4(ActivityMain.this);
        }
        return null;
    }

    protected void onPrepareDialog(int id, Dialog dialog) {
        if (id == DIALOG1) {
            setTitle("测试");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_dialog);

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG1);
            }
        });

        Button buttons2 = (Button) findViewById(R.id.buttons2);
        buttons2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG2);
            }
        });

        Button button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG3);
            }
        });

        Button button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG4);
            }
        });
    }

    private Dialog buildDialog1(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.drawable.alert_dialog_icon);
        builder.setTitle(R.string.alert_dialog_two_buttons_title);
        builder.setPositiveButton(R.string.alert_dialog_ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        setTitle("点击了对话框上的确定按钮: " + whichButton);
                    }
                });
        builder.setNegativeButton(R.string.alert_dialog_cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        setTitle("点击了对话框上的取消按钮: " + whichButton);
                    }
                });

        return builder.create();

    }

    private Dialog buildDialog2(Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.drawable.alert_dialog_icon);
        builder.setTitle(R.string.alert_dialog_two_buttons_msg);
        builder.setMessage(R.string.alert_dialog_two_buttons2_msg);
        builder.setPositiveButton(R.string.alert_dialog_ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        setTitle("点击了对话框上的确定按钮: " + whichButton);
                    }
                });
        builder.setNeutralButton(R.string.alert_dialog_something,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        setTitle("点击了对话框上的进入详细按钮: " + whichButton);
                    }
                });
        builder.setNegativeButton(R.string.alert_dialog_cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        setTitle("点击了对话框上的取消按钮: " + whichButton);
                    }
                });
        return builder.create();
    }

    private Dialog buildDialog3(Context context) {
        LayoutInflater inflater = LayoutInflater.from(this);
        final View textEntryView = inflater.inflate(
                R.layout.alert_dialog_text_entry, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setIcon(R.drawable.alert_dialog_icon);
        builder.setTitle(R.string.alert_dialog_text_entry);
        builder.setView(textEntryView);
        builder.setPositiveButton(R.string.alert_dialog_ok,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        setTitle("点击了对话框上的确定按钮: " + whichButton);
                    }
                });
        builder.setNegativeButton(R.string.alert_dialog_cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        setTitle("点击了对话框上的取消按钮: " + whichButton);
                    }
                });
        return builder.create();
    }

    private Dialog buildDialog4(Context context) {
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setTitle("正在下载歌曲");
        dialog.setMessage("请稍候……");
        return dialog;
    }
}

 

Example2:
public class EX03_17 extends Activity
{
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
 
  public boolean onCreateOptionsMenu(Menu menu)
  {
    menu.add(0, 0, 0, R.string.app_about);
    menu.add(0, 1, 1, R.string.str_exit);
    return super.onCreateOptionsMenu(menu);
  }
 
  public boolean onOptionsItemSelected(MenuItem item)
  {
    super.onOptionsItemSelected(item);
    switch(item.getItemId())
    {
      case 0:
        openOptionsDialog();
        break;
      case 1:
        finish();
        break;
    }
    return true;
  }
 
  private void openOptionsDialog()
  {
    new AlertDialog.Builder(this)
    .setTitle(R.string.app_about)
    .setMessage(R.string.app_about_msg)
    .setPositiveButton(R.string.str_ok,
        new DialogInterface.OnClickListener()
        {
         public void onClick(DialogInterface dialoginterface, int i)
         {
         }
         }
        )
    .show();
  }
}

分享到:
评论

相关推荐

    各种Android Dialog创建及其监听事件实现

    读者可根据JavaDoc API文档来了解如何实现单击item选项监听事件 如下列出的setItems 方法第2个参数易知只要为第2个参数提供OnClickListener实例即可实现监听事件 读者可通过这种方式触类旁通以下几种对话框监听事件...

    Android开发笔记之:Dialog的使用详解

    Dialog是任何系统都必须有的一个控件,作为辅助窗口,用于显示一些消息,或请求用户采取一引起操作等。在Android中也不例外,基本使用可能参看文档。使用时的注意事项1. BACK键能取消掉对话框(dismiss),但是却不会...

    NiceDialog-漂亮的所有常用的dialog都在这.zip

     //显示dialog注意: setMargin()和setWidth()选择一个即可更多用法:1、创建一个继承BaseNiceDialog的类,如果需要传参可仿照如下方式,也是就是常用的Fragment传参方式public class ConfirmDialog extends ...

    Android实现简洁的APP更新dialog数字进度条

    前言:现在一般的Android软件都是需要不断更新的,当你打开某个... 2.自定义控件+Canvas绘画   3.自定义dialog  部分代码:  public class NumberProgressBar extends View { /** * 右侧未完成进度条的颜色 */

    【JavaScript源代码】web面试vue自定义组件及调用方式.docx

    但是在"vant"组件库中, "Dialog 弹出框"组件有2中使用方式 通常我们自定义组件, 一般也是通过方式二的形式使用, 今天介绍方式一如何使用 编码实现 // 将要显示的组件导入 import mymodel from '../components/...

    程序运行过程中等待对话框

    DialogStyle(=dlgNormal, dlgStayOnTop): 等待对话框显示时是否始终在最前面 AviPosition(=aviBottom,aviTop,aviLeft) : AVI的位置 [Options]: showMessage1 :是否显示提示信息1 showMessage2 :是否显示...

    Qt5开发及实例-CH102.rar,Qt5实现计算圆面积的功能的代码

    在lineEdit文本框中输入圆半径值,单击“显示对应圆的面积”按钮后,在label2中显示圆面积值. 实现步骤如下。 (1)首先创建一个新工程。创建过程和本书1.3.1节中的第(1)~(6)步相同,只是在第(3)步中,项目...

    PopupWindow自定义位置显示的实现代码

    关于弹窗的实现大致有以下两种方式AlertDialog和PopupWindow,当然网上也有使用Activity并配合Dialog主题的方式实现弹窗,有兴趣的朋友也可以去研究一下。对于AlertDialog和PopupWindow两者最主要的区别就是显示的...

    Tutorial Dialog SDK 6.0.x Debugging (DA14585/6 DA14531 SDK6调试教程)

    其中堆日志显示部分是官方于2022年2月17日新添加。教程图文并茂,几乎是手把手的方式来教会初学者。适用于 SDK 6.0.16(DA14585/6 和 DA14531)。DA14580已经被官方不建议在新产品中使用,开发者学习和使用SDK6是...

    Android代码-MutilDialogManger

    有没有一种优(tou)雅(lan)的方式来完成这部分的需求呢?没错,这就是我们今天要介绍的东西. 我们想的是需要一个队列来管理弹窗,在各种请求回来之后去往队列中添加我们需要显示的弹窗,第一个弹窗消失了之后在显示...

    zDialog水晶蓝色的弹窗代码.rar

     Dialog显示的内容(三种):1、指向一个URL的iframe窗口;2、页面内隐藏的元素中的html内容;3、直接输出一段html内容;  按ESC键可关闭弹出框;    主调函数参数说明:  Dialog.open({ID,Title,URL,...

    抓包工具 sniffer

    1000=钩选要显示的内容, 用上移或下移按钮排列显示顺序 1002=栏位宽度(像素): [strings] 4=%d 个 TCP/IP 会话 5=, 选定 %d 个 6=创建本文件使用的是 7=选择保存文件的名称 8=数据包摘要 9=无法启动选定网卡上的包...

    AngularDialog:带有对话框的 AngularJS 应用程序 - 试验验证、单元测试和 e2e 测试

    所以我的应用程序有一个表格,显示了基本联系方式的列表 - 名字、姓氏和电子邮件地址。 它有一个添加按钮,可以打开一个对话框,可以在其中输入新的联系人详细信息。 奇怪的是,我的应用程序中的端到端测试似乎工作...

    Android代码-安卓版本更新封装库

    提供2种版本检查方式,在你的项目中添加以下代码即可 使用Dialog `UpdateChecker.checkForDialog(this);` 使用Notification UpdateChecker.checkForNotification(this); 2.添加权限 添加访问网络的权限 `` ...

    Android代码-EasyDialog

    一个DialogFragment的封装库,提供了builder的方式进行调用,因为采用了alertDialog.Builder,所以代码中没有任何自定义,轻量稳定。 简介 原生的Dialog提供了很多Style来让开发者进行自定义,可以满足我们百分之九...

    VC++实现日历的word文档

    可以采用菜单,工具条,DialogBox都可以,自行决定。但是功能要完成:能够让用户选择希望察看的年,月。在用户改变当前显示的年份或者月份之后,客户区中的日历应该改变成对应的日历。 关于对话框 在关于对话框内...

    VC++入门教程、编程-----深入详解二.doc

    每一个属性页都有Back、Next、Cancel按钮,由于每次显示向导的特定页时,MFC自动调用OnSetActive()(设置活动页函数)函数,因此我们可以重载OnSetActive函数,在此函数中利用SetWizardButton(设置属性页按钮显示方式)...

    fckeditor 远程图片保存到本地插件

    口号:开源不是施舍,而是一种合作方式 下载:http://www.cn09.com/remository.html?func=select&id=2 插件介绍: 在FCKeditor中复制网页内容时,其中的图片仍然保存在源站点上,使用该插件可将这些图片文件保存到...

    基于单文档的VC++日历

    可以采用菜单,工具条,DialogBox都可以,自行决定。但是功能要完成:能够让用户选择希望察看的年,月。在用户改变当前显示的年份或者月份之后,客户区中的日历应该改变成对应的日历。 关于对话框 在关于对话框内...

Global site tag (gtag.js) - Google Analytics