Android MapView停止生成(或接收?)事件

时间:2010-10-02 01:24:33

标签: android events

我正在开发一个扩展MapActivity的Android类。我已经设置了OnTouchListener,并且第一个MotionEvent的一切正常。在第一个MotionEvent之后,系统停止生成MotionEvents或接收它们或两者。有人知道我要做什么来继续接收MotionEvents吗?感谢。

@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    mapView.setOnTouchListener(new View.OnTouchListener( ) {    
        @Override public boolean onTouch(View v, MotionEvent event) {
            Log.i(TAG, "onTouch called");

            switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, "ACTION_DOWN: x = " + event.getX() + ", y = " + event.getY());
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, "ACTION_UP: x = " + event.getX() + ", y = " + event.getY());
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.i(TAG, "ACTION_CANCEL: x = " + event.getX() + ", y = " + event.getY());
                break;
            case MotionEvent.ACTION_OUTSIDE:
                Log.i(TAG, "ACTION_OUTSIDE: x = " + event.getX() + ", y = " + event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i(TAG, "ACTION_MOVE: x = " + event.getX() + ", y = " + event.getY());
                break;
            }

            v.onTouchEvent(event);
            return true;   // I also tried return false here, that didn't fix anything
        }
    });

1 个答案:

答案 0 :(得分:2)

您不应该将事件处理放在onCreate方法中,因为onCreate方法在启动时是executet。要为地图启用触摸或标签功能,您应使用Map OverlayUsing Google Maps in Android

在MapActivity中写下MapOverlay类:

class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
           //...
        }

        @Override
        public boolean onTouchEvent(MotionEvent event, MapView mapView) 
        {   
            //---when user lifts his finger---

            return false;
        }        
    }

...然后在onCreate方法中将叠加层添加到MapActivity:

MapOverlay mapOverlay = new MapOverlay();
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.clear();
listOfOverlays.add(mapOverlay);        
mapView.invalidate();