【问题标题】:How should I use showDialog() and onDialogCreate() in Android?我应该如何在 Android 中使用 showDialog() 和 onDialogCreate()?
【发布时间】:2011-04-12 00:50:08
【问题描述】:

我阅读了this 文档但不明白。

它说我可以使用 showDialog() 来显示一个对话框,系统会调用 onDialogCreate()。

但在下一节中,它说我应该使用 AlertDialog.Builder 的 create() 来创建一个对话框。

我尝试了 AlertDialog.Builer 的 show(),它可以工作并弹出一个对话框。但... 那么我应该在哪里调用 showDialog() 和 onDialogCreate()?

【问题讨论】:

    标签: android dialog


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      Lai Yu-hsuan.... 他们说您可以使用 AlertDialog.Builder.create 创建一个对话框,并使用 showDialog(int) 显示您使用 myBuilder.create() 创建的对话框。所以在代码中:

      protected Dialog onCreateDialog(int id) {
          Dialog dialog;
          switch(id) {
          case DIALOG_MY:
              // do the work to define My Dialog
              dialog= getInstanceMyDialog();
              break;
          default:
              dialog = null;
              break;
          }
          return dialog;
      }
      
      private AlertDialog getInstanceMyDialog() {
          AlertDialog.Builder builder= new AlertDialog.Builder(this);
          builder.setMessage("MyMessage");
          AlertDialog alert= builder.create();
          alert.setTitle("MyTitle");
          return alert;
      }
      

      然后您可以显示如下所示的对话框:

      this.showDialog(DIALOG_MY);
      

      日航

      【讨论】:

        猜你喜欢
        • 2020-07-21
        • 1970-01-01
        • 2020-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-24
        • 1970-01-01
        相关资源
        最近更新 更多