打开上下文菜单而不注册视图

发布于 2024-09-13 01:18:01 字数 215 浏览 2 评论 0原文

如何在不使用 registerForContextMenu() 注册视图的情况下打开视图的上下文菜单?

我的活动覆盖 onTouchEvent 来检测运动,如果我使用 registerForContextMenu() 它就会停止工作。我正在检测到长按,因此我想将该请求转发给为我构建菜单的方法,但不知道它是否可以这样工作。

How can I open a context menu for a view without registering it using registerForContextMenu()?

My activity overrides onTouchEvent to detect motion, and if I use registerForContextMenu() it stops working. I'm detecting a long press, so I'd like to forward that request to a method that would build the menu for me, but don't know if it can work that way.

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

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

发布评论

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

评论(2

何止钟意 2024-09-20 01:18:01

我相信你可以使用 查看。 setOnCreateContextMenuListener 在布局初始化时手动将您的 Activity 绑定为视图的上下文菜单提供程序,然后调用 View.showContextMenu 一旦检测到长按调出菜单,就会在视图上显示。它应该会调用您的 onCreateContextMenu 调用,就像您以正常方式注册它一样。

编辑:好的,这与覆盖视图的触摸事件侦听器具有相同的效果。鉴于此,我能想到的唯一解决方案是创建一个隐藏视图并注册为视图的上下文菜单提供程序,然后要求它在长时间触摸时显示如上所述的上下文菜单看到真实/可见的视图。

I believe you can use View.setOnCreateContextMenuListener to manually bind your activity as the context menu provider for a view at layout initialization time, and then call View.showContextMenu on the view once you detect a long press to bring up the menu. It should call through to your onCreateContextMenu call just as if you'd registered it the normal way.

EDIT: Okay, this has the same effect of overriding the touch event listener for the view. Given that, the only solution I can think of is to create a hidden view and register to be that view's context menu provider, and then ask it to show a context menu as above when a long touch on the real/visible view is seen.

楠木可依 2024-09-20 01:18:01

我的 onTouchEvent 有以下几行。它并不漂亮,但很有效。

private void onLongPressed(View view) {
    registerForContextMenu(view);
    view.getParent().showContextMenuForChild(view);
    unregisterForContextMenu(view);
}

如果我在 onTouchListener 中检测到长按,则 onLongPressed 函数正在调用。

My onTouchEvent have the followed lines. It's not beautiful, but it's working.

private void onLongPressed(View view) {
    registerForContextMenu(view);
    view.getParent().showContextMenuForChild(view);
    unregisterForContextMenu(view);
}

The onLongPressed-function is calling, if I detect a long press in my one onTouchListener.

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