0

我有一个自定义视图,我将其添加到相对布局中,并且仅将触摸侦听器添加到自定义视图中。问题是未调用 onTouchevent。请帮助我的实际代码中的示例代码::

ViewGroup base = (ViewGroup)findViewById(R.id.base); //relative layout
base.addView(move);
move.setOnTouchListener(touchListener);
OnTouchListener touchListener = new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            Log.d("onTouch v","="+v);
            if(v instanceof MovingView) {
                return false;
            }
            else 
                return true;
        }


    public boolean onTouchEvent(MotionEvent event) {
        int x = (int)event.getX();
            int y = (int)event.getY();


}
};// close listener
4

1 回答 1

0

首先,在初始化之前设置监听器(它为空)。尝试这个:

ViewGroup base = (ViewGroup)findViewById(R.id.base); //relative layout
base.addView(move);

OnTouchListener touchListener = new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            Log.d("onTouch v","="+v);
            if(v instanceof MovingView) {
                return false;
            }
            else 
                return true;
        }


    public boolean onTouchEvent(MotionEvent event) {
        int x = (int)event.getX();
            int y = (int)event.getY();


}
};// close listener

move.setOnTouchListener(touchListener);

其次,请注意:

“当触摸屏事件未被其下的任何视图处理时,将调用 onTouchEvent() 方法。”

于 2013-07-04T04:18:32.570 回答