Android 按下后退按钮或触摸外部时,不会为AlertDialog调用setOnCancelListener和setOnDismissListener

Android 按下后退按钮或触摸外部时,不会为AlertDialog调用setOnCancelListener和setOnDismissListener,android,Android,什么时候 在对话框区域外触摸 按下后退按钮 我希望调用onDismiss(或onCancel)。但是,这两个名称都不是。我能知道我遗漏了什么吗?从中,我想当我按下后退按钮时会调用onCancel。但这对我不起作用。我可以知道我错过了什么吗 public class RateAppDialogFragment extends SherlockDialogFragment { public static RateAppDialogFragment newInstance() {

什么时候

  • 在对话框区域外触摸
  • 按下后退按钮
我希望调用
onDismiss
(或
onCancel
)。但是,这两个名称都不是。我能知道我遗漏了什么吗?从中,我想当我按下后退按钮时会调用
onCancel
。但这对我不起作用。我可以知道我错过了什么吗

public class RateAppDialogFragment extends SherlockDialogFragment {
    public static RateAppDialogFragment newInstance() {
        RateAppDialogFragment rateAppDialogFragment = new RateAppDialogFragment();
        return rateAppDialogFragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.rate_app_dialog_fragment, null);

        Utils.setCustomTypeFace(view, Utils.ROBOTO_LIGHT_TYPE_FACE);

        final AlertDialog dialog = new AlertDialog.Builder(this.getSherlockActivity())            
        .setTitle("Love JStock?")
        .setView(view)
        // Add action buttons
        .setPositiveButton("Rate 5 stars \u2605", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                Utils.showShortToast("Rate");
            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Utils.showShortToast("No");
            }
        })
        .setNeutralButton("Later", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Utils.showShortToast("Later");
            }
        })
        .create();

        dialog.setCanceledOnTouchOutside(true);

        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                Utils.showShortToast("Back button pressed?");
            }
        });

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {
                // TODO Auto-generated method stub
                Utils.showShortToast("Back button pressed?");
            }
        });

        return dialog;
    }

}


您没有设置
反按
键事件

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();
        }
        return true;
    }
});

您应该阅读以下主题:


使用
DialogFragment
显示
Dialog

根据,解决方案是在
DialogFragment

请你也注意一下

注意:DialogFragment拥有Dialog.setOnCancelListener和 Dialog.setOnDismissListener回调。你不能自己设置它们。 要了解这些事件,请重写onCancel(DialogInterface)和 onDismiss(DialogInterface)


在对话框片段中,重写

@Override
public void onStop() {
    super.onStop();
    if (mListener != null) {
       // do something here
    }
}

如果使用的是
警报对话框
,请参阅

builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        // dialog dismiss without button press
    }
});

dialog.setCanceledOnTouchOutside(true)
(谢谢@LeosLiterak)

@pskink您在代码上测试运行了吗?对你来说真的很好吗?我用Log.d。没什么不同。toast和logging消息都不会显示。我使用最小的setup-Builder运行它,只使用正负按钮,并用2.2进行了测试avd@pskink如果您使用的是DialogFragment.ok,现在就可以看到这个问题,但是onCreateDialog的文档解释了一切,但是,触摸对话框区域之外的内容如何?另外,您知道为什么setOnCancelListener和setOnDismissListener不工作吗?它们应该会起作用,不是吗?因为你找到了解决办法。我会说那是一个又大又愚蠢的错误D如果您要覆盖默认方法,则必须在其上使用鼠标右键
@override
。这是一个很好的解决方案!实际上,这是在DialogFragment中使用Cancel的方法。如果您离开
super.onCancel(dialog)
,它会起作用。。我能够立即触发我的操作,并且
cancel
按钮最终不会调用这个good@CheokYan Cheng我有一个类似的用例,并根据您上面的建议设置了onCancel()。有时需要在对话框外多次单击(我在DilaogfFragment中使用DatePickerDialog),对话框才能最终消失。关于如何捕获对话框外部的第一次触摸,以便我可以立即关闭对话框?并添加dialog.setCanceledOnTouchOutside(true)以及java.lang.IllegalStateException:您不能设置dialog的OnCancelListener或OnDismissListener。不要与builder一起工作(至少对我而言),在API 21上测试。@Joe这是因为您将它与
对话框片段一起使用(请参阅)。使用AlertDialog,它正在工作。
// This is DialogFragment, not Dialog
@Override
public void onCancel(DialogInterface dialog) {
}
@Override
public void onStop() {
    super.onStop();
    if (mListener != null) {
       // do something here
    }
}
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        // dialog dismiss without button press
    }
});