本文介绍了Android的:如何覆盖onBack pressed()在AlertDialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 AlertDialog dlgDetails 这是从另一个显示 AlertDialog dlgMenu 。我希望能够再次显示dlgMenu如果用户presses在dlgDetails后退按钮,并退出该对话框,如果他在$对话外p $ psses。

I have an AlertDialog dlgDetails which is shown from another AlertDialog dlgMenu. I would like to be able to show dlgMenu again if the user presses the back button in dlgDetails and simply exit the dialog if he presses outside the dialog.

我觉得要做到这一点的最好办法是重写 onBack pressed 的dlgDetails,但我不知道该怎么做,因为AlertDialogs必须间接创造使用生成器。

I think the best way to do this is to override onBackPressed for dlgDetails, but I am not sure how to do that since AlertDialogs must be created indirectly using the Builder.

我想创建一个派生AlertDialog(公共类AlertDialogDetails扩展AlertDialog {...} ),但是我想我还必须扩展 AlertDialog.Builder 在该类返回一个AlertDialogDetails,但是是不是有一个更简单的方法?如果没有,你会怎么去覆盖生成器?

I am trying to create a derived AlertDialog (public class AlertDialogDetails extends AlertDialog { ...} ) but then I guess I must also extend AlertDialog.Builder in that class to return an AlertDialogDetails, but isn't there a simpler way? And if not, how would you go about overriding the Builder?

推荐答案

我终于增加了一个按键侦听到我的对话听的返回键。不一样优雅的覆盖 onBack pressed(),但它的工作原理。这里是code:

I finally added a key listener to my dialog to listen to the Back key.Not as elegant as overriding onBackPressed() but it works.Here is the code:

dlgDetails = new AlertDialog.Builder(this)
    .setOnKeyListener(new DialogInterface.OnKeyListener() {
        @Override
        public boolean onKey (DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK && 
                event.getAction() == KeyEvent.ACTION_UP && 
                !event.isCanceled()) {
                dialog.cancel();
                showDialog(DIALOG_MENU);
                return true;
            }
            return false;
        }
    })
    //(Rest of the .stuff ...)

这篇关于Android的:如何覆盖onBack pressed()在AlertDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 23:28