Java Android onDismissListener调用了Cancel

Java Android onDismissListener调用了Cancel,java,android,android-alertdialog,event-listener,Java,Android,Android Alertdialog,Event Listener,我在我的AlertDialog中添加了一个AlertDialog.OnDismissListener。调用dialog.cancel()时,将调用onCancelListener以及onDismissListener 这是预期的行为吗?当调用对话框时,是否可以以某种方式防止调用onDismissListener。取消(),而不是调用对话框。解除() 有一些代码: AlertDialog.Builder builder = new AlertDialog.Builder(activity)

我在我的AlertDialog中添加了一个
AlertDialog.OnDismissListener
。调用
dialog.cancel()
时,将调用
onCancelListener
以及
onDismissListener

这是预期的行为吗?当调用
对话框时,是否可以以某种方式防止调用
onDismissListener
。取消()
,而不是调用
对话框。解除()

有一些代码:

AlertDialog.Builder builder = new AlertDialog.Builder(activity)
                .setView(view)
                .setTitle(title)
                .setIcon(icon)
                .setCancelable(true)
                .setNegativeButton(R.string.cancel, (d, i) -> {
                    d.cancel();
                    Log.d(TAG, "Cancel pressed!");
                })
                .setPositiveButton(positiveBtnText, (d, i) -> {
                    d.dismiss();
                    Log.d(TAG, "Dismiss pressed!");
                });
        AlertDialog dialog = builder.create();
        dialog.setOnCancelListener(dialogInterface -> {
            Log.d(TAG, "Dialog canceled!");
        });
        dialog.setOnDismissListener(dialogInterface -> {
            Log.d(TAG, "Dialog dismissed!");
        });
意外行为也会出现在日志中:

03-25 05:15:31.895 25985-25985/io.l.l D/io.l.l.u.ArrayAdapter: Cancel pressed!
03-25 05:15:31.895 25985-25985/io.l.l D/io.l.l.u.ArrayAdapter: Operation canceled!
03-25 05:15:31.896 25985-25985/io.l.l D/io.l.l.u.ArrayAdapter: Dismiss called!

您不需要在setPositiveButton/setNegativeButton callback中调用Dismise或cancel,因此您可以直接在那里实现您的逻辑。

没有理由同时使用
setNegativeButton
/
setPositiveButton
和设置
OnCancelListener
OnDismissListener

删除:

dialog.setOnCancelListener(dialogInterface -> {
            Log.d(TAG, "Dialog canceled!");
        });
        dialog.setOnDismissListener(dialogInterface -> {
            Log.d(TAG, "Dialog dismissed!");
        });
并在您提供给
setPositiveButton
的侦听器中添加当用户按OK时要运行的代码:

// ...
.setPositiveButton(positiveBtnText, (d, i) -> {
                    // Your code that reacts to the user pressing "OK" goes here!
                    Log.d(TAG, "OK pressed!");
                });
.setNegativeButton(R.string.cancel, (d, i) -> {
                    // Your code that reacts to the user pressing "Cancel" goes here!
                    Log.d(TAG, "Cancel pressed!");
                })
类似地,在您提供给
setNegativeButton
的侦听器中添加当用户按下Cancel时要运行的代码:

// ...
.setPositiveButton(positiveBtnText, (d, i) -> {
                    // Your code that reacts to the user pressing "OK" goes here!
                    Log.d(TAG, "OK pressed!");
                });
.setNegativeButton(R.string.cancel, (d, i) -> {
                    // Your code that reacts to the user pressing "Cancel" goes here!
                    Log.d(TAG, "Cancel pressed!");
                })

不要使用
OnCancelListener
OnDismissListener
作为区分消极和积极按钮点击的方法


您可以在各自的侦听器中为每个按钮实现您的逻辑,而无需调用
d.disease()
d.cancel()

您不需要调用
d.disease
,当您点击其中一个按钮时,对话框将自动取消。你想在OnCancel和OnDismiss听众中做什么?谢谢。我想做的事情取决于按下哪个按钮。为什么这很重要?我补充了一个答案来解释这一点。不需要使用
setOnCancelListener
setOnDismissListener
这很难看,但我想我会坚持使用它。谢谢:)@K40S不客气:)。为什么它很丑?如果在匿名类中包含大量代码会让您感到不安,那么您总是可以将代码移动到周围活动的一个方法中,并从侦听器实现中调用该方法:)。我感到不安的是,我需要在一段代码中调用
dialog.findViewById()
,对话框未初始化的地方。@K40S lambda表达式中的
d
参数是收到单击的对话框。如果你认为它的风格更好,你可以用它来代替。是的,但它是一个
对话框界面
,而且类型转换更难看。我通过创建一个带有
句柄(AlertDialog d)
方法的接口解决了这个问题,我可以将它用作Lambda。尽管如此,这还是比监听器正常工作时的代码多得多。