如何从android中的事件坐标获取视图?
我想使用 onInterceptTouchEvent (MotionEvent ev) 拦截父视图上的触摸事件
。
从那里我想知道单击了哪个视图以执行其他操作,有什么方法可以知道从收到的运动事件中单击了哪个视图?
I want to intercept the touch events on my parent view with onInterceptTouchEvent (MotionEvent ev)
.
From there I want to know which view was clicked in order to do other things, is there any way to know which view was clicked from that motion event received?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于任何想知道我做了什么的人来说……我不能。我做了一个解决方法来知道我的特定视图组件是否被单击,所以我只能以这个结束:
和方法:
但是,这仅适用于布局中可以作为参数传递的已知视图,我仍然无法得到只需知道坐标即可单击视图。 您可以搜索布局中的所有视图。
Well for anyone who wants to know what I did ... i couldn't. I did a workaround to just know if my specific view component was clicked, so I could only end with this:
and the method:
However this only works for known views in the layout that you can pass as parameter, I still can't get the clicked view just by knowing the coordinates. You may search for all views in the layout though.
只是为了使 htafoya 的方法更简单:
Just to make method of htafoya simpler:
获取触摸视图的一个简单方法是为各个视图设置 OnTouchListener 并将视图存储在活动的类变量中。
返回 false 将使输入事件可用于活动的 onTouchEvent() 方法,您可以在其中轻松处理所有触摸事件(也是父视图的触摸事件)。
A simple way for getting the touched view is to set an OnTouchListener to the individual views and store the view in a class variable of the activity.
Returning false will make the input event available to the method onTouchEvent() of the activity, where you can easily handle all the touch events (also the ones of your parent view).