我问的是以下链接之前提出的相同问题,但是这些链接中提出的解决方案对我不起作用,所以我再次发布。
如何使AlertDialog消失?
当我单击OK按钮
时,Android Alertialog总是退出用户单击AlertDialog OK按钮后如何导航到下一个活动?
基本上,我正在创建一个AlertDialog构建器,以通知用户,要求启用使用情况数据访问的设置以及按下确定按钮时,然后打开"设置菜单"。当我按下返回按钮重返应用程序时,尽管我希望被解雇会重返应用程序。
public void show_alert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This application requires access to the Usage Stats Service. Please " +
"ensure that this is enabled in settings, then press the back button to continue ");
builder.setCancelable(true);
builder.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
dialog.dismiss();
}
});
builder.show();
return;
}
有什么提示在这里怎么了?
在某些测试后编辑:
我在6.0.1上测试了OPS代码,并且表现为预期的 - 即,在单击"确定"后解散了对话框。我将在下面留下最初的答案作为替代方案。可以在此处找到其他替代方案。
您可以从builder.show()
方法中获取对您的警报对话框的引用:
mMyDialog = builder.show();
在您的onClick
方法中:
mMyDialog.dismiss();
完整样本:
AlertDialog mMyDialog; // declare AlertDialog
public void show_alert(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("This application requires access to the Usage Stats Service. Please " +
"ensure that this is enabled in settings, then press the back button to continue ");
builder.setCancelable(true);
builder.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivity(intent);
mMyDialog.dismiss(); // dismiss AlertDialog
}
});
mMyDialog = builder.show(); // assign AlertDialog
return;
}