如何在没有任何参考的情况下关闭所有打开的警报对话框

编程入门 行业动态 更新时间:2024-10-26 12:23:00
本文介绍了如何在没有任何参考的情况下关闭所有打开的警报对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的游戏中有重启选项.但问题是当我按下重新启动时,现有活动中可能会有一些打开的警报对话框.

I have a restart option in my game. but the problem is that there may be some open alertdialog in the existing activity when I press restart.

当我按下重启时,我想关闭所有打开的 Alertdialog(如果有).我没有任何参考打开警报对话框(可以是 o、1 或多个).

When I press restart I want to dismiss all open Alertdialog(if any). I do not have any reference to opened up alertdialogs(Can be o,1 or more than one).

有什么方法可以让我随时关闭活动中所有打开的警报对话框而无需任何参考?

Is there any way I can dismiss all the opened up alertdialogs in the activity at any time without having any reference to it ?

推荐答案

首先,您可以将所有对话框分配给一个成员变量,例如

First you could assign all your dialogs to a member variable, e.g.

private Vector<AlertDialog> dialogs = new Vector<AlertDialog>();

@Override
protected Dialog onCreateDialog(int id) {
  switch (id) {
    case DIALOG_ALERT:
      Builder builder = new AlertDialog.Builder(this);
      ...
      AlertDialog dialog = builder.create();
      dialogs.add(dialog);
      dialog.show();
   }
   return super.onCreateDialog(id);
}

之后,您可以使用对话框的 isShowing() 方法测试对话框是否显示(查看类 android.app.Dialog 中的继承方法)http://developer.android/reference/android/app/AlertDialog.html),例如

Afterwards, you can test whether dialogs are showing or not by using the isShowing() method of your dialogs (check out inherited methods from class android.app.Dialoghttp://developer.android/reference/android/app/AlertDialog.html), e.g.

public void closeDialogs() {
   for (AlertDialog dialog : dialogs)
      if (dialog.isShowing()) dialog.dismiss();
}

或者您可能会像Pragnani所说的那样完成并重新开始您的活动.取决于你的重启按钮在哪里......

Or you might finish and start your activity again as Pragnani said. Depends on where your restart button is ...

这篇关于如何在没有任何参考的情况下关闭所有打开的警报对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 03:52:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034892.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有任何   警报   对话框   情况下   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!