Android:在RelativeLayout的onTouchEvent中看不到ACTION_MOVE / UP

时间:2011-05-09 06:39:45

标签: android event-handling

我向RelativeLayout注册了一个监听器,见下文。我想添加一些自定义事件处理,

mOnTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            final int action = motionEvent.getAction();
            boolean ret = false;

            switch (action) {
            case MotionEvent.ACTION_DOWN:
                ret = doSth(motionEvent);
                break;
            case MotionEvent.ACTION_MOVE:
                ret = doSth(motionEvent);
                break;
            case MotionEvent.ACTION_UP:
                ret = doSth(motionEvent);
                break;
            }

            return ret;     // returning false got me none of MOVE/UP events right here
        }
    };

但是,除非返回true,否则我无法获得任何MOVE / UP事件。

另一次尝试,我向CheckBox注册了同样的监听器,一切都很顺利 ViewGroup和Widget之间有区别吗?设计目的?

1 个答案:

答案 0 :(得分:24)

“但是,除非返回true,否则我无法获得任何MOVE / UP事件。”

你已经回答了自己的问题。如果您没有返回true表示您关心并正在处理该事件,系统将不会向您发送后续事件,因为您告诉系统您不关心它。如果您需要监控触摸事件的整个生命周期,则需要始终返回true