如何在对话框外发生单击时触发事件

时间:2023-01-28 10:37:15

I would like to know how to solve a problem I've got.

我想知道如何解决我遇到的问题。

I have a Dialog which pops up in an activity. The Dialog doesn't cover the whole screen, so the buttons from the activity still show. I can easily close the dialog when there is a touch outside the dialog's bounds with dialog.setCanceledOnTouchOutside(true);

我有一个Dialog,它出现在一个活动中。对话框不会覆盖整个屏幕,因此活动中的按钮仍然显示。当在dialog.setCanceledOnTouchOutside(true)的对话框边界外触摸时,我可以轻松地关闭对话框;

However what I want to do is fire an event if a click is outside the Dialog's bounds (e.g if someone touches a button on the main Activity, it should close the Dialog and fire that event at the same time).

但是,我想要做的是,如果点击超出了Dialog的界限,则触发事件(例如,如果有人触摸主Activity上的按钮,它应该关闭Dialog并同时触发该事件)。

2 个解决方案

#1


34  

It Works For me,,

这个对我有用,,

        Window window = dialog.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        dialog.show();

See this http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_NOT_TOUCH_MODAL

请参阅http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_NOT_TOUCH_MODAL

#2


44  

When dialog.setCanceledOnTouchOutside(true); then you just override onCancel() like this:

当dialog.setCanceledOnTouchOutside(true);然后你就像这样重写onCancel():

dialog.setOnCancelListener(
        new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                //When you touch outside of dialog bounds, 
                //the dialog gets canceled and this method executes.
            }
        }
);

Type your code inside the onCancel() method so it runs when the dialog gets canceled.

在onCancel()方法中键入代码,以便在取消对话框时运行。

#1


34  

It Works For me,,

这个对我有用,,

        Window window = dialog.getWindow();
        window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        dialog.show();

See this http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_NOT_TOUCH_MODAL

请参阅http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_NOT_TOUCH_MODAL

#2


44  

When dialog.setCanceledOnTouchOutside(true); then you just override onCancel() like this:

当dialog.setCanceledOnTouchOutside(true);然后你就像这样重写onCancel():

dialog.setOnCancelListener(
        new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                //When you touch outside of dialog bounds, 
                //the dialog gets canceled and this method executes.
            }
        }
);

Type your code inside the onCancel() method so it runs when the dialog gets canceled.

在onCancel()方法中键入代码,以便在取消对话框时运行。