如何通过单击对话框外部来关闭对话框?
我已经为我的应用程序实现了一个自定义对话框。我想实现当用户在对话框外部单击时,对话框将被关闭。 为此我需要做什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
从您的活动/片段中调用
dialog.setCancelable(false);
。Call
dialog.setCancelable(false);
from your activity/fragment.您可以使
背景
占据所有屏幕尺寸透明
并监听onClick
事件以关闭
它。You can make a
background
occupying all the screen sizetransparent
and listen to theonClick
event todismiss
it.我尝试了一些答案,但仍然遇到一个问题,例如当我按对话框外部时,对话框隐藏,但显示暗淡的视图,再次按将转到父活动。但实际上我想在第一次点击后进入父活动。
所以我所做的
是改变我的活动来实现
DialogInterface.OnCancelListener
并繁荣,它起作用了。
I tried some answers still I faced a problem like when I press outside the dialog dialog was hiding but a dimmed view was showing, and pressing again would go to the parent activity. But actually I wanted to go the parent activity after first click.
So what I did was
and changed my activity to implement
DialogInterface.OnCancelListener
withAnd boom, it worked.
这是代码
试试这个。当你触摸外面时,你可以隐藏键盘
Here is the code
Try this one . you can hide the keyboard when you touch outside
您可以使用dialog.setCanceledOnTouchOutside(true);,如果您触摸对话框外部,它将关闭对话框。
例如,
或者如果您的对话框处于非模型状态,则
1 - 为对话框的窗口属性设置标志 -
FLAG_NOT_TOUCH_MODAL
2 - 在 Windows 属性中添加另一个标志,
FLAG_WATCH_OUTSIDE_TOUCH
- 这个用于对话框接收其可见区域之外的触摸事件。3 - 覆盖对话框的
onTouchEvent()
并检查操作类型。如果动作类型是'
MotionEvent.ACTION_OUTSIDE
' 表示用户正在对话框区域之外进行交互。因此,在这种情况下,您可以关闭对话框或决定要执行的操作。查看明文?
有关详细信息,请参阅如何取消基于触摸点的自定义对话框? 和
如何关闭非模式对话框,当触摸到对话框区域之外时
You can use
dialog.setCanceledOnTouchOutside(true);
which will close the dialog if you touch outside of the dialog.Something like,
Or if your Dialog in non-model then,
1 - Set the flag-
FLAG_NOT_TOUCH_MODAL
for your dialog's window attribute2 - Add another flag to windows properties,,
FLAG_WATCH_OUTSIDE_TOUCH
- this one is for dialog to receive touch event outside its visible region.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?
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
只需使用
Simply use
您可以使用 onTouchEvent 的此实现。它可以防止底层活动对触摸事件做出反应(如howettl所述)。
资料来源:http://blog.twimager.com/2010/08/leading- Activity-by-touching-outside.html
You can use this implementation of onTouchEvent. It prevent from reacting underneath activity to the touch event (as mentioned howettl).
Source: http://blog.twimager.com/2010/08/closing-activity-by-touching-outside.html
或者,如果您使用样式 xml 中定义的主题自定义对话框,请将此行放入您的主题中:
Or, if you're customizing the dialog using a theme defined in your style xml, put this line in your theme:
此方法应完全避免灰色区域以下检索点击事件的活动。
删除此行(如果有):
将其放在您创建的活动中
,然后用此覆盖触摸事件
This method should completely avoid activities below the grey area retrieving click events.
Remove this line if you have it:
Put this on your activity created
then override the touch event with this
触摸外部时关闭对话框。
如果您不想关闭外部触摸,请使用以下代码:
to close dialog on touch outside.
And if you don't want to close on touch outside, use the code below:
你可以尝试这个:-
或者
如果你有
AlterDialog.Builder
那么你可以尝试这个:-You can try this :-
or
And if you have a
AlterDialog.Builder
Then you can try this:-此代码用于当使用单击对话框时隐藏软输入以及当用户单击对话框的外侧时软输入和对话框都关闭的情况。
This code is use for when use click on dialogbox that time hidesoftinput and when user click outer side of dialogbox that time both softinput and dialogbox are close.
以下对我有用:
Following has worked for me:
另一种解决方案,此代码取自
Window
的android源代码您应该将这两个方法添加到对话框源代码中。
这个解决方案没有这个问题:
Another solution, this code was taken from android source code of
Window
You should just add these Two methods to your dialog source code.
This solution doesnt have this problem :