我有一个警告对话框,为用户提供两个选项:yes
和no
。选择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();
答案 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]
设置对话框是否可取消。默认为真。