简体   繁体   English

如何在 Dialog 上触发事件关闭 Android?

[英]How to trigger an event on Dialog dismiss Android?

I have a custom dialog which extends the Dialog Class, I would like to bind an event to execute some code after the Dialog is closed when the user presses the BACK button of the device.我有一个扩展 Dialog 类的自定义对话框,我想绑定一个事件以在用户按下设备的 BACK 按钮时关闭对话框后执行一些代码。 How can I do that?我怎样才能做到这一点? I found a post where someone says that the .addWindowListener() should be used when working with Dialogs and other Window widgets.我发现了一个帖子,有人说在使用 Dialogs 和其他 Window 小部件时应该使用 .addWindowListener() 。 But the dialog class doesn't have the addWindowListener method, so I cannot use it.但是对话框类没有 addWindowListener 方法,所以我不能使用它。 Is there another way without using fragments cause I shouldn't re-write the MyCustomDialog class?有没有另一种不使用片段的方法,因为我不应该重写 MyCustomDialog 类?

This is the code:这是代码:

public class MyCustomDialog extends Dialog {

public MyCustomDialog(Context context, int layoutResourceId) {
    super(context);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(layoutResourceId);

}
}  

Thanks for the attention!感谢关注!

EDIT: i found this on the android developers site, is there a way to use it with MyCustomDialog class?编辑:我在 android 开发人员网站上找到了这个,有没有办法将它与 MyCustomDialog 类一起使用?

onDismiss DialogInterface 关闭对话框界面

Since you are extending android Dialog class you can implement a Dismiss Listener in your Activity's and set it when you create the Dialog, and then in the listener implement any functionality you want depending on the button that was used to dismiss the Dialog.由于您正在扩展 android Dialog 类,因此您可以在 Activity 中实现一个Dismiss Listener并在创建 Dialog 时设置它,然后在侦听器中根据用于关闭 Dialog 的按钮实现您想要的任何功能。

Hope this will solve your problem.希望这能解决您的问题。

Edit You can use dialog.setCanceledOnTouchOutside(false);编辑您可以使用dialog.setCanceledOnTouchOutside(false); which will stop closing the dialog if you touch outside of the dialog.如果您触摸对话框外部,它将停止关闭对话框。

Something like,就像是,

  Dialog dialog = new Dialog(context)
  dialog.setCanceledOnTouchOutside(false);

OR Alternatively或者 或者

Override onTouchEvent() of dialog and check for action type.覆盖对话框的onTouchEvent()并检查操作类型。 if the action type is ' MotionEvent.ACTION_OUTSIDE ' means, user is interacting outside the dialog region.如果操作类型为“ MotionEvent.ACTION_OUTSIDE ”,则表示用户在对话区域外进行交互。 So in this case, you can dimiss your dialog or decide what you wanted to perform.因此,在这种情况下,您可以关闭对话或决定要执行的操作。 view plainprint?查看普通版?

dialog.setOnTouchListener(new View.OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event)  
    {  

       if(event.getAction() == MotionEvent.ACTION_OUTSIDE){  
            Toast.make(getApplicationContext(), "TOuched outside the dialog", Toast.LENGTH_LONG).show();  
               this.dismiss();  
       }  
       return false;  
    }
});  

And for back press you can do dialog.setCancelable(false);对于后按,您可以执行dialog.setCancelable(false); which will prevent dialog getting cancelled from backpress event.这将防止对话框被 backpress 事件取消。

OR you can alternatively override setOnKeyListener event and put your own code into it.或者,您也可以覆盖setOnKeyListener事件并将您自己的代码放入其中。

Edit编辑

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

Happy Coding!!!编码快乐!!!

You need to Override onBackPressed inside Dialog class.您需要在 Dialog 类中覆盖onBackPressed Also make sure to close dialog after override OnBackPressed .还要确保在覆盖 OnBackPressed 后关闭对话框。

Try this尝试这个

public class MyCustomDialog extends Dialog {

public MyCustomDialog(Context context, int layoutResourceId) {
    super(context);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(layoutResourceId);

}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

     dismiss();   // make sure to call dismiss to close dialog

    // put your code here
}
}

If you want to trigger an event if user clicked/touched outside the dialog and close it or used the back button to close then use如果您想在用户单击/触摸对话框外并关闭它或使用后退按钮关闭时触发事件,则使用

dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                //your trigger goes here
                Toast.makeText(IntroductoryActivity.this, "on cancel", Toast.LENGTH_SHORT).show();
            }
        });

But if you want to trigger an event if something dismisses the dialog, like some other event then use但是如果你想在某些东西关闭对话框时触发一个事件,就像其他一些事件一样,然后使用

dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                //your trigger goes here
                Toast.makeText(IntroductoryActivity.this, "on dismiss", Toast.LENGTH_SHORT).show();
            }
        }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM