android如何处理onTouch与onClick事件冲突

 即在MotionEvent.ACTION_MOVE事件中加入取消点击操作代码。

public boolean onTouchEvent(MotionEvent ev) {

        switch (ev.getAction()) {

        case MotionEvent.ACTION_DOWN:

    //do some things

            break;

        case MotionEvent.ACTION_MOVE:

          

MotionEvent cancelEvent = MotionEvent.obtain(ev);

            cancelEvent.setAction(MotionEvent.ACTION_CANCEL |

                       (ev.getActionIndex()<< MotionEvent.ACTION_POINTER_INDEX_SHIFT));

            onTouchEvent(cancelEvent);

                                               break;

        case MotionEvent.ACTION_UP:

           //do some things

            break;

        }

        return super.onTouchEvent(ev);

    }

关键代码:

MotionEvent cancelEvent = MotionEvent.obtain(ev);

            cancelEvent.setAction(MotionEvent.ACTION_CANCEL |

                       (ev.getActionIndex()<< MotionEvent.ACTION_POINTER_INDEX_SHIFT));

            onTouchEvent(cancelEvent);

你可能感兴趣的:(android如何处理onTouch与onClick事件冲突)