AlertDialog不会关闭

时间:2014-04-18 11:55:58

标签: java android

我有一个警告对话框,为用户提供两个选项:yesno。选择no后,AlertDialog会关闭,但选择yes后会让用户在线向他们发送指示,但当我返回应用程序时,AlertDialog仍然是可见,当点击是时,我似乎无法自动消失。以下是我的代码:

AlertDialog.Builder alert_confirm = new AlertDialog.Builder(TrackingServiceActivity.this);
alert_confirm.setMessage("Are you sure you want directions to " + data.name +"! Tracking will be suspended if it has started!");
alert_confirm.setCancelable(false).
alert_confirm.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                getDirections(latitude, longitude, data.lat, data.lon);
                stopTracker();
                return;
            }
        });

alert_confirm.setNegativeButton("No",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                return;
            }
        });

AlertDialog alert = alert_confirm.create();
alert.show();

4 个答案:

答案 0 :(得分:0)

在“正向”按钮单击侦听器

中使用此功能
getDirections(latitude, longitude, data.lat, data.lon);
stopTracker();
dialog.dismiss();

答案 1 :(得分:0)

在Yes侦听器中使用dialog.dismiss();

 public void onClick(DialogInterface dialog, int which) {
                                        getDirections(latitude, longitude, data.lat, data.lon);
                                        stopTracker();
                                        dialog.dismiss();
                                        return;
                                    }

答案 2 :(得分:0)

你错过了dismiss();电话。就这样做:

AlertDialog.Builder alert_confirm = new AlertDialog.Builder(TrackingServiceActivity.this);
alert_confirm.setMessage("Are you sure you want directions to " + data.name +"! Tracking will be suspended if it has started!");
alert_confirm.setCancelable(false).
alert_confirm.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                getDirections(latitude, longitude, data.lat, data.lon);
                stopTracker();
                dismiss();
                return;
            }
        });

alert_confirm.setNegativeButton("No",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                return;
            }
        });

AlertDialog alert = alert_confirm.create();
alert.show();

答案 3 :(得分:0)

删除此行

alert_confirm.setCancelable(false);

[setCancelable:][1]设置对话框是否可取消。默认为真。