【问题标题】:AlertDialog showing multiple Dialogs on top of each other?AlertDialog 在彼此之上显示多个对话框?
【发布时间】:2016-05-10 00:33:12
【问题描述】:

我的 GPSTracker 中有这个方法:

public void showSettingsAlert() {
    final AlertDialog.Builder alertDialog = new AlertDialog.Builder(myContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS Settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // Setting Icon to Dialog
    // alertDialog.setIcon(R.drawable.delete);

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            myContext.startActivity(intent);
        }
    });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });

    alertDialog.show();
}

当我运行它时,我会不断地同时出现 10 个对话框,所以基本上我可以按 10 次取消或类似的东西,我该如何解决? 我读到了

final AlertDialog alert = alertDialog.create();
if(alert.isShowing()) {
alert.dismiss();
}
else {
alert.show();
}

但这对我不起作用,我仍然有多个对话...有人可以帮帮我吗?基本上我在打电话 if(gps.isEnabled()) gps.showSettingsAlert();在我的活动中。

if (itemsArrayList.get(position).getCoordinates() == null || !gps.canGetLocation()) {
            holder.distance.setText("Distance not available");
            gps.showSettingsAlert();
        }

【问题讨论】:

  • 请发布更多您的代码。到目前为止您发布的代码没有任何问题,因此问题出在其他地方。如果您发布更多代码,它将使某人更容易识别问题。
  • 张贴你的电话showSettingsAlert的代码
  • 我添加了调用 showSettingsAlert 的代码。

标签: android dialog gps android-alertdialog


【解决方案1】:

我认为 Phan Văn Linh 提供的解决方案是关键,只需稍加修改即可。

  • 首先将 alertDialog 作为您的活动或应用程序的全局变量(取决于您)并将其初始化为 null。

    私有 AlertDialog.Builder _alertDialog = null;

null 初始化将帮助您检查是否创建了 alertDialog 的实例。

  • 然后像这样制作 showSettingDialgo:

    公共无效 showSettingsAlert() { if( _alertDialog != null){//这里我们检查一个实例是否显示 返回; }

    _alertDialog = new AlertDialog.Builder(myContext); // 设置对话框标题 ... _alertDialog.show(); }

  • 最后,当对话框被关闭时,再次使实例为空。

    _alertDialog = null;

【讨论】:

  • 我无法让它工作,它仍然向我展示了很多对话框。我有 public class GpsTracker extends Service implements LocationListener { } 我的 showSettingsAlert 在里面,然后得到了我的 ListAdapter,我需要在那里显示 GPS...
  • 您能否提供更多有关 yopur GPSTracker 的代码以及如何使用它?
【解决方案2】:

alertDialog 设为 Activity 类的全局变量
然后在showSettingsAlert(),你可以查看警报是否显示。

private AlertDialog alertDialog;
...
public void showSettingsAlert() {
    if( alertDialog != null && alertDialog.isShowing() ){
       return;
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(context);    
    // Setting Dialog
    //builder.setTitle(...)
    //builder.setMessage...
    //builder.setPositiveButton...
    //builder.setNegativeButton...   
    ...
    alertDialog= builder.create();
    alertDialog.show();
}

希望有帮助

【讨论】:

  • isShowing() 方法不适用于 AlertDialog.Builder :(
  • @HappyR 你是对的,我的错。我已经更新了我的答案。请检查一下
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多