Android 防止后退按钮关闭对话框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12150646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent back button from closing a dialog box
提问by CaptainProg
I am trying to prevent an AlertDialog box from closing when pressing the back button in Android. I have followed both of the popular methods in this thread, and with System.out.println I can see that in both cases the code executes. However, the back button stillcloses the dialog box.
我试图防止在 Android 中按下后退按钮时关闭 AlertDialog 框。我遵循了这个线程中的两种流行方法,并且使用 System.out.println 我可以看到在这两种情况下代码都执行。但是,后退按钮仍会关闭对话框。
What could I be doing wrong? Ultimately I'm trying to prevent the back button closing a dialog box - it is a disclaimer that is displayed the first time the app is run and I don't want the user to have any option but to press the "Accept" button in order for the app to continue.
我可能做错了什么?最终,我试图阻止后退按钮关闭对话框 - 这是第一次运行应用程序时显示的免责声明,我不希望用户有任何选择,只能按下“接受”按钮为了让应用程序继续。
回答by Sam
Simply use the setCancelable()
feature:
只需使用该setCancelable()
功能:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
This prevents the back button from closing the dialog, but leaves the "negative" button intact if you chose to use it.
这可以防止后退按钮关闭对话框,但如果您选择使用它,“否定”按钮将保持不变。
While any user that does not want to accept your terms of service can push the home button, in light of Squonk's comment, here two more ways to prevent them from "backing out" of the user agreement. One is a simple "Refuse" button and the other overrides the back button in the dialog:
虽然任何不想接受您的服务条款的用户都可以按下主页按钮,但根据 Squonk 的评论,这里还有两种方法可以防止他们“退出”用户协议。一个是简单的“拒绝”按钮,另一个覆盖对话框中的后退按钮:
builder.setNegativeButton("Refuse", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
finish();
return false;
}
});
回答by falvojr
To prevent the back button closes a Dialog (without depending on Activity), just the following code:
为了防止后退按钮关闭一个Dialog(不依赖Activity),只需如下代码:
alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
// Prevent dialog close on back press button
return keyCode == KeyEvent.KEYCODE_BACK;
}
});
回答by Ognyan
When using DialogFragment
you will need to call setCancelable()
on the fragment, not the dialog:
使用时,DialogFragment
您需要调用setCancelable()
片段,而不是对话框:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
dialog = new ProgressDialog(getActivity());
dialog.setIndeterminate(true);
dialog.setMessage(...);
setCancelable(false);
return dialog;
}
Calling dialog.setCancelable()
seem to have no effect. It seems that DialogFragment
does not takes notice of the dialog's setting for cancelability.
调用dialog.setCancelable()
似乎没有效果。似乎DialogFragment
没有注意到对话框的可取消性设置。
回答by David
Use setCancelable(false)
用 setCancelable(false)
SampleDialog sampleDialog = SampleDialog.newInstance();
sampleDialog.setCancelable(false);
sampleDialog.show(getSupportFragmentManager(), SampleDialog.class.getSimpleName());
回答by ban-geoengineering
Only this worked for me:
只有这对我有用:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(title);
builder.setMessage(content);
/**
* Make it when the Back button is pressed, the dialog isn't dismissed.
*/
builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
Utilities.makeToast(getContext(), "There is no way back!");
return true; // Consumed
}
else {
return false; // Not consumed
}
}
});
Dialog dialog = builder.create();
/**
* Make it so touching on the background activity doesn't close the dialog
*/
dialog.setCanceledOnTouchOutside(false);
return dialog;
As you can see, I also added a dialog.setCanceledOnTouchOutside(false);
line so tapping outside of the dialog doesn't result in it being closed either.
如您所见,我还添加了dialog.setCanceledOnTouchOutside(false);
一行,因此在对话框外轻敲也不会导致它被关闭。
回答by Ricardo Saracino
In JQuery Mobile a popup adds a hash to the url, the following code allows the back to dismiss the popup when open and return to the app when closed. You could use the same logic for a custom ui framework.
在 JQuery Mobile 中,弹出窗口向 url 添加了一个哈希值,以下代码允许返回在打开时关闭弹出窗口并在关闭时返回到应用程序。您可以对自定义 ui 框架使用相同的逻辑。
@Override
public void onBackPressed() {
// check if modal is open #&ui-state=dialog
if (webView.getVisibility() == View.VISIBLE && webView.getUrl().contains("#&ui-state=dialog")) {
// don't pass back button action
if (webView.canGoBack()) {
webView.goBack();
}
} else {
// pass back button action
super.onBackPressed();
}
}