简体   繁体   English

AlertDialog将不会关闭

[英]AlertDialog will not close

I have an alert dialog, which offers the user two options: yes and no . 我有一个警报对话框,为用户提供两个选项: yesno When no is selected it the AlertDialog closes but when yes is selected it takes the user online to give them directions but when I go back to the application the AlertDialog is still visible and I can't seem to make it disappear automatically when yes is clicked. 如果no被选中的AlertDialog关闭,但是当yes选择它需要用户在网上给他们的方向,但是当我回到应用程序的AlertDialog是仍然可见,我似乎无法让它自动消失被点击时是。 Below is my code: 下面是我的代码:

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();

use this in Positive button click listner 在正按钮点击列表器中使用它

getDirections(latitude, longitude, data.lat, data.lon);
stopTracker();
dialog.dismiss();

use dialog.dismiss(); 使用dialog.dismiss(); in Yes listener. 在是的听众中。

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

You've missed dismiss(); 您错过了dismiss(); call. 呼叫。 Just do it like this : 像这样做:

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();

Remove this line 删除此行

alert_confirm.setCancelable(false);

[setCancelable:][1] Sets whether the dialog is cancelable or not. [setCancelable:][1]设置对话框是否可取消。 Default is true. 默认为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM