问题描述
我想使用 onInterceptTouchEvent(MotionEvent ev)
拦截父视图中的触摸事件。
I want to intercept the touch events on my parent view with onInterceptTouchEvent (MotionEvent ev)
.
从那里我想知道为了做其他的事情而点击了哪个视图,有没有办法知道从那个收到的动议事件中点击了哪个视图? p>
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?
推荐答案
对于任何想要知道我做了什么的人,我不能。我做了一个解决方法,只是知道我的具体视图组件是否被点击,所以我只能这样结束:
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:
if(isPointInsideView(ev.getRawX(), ev.getRawY(), myViewComponent)){
doSomething()
}
和方法:
/**
* Determines if given points are inside view
* @param x - x coordinate of point
* @param y - y coordinate of point
* @param view - view object to compare
* @return true if the points are within view bounds, false otherwise
*/
public static boolean isPointInsideView(float x, float y, View view){
int location[] = new int[2];
view.getLocationOnScreen(location);
int viewX = location[0];
int viewY = location[1];
//point is inside view bounds
if(( x > viewX && x < (viewX + view.getWidth())) &&
( y > viewY && y < (viewY + view.getHeight()))){
return true;
} else {
return false;
}
}
但是,这只适用于布局中的已知视图您可以作为参数传递,我仍然无法通过了解坐标得到点击的视图。您可以搜索布局中的所有视图。
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.
这篇关于如何从Android中的事件坐标获取视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!