如何从android中的事件坐标获取视图?

发布于 2024-10-02 11:58:27 字数 133 浏览 3 评论 0原文

我想使用 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 技术交流群。

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

发布评论

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

评论(3

失退 2024-10-09 11:58:27

对于任何想知道我做了什么的人来说……我不能。我做了一个解决方法来知道我的特定视图组件是否被单击,所以我只能以这个结束:

   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;
    }
}

但是,这仅适用于布局中可以作为参数传递的已知视图,我仍然无法得到只需知道坐标即可单击视图。 您可以搜索布局中的所有视图

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()
   }

and the method:

/**
 * 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.

吻风 2024-10-09 11:58:27

只是为了使 htafoya 的方法更简单:

/**
* 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
*/
private 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
    return ((x > viewX && x < (viewX + view.getWidth())) &&
            (y > viewY && y < (viewY + view.getHeight())));
}

Just to make method of htafoya simpler:

/**
* 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
*/
private 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
    return ((x > viewX && x < (viewX + view.getWidth())) &&
            (y > viewY && y < (viewY + view.getHeight())));
}
一袭水袖舞倾城 2024-10-09 11:58:27

获取触摸视图的一个简单方法是为各个视图设置 OnTouchListener 并将视图存储在活动的类变量中。
返回 false 将使输入事件可用于活动的 onTouchEvent() 方法,您可以在其中轻松处理所有触摸事件(也是父视图的触摸事件)。

myView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
    touchedView = myView;
    return false;
    }
}); 


@Override
public boolean onTouchEvent(MotionEvent event) {


    switch (event.getAction()) {

        case MotionEvent.ACTION_UP:

            if(touchedView!=null) {
                doStuffWithMyView(touchedView);
            ....
            ....

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).

myView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
    touchedView = myView;
    return false;
    }
}); 


@Override
public boolean onTouchEvent(MotionEvent event) {


    switch (event.getAction()) {

        case MotionEvent.ACTION_UP:

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