【问题标题】:display dialog of another activity in android在android中显示另一个活动的对话框
【发布时间】:2012-07-20 10:40:13
【问题描述】:

从活动 1,我打开活动 2。现在,当我在活动 2 的屏幕中时,我收到活动 1 的事件,并且在收到该事件时我想显示一个对话框。我怎样才能做到这一点?? 当我在活动 2 中时,我没有看到对话框,但是当我回到活动 1 时,我看到了对话框。

即使我在活动 2 的屏幕中,我也想看到对话框。 我也查看了此链接,但没有太大帮助 Showing dialog on top of another running activity (Android)? 在收到 reqd 事件时,我调用此方法。

【问题讨论】:

    标签: android android-activity dialog


    【解决方案1】:

    首先在 Activity 2 中创建单独的对话框,然后执行下面提到的操作

    1. 在收到与活动 1 相关的数据时尝试发送广播。
    2. 创建一个接收广播的接收器类
    3. 现在您必须使用观察者设计模式,即当 Receiver 类中发生某些事件时,它将通知 Activity B 数据已到达并显示相应的对话框。

    如果不成功,请分享结果。

    【讨论】:

      【解决方案2】:

      在你的第一个活动(splashScreen)中编写这个函数来创建警报框

      public static void MyAlertBox(String title, String mymessage,  Context context)
      {
      new AlertDialog.Builder(context)
         .setMessage(mymessage)
         .setTitle(title)
         .setCancelable(false)
         .setPositiveButton("OK", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
              .
              }
          })
         .show();
      }
      

      声明全局可验证:

      public static Activity currentActivity= null;
      

      在每个活动中: onResume() 写“currentActivity=this;”

      你想在哪里显示警报只需写:(活动 1)

      SplashScreen.MyAlertBox("Alert",
                  "Alert box from activity 1",currentActivity );
      

      希望对你有用!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多