0

我的活动中有一个按钮网格。当用户在这个按钮网格上滑动手指时,我希望基本上记录所有触摸的按钮,以便我知道触摸了哪些按钮。

我一直在对此进行研究,但我不知道如何做到这一点,因为大多数示例都是使用位图。它选择的第一个视图是唯一从我的OnTouchListener. 从我的读数来看,MotionEvent.ACTION_MOVE这是什么用途,我看到有人说要使用view.getRawX()view.getRawY()但我不明白如何使用它来确定用户滑动手指时按下了哪些其他按钮,哪些没有按下屏幕上。

我是 Android 新手,所以如果这比我想象的要简单得多,我深表歉意。任何输入将不胜感激,因为我认为这不应该如此复杂。感谢您的时间!:)

4

2 回答 2

2

Once your View returns true to say that it's consuming the touch event, the rest of the views won't receive it. What you can do is make a custom ViewGroup (you say you have a grid, I'll just assume GridView?) that intercepts and handles all touch events:

public class InterceptingGridView extends GridView {
    private Rect mHitRect = new Rect();

    public InterceptingGridView (Context context) {
        super(context);
    }

    public InterceptingGridView (Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent (MotionEvent ev) {
        //Always let the ViewGroup handle the event
        return true;
    }

    @Override
    public boolean onTouchEvent (MotionEvent ev) {
        int x = Math.round(ev.getX());
        int y = Math.round(ev.getY());

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.getHitRect(mHitRect);

            if (mHitRect.contains(x, y)) {
                /*
                 * Dispatch the event to the containing child. Note that using this
                 * method, children are not guaranteed to receive ACTION_UP, ACTION_CANCEL,
                 * or ACTION_DOWN events and should handle the case where only an ACTION_MOVE is received.
                 */
                child.dispatchTouchEvent(ev);
            }
        }

        //Make sure to still call through to the superclass, so that
        //the ViewGroup still functions normally (e.g. scrolling)
        return super.onTouchEvent(ev);
    }
}

How you choose to handle the event depends on the logic that you require, but the takeaway is to let the container view consume all of the touch events, and let it handle dispatching the events to the children.

于 2013-05-01T03:44:05.933 回答
0

也许这会帮助你:

    @Override
public boolean onTouch(View v, MotionEvent event) {

        int action = event.getAction() & MotionEvent.ACTION_MASK;
        int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
        int pointerId = event.getPointerId(pointerIndex);


        switch (action) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_POINTER_DOWN:

            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
        case MotionEvent.ACTION_CANCEL:

            break;

        case MotionEvent.ACTION_MOVE:
            int pointerCount = event.getPointerCount();
            for (int i = 0; i < pointerCount; i++) {

            }
            break;
        }

        return true;

}

它适用于多点触控

于 2013-05-01T03:36:54.483 回答