我想拦截我的父视图上的触摸事件onInterceptTouchEvent (MotionEvent ev)
.
从那里我想知道单击了哪个视图以执行其他操作,有什么方法可以知道从收到的运动事件中单击了哪个视图?
对于任何想知道我做了什么的人来说……我不能。我做了一个解决方法来知道我的特定视图组件是否被单击,所以我只能以此结束:
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;
}
}
然而,这只适用于布局中可以作为参数传递的已知视图,我仍然无法仅通过知道坐标来获取单击的视图。您可以搜索布局中的所有视图.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)