onTouchEvent 从未调用过 MapActivity

发布于 2024-11-02 10:35:20 字数 746 浏览 2 评论 0原文

我有一个扩展 MapActivity 的活动。但是当我点击地图时,onTouchEvent 永远不会被调用。这是为什么呢?

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d("temp", "onTouchEvent");
    return true;
}

编辑:我现在在自定义创建的 ItemizedOverlay 中有这两种方法来捕获我的事件。 当我点击叠加层时,第一个被调用。但是当我触摸地图时,第二个(onTouchEvent)永远不会被调用。

@Override
    protected boolean onTap(int index) {
      OverlayItem item = mOverlays.get(index);
      this.movement = true;
      Log.d("temp", "overlayItem tapped" + item.getTitle());
      return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) {
        this.movement = false;
        Log.d("temp", "overlayItem tapped finish");
        return true;
    }

I have an activity which extends MapActivity. But when I tap the map, the onTouchEvent never gets called. Why is this?

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d("temp", "onTouchEvent");
    return true;
}

edit: I now have these 2 methods in a custum created ItemizedOverlay to catch my events.
The first one gets called when I tap an overlay. But the second (onTouchEvent) never get's called when I touch the map.

@Override
    protected boolean onTap(int index) {
      OverlayItem item = mOverlays.get(index);
      this.movement = true;
      Log.d("temp", "overlayItem tapped" + item.getTitle());
      return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event, MapView mapView) {
        this.movement = false;
        Log.d("temp", "overlayItem tapped finish");
        return true;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢烬 2024-11-09 10:35:20

MapActivity 关联的 MapView 几乎肯定会为您处理视图。如果您查看 Activity#onTouchEvent() 的文档,它会显示:“当触摸屏事件未被其下的任何视图处理时调用。”

http://developer.android.com/参考/android/app/Activity.html#onTouchEvent(android.view.MotionEvent)

我猜你想用这个触摸事件做什么已经由MapView处理了,也许找到合适的地方把它放进去那个代码?

The MapView associated with a MapActivity almost certainly handles the view for you. If you look at the docs for Activity#onTouchEvent() it says: "Called when a touch screen event was not handled by any of the views under it."

http://developer.android.com/reference/android/app/Activity.html#onTouchEvent(android.view.MotionEvent)

I'm guessing what you want to do with that touch event is already handled by the MapView, maybe find the right place to put it inside of that code?

远昼 2024-11-09 10:35:20

您应该覆盖 dispatchTouchEvent

You should override dispatchTouchEvent

谜兔 2024-11-09 10:35:20

处理程序没有被执行。我还没有找到这种行为的确切原因。原因可能是 MapActivity 不会自动将事件转发到注册的 MapView,或者它只是没有收到有关该事件的通知,因为运动事件实际上发生在 MapView 本身而不是其父级 MapActivity 上。

可以做的就是直接在 MapView 上注册事件
mapView.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
  // TODO Auto-generated method stub
   return false;
  }
});

..或者覆盖MapActivity的dispatchTouchEvent(MotionEvent)。但是在这种情况下必须考虑的是适当地转发事件。

The handler isn't being executed. I didn't find the exact reason for this behavior yet. Probably the reason is that the MapActivity doesn't automatically forward the event to the registered MapView or it just doesn't get notified about the event, since the motion event actually occurs on the MapView itself and not its parent, the MapActivity.

What can be done instead is to either register the event directly on the MapView
mapView.setOnTouchListener(new OnTouchListener() {

public boolean onTouch(View v, MotionEvent event) {
  // TODO Auto-generated method stub
   return false;
  }
});

..or to override the MapActivity's dispatchTouchEvent(MotionEvent). What has to be considered however is to appropriately forward the event in this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文