Android:如何覆盖AlertDialog中的onBackPressed()?

发布于 2024-12-10 04:41:06 字数 457 浏览 0 评论 0原文

我有一个 AlertDialog dlgDetails,它是从另一个 AlertDialog dlgMenu 显示的。如果用户按下 dlgDetails 中的后退按钮,我希望能够再次显示 dlgMenu,并且如果用户按下对话框之外的按钮,则只需退出对话框。

我认为最好的方法是重写 dlgDetails 的 onBackPressed ,但我不确定如何做到这一点,因为必须使用生成器间接创建 AlertDialogs 。

我正在尝试创建一个派生的 AlertDialog (public class AlertDialogDetails extends AlertDialog { ...} ),但我想我还必须在该类中扩展 AlertDialog.Builder 才能返回AlertDialogDetails,但是有没有更简单的方法?如果没有,您将如何覆盖构建器?

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.

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

玩套路吗 2024-12-17 04:41:06

我最终在对话框中添加了一个按键侦听器来侦听“后退”键。
不像重写 onBackPressed() 那样优雅,但它可以工作。
下面是代码:

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 ...)

有关 Kotlin 中的答案,请参见此处:当 Alertdialog 的 setcancelable 为 false 时,无法在backpressed 上工作

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 ...)

For answer in Kotlin see here:Not working onbackpressed when setcancelable of alertdialog is false

晨与橙与城 2024-12-17 04:41:06

找到了一个更短的解决方案:)试试这个:

         accountDialog = builder.create();

        accountDialog.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                dialog.dismiss();
                activity.finish();

            }
        });

Found a shorter solution :) try this:

         accountDialog = builder.create();

        accountDialog.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                dialog.dismiss();
                activity.finish();

            }
        });
百变从容 2024-12-17 04:41:06

这会处理“后退”按钮和对话框外部的单击:

yourBuilder.setOnCancelListener(new OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        dialog.cancel();
        // do your stuff...
    }
});

dialog.cancel() 是关键:使用 dialog.dismiss() 这只会处理对话框外部的单击对话框,如上面回答。

This handles both the BACK button and the click OUTSIDE the dialog:

yourBuilder.setOnCancelListener(new OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        dialog.cancel();
        // do your stuff...
    }
});

dialog.cancel() is the key: with dialog.dismiss() this would handle only the click outside of the dialog, as answered above.

残疾 2024-12-17 04:41:06

我在 java 类中创建了一个新函数,并从对话框生成器的 onClick 方法调用了该函数。

public class Filename extends Activity(){

@Override
onCreate(){
 //your stuff
 //some button click launches Alertdialog
}

public void myCustomDialog(){
 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
 //other details for builder
      alertDialogBuilder.setNegativeButton("BACK",
            new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
                         dialod.dismiss;
                         myCustomBack();
                    }
      });

 AlertDialog alertDialog = alertDialogBuilder.create();
 alertDialog.setCanceledOnTouchOutside(true);
 alertDialog.show();
}

public void myCustomBack(){
  if(condition1){
    super.onBackPressed();
  }
  if(condition 2){
    //do  stuff here
  }
}

@Override
public void onBackPressed(){
  //handle case here
  if (condition A)
    //Do this
  else 
    //Do that
}

}

I created a new function within the java class and made a call to that function from the onClick method of the dialog Builder.

public class Filename extends Activity(){

@Override
onCreate(){
 //your stuff
 //some button click launches Alertdialog
}

public void myCustomDialog(){
 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
 //other details for builder
      alertDialogBuilder.setNegativeButton("BACK",
            new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
                         dialod.dismiss;
                         myCustomBack();
                    }
      });

 AlertDialog alertDialog = alertDialogBuilder.create();
 alertDialog.setCanceledOnTouchOutside(true);
 alertDialog.show();
}

public void myCustomBack(){
  if(condition1){
    super.onBackPressed();
  }
  if(condition 2){
    //do  stuff here
  }
}

@Override
public void onBackPressed(){
  //handle case here
  if (condition A)
    //Do this
  else 
    //Do that
}

}
策马西风 2024-12-17 04:41:06

这是 C# 的实际解决方案,基于 Pooks 的答案:

首先,我们必须创建一个新类来处理事件:

private sealed class KeyListener : Java.Lang.Object, IDialogInterfaceOnKeyListener
{
    private readonly Action _action;

    public KeyListener(Action action)
    {
        _action = action;
    }

    public bool OnKey(IDialogInterface dialog, [GeneratedEnum] Android.Views.Keycode keyCode, KeyEvent e)
    {
        var isBack = keyCode == Android.Views.Keycode.Back
            && e.Action == KeyEventActions.Up
            && !e.IsCanceled;

        if (isBack)
            _action.Invoke();

        return isBack;
    }
}

我们需要一个操作来接收事件:

private void OnBackPressed()
{
    // ... do the stuff
}

现在,我们可以设置此事件:

var dlgDetails = new AlertDialog.Builder(this)
    .SetOnKeyListener(new KeyListener(OnBackPressed))
    // add further set ups
    .Create();

Here is an actual solution for C#, based on the answer from Pooks:

First, we have to create a new class for handling the event:

private sealed class KeyListener : Java.Lang.Object, IDialogInterfaceOnKeyListener
{
    private readonly Action _action;

    public KeyListener(Action action)
    {
        _action = action;
    }

    public bool OnKey(IDialogInterface dialog, [GeneratedEnum] Android.Views.Keycode keyCode, KeyEvent e)
    {
        var isBack = keyCode == Android.Views.Keycode.Back
            && e.Action == KeyEventActions.Up
            && !e.IsCanceled;

        if (isBack)
            _action.Invoke();

        return isBack;
    }
}

And we need an action to receive the event:

private void OnBackPressed()
{
    // ... do the stuff
}

Now, we can set up this event:

var dlgDetails = new AlertDialog.Builder(this)
    .SetOnKeyListener(new KeyListener(OnBackPressed))
    // add further set ups
    .Create();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文