未调用TouchListener onTouchEvent

时间:2013-07-04 04:03:08

标签: android

我有一个自定义视图,我将添加到相对布局并添加触摸侦听器 自定义视图。问题是onTouchevent没有调用.Plz帮助     来自我的实际代码::

的示例代码
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

1 个答案:

答案 0 :(得分:0)

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

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()方法   当触摸屏事件未被任何视图处理时   它“。