如何通过在对话框外单击来关闭对话框?

编程入门 行业动态 更新时间:2024-10-27 00:25:37
本文介绍了如何通过在对话框外单击来关闭对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经为我的应用程序实现了一个自定义对话框.我想实现当用户在对话框外单击时,对话框将被关闭.我该怎么做?

I have implemented a custom dialog for my application. I want to implement that when the user clicks outside the dialog, the dialog will be dismissed. What do I have to do for this?

推荐答案

您可以使用 dialog.setCanceledOnTouchOutside(true); 如果您触摸对话框外部,它将关闭对话框.

You can use dialog.setCanceledOnTouchOutside(true); which will close the dialog if you touch outside of the dialog.

类似的东西,

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

或者如果你的 Dialog 在非模型中,

Or if your Dialog in non-model then,

1 - 为对话框的窗口属性设置标志-FLAG_NOT_TOUCH_MODAL

1 - Set the flag-FLAG_NOT_TOUCH_MODAL for your dialog's window attribute

Window window = this.getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

2 - 向 windows 属性添加另一个标志,FLAG_WATCH_OUTSIDE_TOUCH - 这个标志用于对话框在其可见区域之外接收触摸事件.

2 - Add another flag to windows properties,, FLAG_WATCH_OUTSIDE_TOUCH - this one is for dialog to receive touch event outside its visible region.

3 - 覆盖对话框的 onTouchEvent() 并检查操作类型.如果操作类型是'MotionEvent.ACTION_OUTSIDE' 表示用户在对话区域外进行交互.因此,在这种情况下,您可以关闭对话或决定要执行的操作.查看普通版?

3 - Override onTouchEvent() of dialog and check for action type. if the action type is 'MotionEvent.ACTION_OUTSIDE' means, user is interacting outside the dialog region. So in this case, you can dimiss your dialog or decide what you wanted to perform. view plainprint?

public boolean onTouchEvent(MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_OUTSIDE){ System.out.println("TOuch outside the dialog ******************** "); this.dismiss(); } return false; }

有关详细信息,请查看 How关闭基于接触点的自定义对话框? 和如何关闭非模态对话框,在对话区域外触摸时

For more info look at How to dismiss a custom dialog based on touch points? and How to dismiss your non-modal dialog, when touched outside dialog region

更多推荐

如何通过在对话框外单击来关闭对话框?

本文发布于:2023-11-17 06:25:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609145.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对话框   单击

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!