【问题标题】:Android : How to Allow Only One Touch?Android:如何只允许一键触摸?
【发布时间】:2013-05-22 03:56:33
【问题描述】:

如何在 Android 上只允许一键触摸?我不想支持多点触控,如果我的一根手指已经在触摸屏幕,我希望我的应用放弃其他触控。就像在 iOS 上使用独家触控一样。

另外,有没有办法设置允许的触摸次数?

谢谢!

编辑: 最小目标 API = 8。

【问题讨论】:

    标签: android touch multi-touch


    【解决方案1】:

    是的,您可以使用 android:splitMotionEvents="false"android:windowEnableSplitTouch="false" 禁用多点触控

    【讨论】:

    • 不确定这是否有效。但它需要 API 级别 11 及以上。
    • 我的目标最低版本是 API 10。
    【解决方案2】:

    您必须跟踪第一次触摸的指针 id 并仅使用与该触摸相对应的触摸事件。这是来自The official Android blog "Making Sense of Multitouch "的好例子

    private static final int INVALID_POINTER_ID = -1;
    
    // The ‘active pointer’ is the one currently moving our object.
    private int mActivePointerId = INVALID_POINTER_ID;
    
    // Existing code ...
    
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN: {
            final float x = ev.getX();
            final float y = ev.getY();
    
            mLastTouchX = x;
            mLastTouchY = y;
    
            // Save the ID of this pointer
            mActivePointerId = ev.getPointerId(0);
            break;
        }
    
        case MotionEvent.ACTION_MOVE: {
            // Find the index of the active pointer and fetch its position
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float y = ev.getY(pointerIndex);
    
            final float dx = x - mLastTouchX;
            final float dy = y - mLastTouchY;
    
            mPosX += dx;
            mPosY += dy;
    
            mLastTouchX = x;
            mLastTouchY = y;
    
            invalidate();
            break;
        }
    
        case MotionEvent.ACTION_UP: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }
    
        case MotionEvent.ACTION_CANCEL: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }
    
        case MotionEvent.ACTION_POINTER_UP: {
            // Extract the index of the pointer that left the touch sensor
            final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) 
                    >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
            final int pointerId = ev.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mLastTouchX = ev.getX(newPointerIndex);
                mLastTouchY = ev.getY(newPointerIndex);
                mActivePointerId = ev.getPointerId(newPointerIndex);
            }
            break;
        }
        }
    
        return true;
    }
    

    【讨论】:

    • 我试过这个,我将它添加到我的活动中,但它无法识别活动内片段的触摸。我该怎么做?谢谢!
    • 我试图通过使用我在 ontouchlistener 中设置的对象的 id 来做到这一点,但这让我陷入了麻烦。这是完美的。
    • 所以,在IOS中,你说“view.isExclusiveTouch = true;” ...而在Android中,您必须使用上面的代码吗?我拒绝相信这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-01
    • 1970-01-01
    相关资源
    最近更新 更多