Android 如何处理对话框中的后退按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10346011/
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
How to handle Back button with in the dialog?
提问by kiran
I am developing an application that when the button is pressed, it opens a dialog with OK and Cancel buttons.
我正在开发一个应用程序,当按下按钮时,它会打开一个带有“确定”和“取消”按钮的对话框。
It works fine.
它工作正常。
When the user presses the back button, I am handling this as follows
当用户按下后退按钮时,我按如下方式处理
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
}
return super.onKeyDown(keyCode, event);
}
But the above method is not called. How can I handle this?
但是上面的方法并没有被调用。我该如何处理?
回答by Yasin Hassanien
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
dialog.dismiss();
}
return true;
}
});
回答by alexc
Sounds like you want to set the OnCancelListener when you create the Dialog. It looks like this:
听起来您想在创建对话框时设置 OnCancelListener。它看起来像这样:
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//do whatever you want the back key to do
}
});
回答by Never Quit
You need to override OnCancel
method. This method calls on Back Key press. Here's code which works perfect to me.
您需要覆盖OnCancel
方法。此方法调用返回键按下。这是对我来说很完美的代码。
AlertDialog alertDialog;
alertDialog.setOnCancelListener(new OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog)
{
// TODO Auto-generated method stub
dialog.dismiss();
}
});
Hope this will help you, and accept it if it is helpful to you.
希望对你有帮助,如果对你有帮助,请采纳。
Thanks..
谢谢..
回答by NitZRobotKoder
Try this
尝试这个
new AlertDialog.Builder(this).setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
Logger.d(TAG, "--------- Do Something -----------");
return true;
}
return false;
}
})
回答by Daniel Wilson
If you are using a DialogFragment, from what I can tell the right way to do it is to override onCancel()
如果您使用的是 DialogFragment,那么我可以告诉正确的方法是覆盖 onCancel()
I noticed setOnCancelListener
does not work, and setOnKeyListener
works, but for me has the fun side effect that it swallows all keys if your dialog has an edit text.
我注意到setOnCancelListener
不起作用,并且setOnKeyListener
有效,但对我来说有一个有趣的副作用,如果您的对话框有编辑文本,它会吞下所有键。
回答by santamoniya
Override method onBackPressed()
in your own dialog and use it in your code:
onBackPressed()
在您自己的对话框中覆盖方法并在您的代码中使用它:
public class MyDialog extends Dialog {
public MyDialog(@NonNull Context context) {
super(context);
}
@Override
public void onBackPressed() {
// Do what you want
}
}
Use:
用:
MyDialog dlg = new MyDialog(context);
dlg.show();
回答by Mohammed Azharuddin Shaikh
it is because when your Dialog opens then your window navigate its focused to Dialog.
So now you have to handle key
on your Dialog.
这是因为当您的 Dialog 打开时,您的窗口将其焦点导航到 Dialog。所以现在你必须处理key
你的对话框。
回答by Andrew Glukhoff
This code works:
此代码有效:
Dialog dlg = new Dialog(thisAct, R.style.DialogTheme);
dlg.setContentView(view);
dlg.setCancelable(false);
dlg.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
dlg.setOnKeyListener((arg0, keyCode, event) -> {
Timber.d("onKey(%d)", keyCode);
//{home intercepting
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Timber.i("HOME pressed");
return true;
}
return true;
});
dlg.show();