Android:当用户在视图之外按下时如何关闭自定义视图

发布于 2024-11-02 14:20:04 字数 359 浏览 0 评论 0原文

我有一个自定义 View,它由一个按钮和一个在按钮下方动画的视图组成,其中包含多选项目的自定义视图。当用户按下按钮时,我会显示带有项目的“下拉菜单”。当他们按下下拉菜单之外时,我想隐藏“下拉菜单”。我尝试覆盖 onTouchEventonInterceptTouchEvent,但这些并不总是被调用。

我查看了 Spinner 的源代码,并注意到 Google 正在使用一个对话框作为我认为的下拉菜单(目前它的定位方式超出了我的范围)。

有什么办法可以让我的 View 拦截 Window 上的所有触摸事件吗?

I have a custom View, which consists of a button, and a view that animates in below the button that contains a custom view of multi-choice items. When the user presses the button, I show the "dropdown" with the items. I want to hide the "dropdown" when they press outside of the dropdown. I tried overriding the onTouchEvent and the onInterceptTouchEvent, but these aren't always called.

I took a look at the source for the Spinner, and noticed that Google is using an Dialog for what I believe to be its dropdown (how it is being positioned is beyond me at this point).

Is there any way I can have my View intercept ALL touch events on the Window?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半夏半凉 2024-11-09 14:20:04

您可以在顶部ViewGroup(例如,在LinearLayout 或您正在使用的任何内容上)实现onTouchListener

然后,确定自定义视图的位置: 如果触摸位置(使用 event.getX()event.getY() 方法)位于视图之外(使用 < code>myView.getTop() 等),然后它可以隐藏它(myView.setVisibility(View.GONE))。

无论如何,它应该返回 false< /code> 允许孩子们View来处理触摸

来自android开发指南:

请记住,关键事件始终会传递到当前处于焦点的视图。它们从视图层次结构的顶部开始分派,然后向下分派,直到到达适当的目的地。

注意:Android 将首先调用事件处理程序,然后调用类定义中相应的默认处理程序。因此,从这些事件侦听器返回 true 将停止将事件传播到其他事件侦听器,并且还会阻止对视图中默认事件处理程序的回调。因此,请确保您希望在返回 true 时终止事件。

You can implement the onTouchListener on the top ViewGroup (e.g., on a LinearLayout or whatever you're using).

Then, determine the position of your custom view: If the touch position (using event.getX() and event.getY() methods) is outside the View (using myView.getTop(), etc.), then it can hide it (myView.setVisibility(View.GONE).

In any case, it should return false to allow the children Views to handle the touch.

From the android dev guide:

Remember that key events are always delivered to the View currently in focus. They are dispatched starting from the top of the View hierarchy, and then down, until they reach the appropriate destination.

Note: Android will call event handlers first and then the appropriate default handlers from the class definition second. As such, returning true from these event listeners will stop the propagation of the event to other event listeners and will also block the callback to the default event handler in the View. So be certain that you want to terminate the event when you return true.

倾城花音 2024-11-09 14:20:04

我不知道它是如何工作的,但是 TouchDelegate乍一看,可能是一个解决方案。另外, Activity.onTouchEvent( ) 可能值得一试。

如果它们不适用(即您不知道如何使用 TouchDelegate 并且您的布局具有可以在“下拉”视图之外执行触摸的按钮),您可以考虑 拦截事件,同时它仍然“向上”朝向视图。

I have no idea about how it works, but a TouchDelegate could be a solution at first glance. Also, Activity.onTouchEvent() might be worth a check.

If they're not applicable (i.e. you don't know either how to use TouchDelegate and your layout has buttons that could get the touches performed outside the "dropdown" view), you could consider intercepting the event while it's still going "up" towards views.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文