Android:如何为自定义视图创建上下文菜单

发布于 2024-09-28 15:08:19 字数 496 浏览 0 评论 0原文

我想显示一个自定义视图的 ContextMenu,它不是 AdapterView 的一部分。

我打电话过去

myActivity.registerForContextMenu(myView);

,上下文菜单已经出现了。

但我不知道如何访问 myView 的数据,

@Override
onContextItemSelected(MenuItem item) //...

我想我可以在这来自文档中创建一个自定义 menuInfo

@Override
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) //...

:“menuInfo - 有关应显示上下文菜单的项目的额外信息。此信息会有所不同取决于 v 的类别。”

谁能给我提示吗?

I'd like to show a ContextMenu for a custom View which is not part of an AdapterView.

I called

myActivity.registerForContextMenu(myView);

and the ContextMenu already shows up.

But I don't know how to access the data of myView in

@Override
onContextItemSelected(MenuItem item) //...

I thought I could create a custom menuInfo in

@Override
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) //...

This is from the docs: "menuInfo - Extra information about the item for which the context menu should be shown. This information will vary depending on the class of v."

Can anyone give me a hint?

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

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

发布评论

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

评论(1

瞄了个咪的 2024-10-05 15:08:19

您的自定义视图类应覆盖 getContextMenuInfo() 并返回一个实现 ContextMenuInfo 的自定义对象(不是考虑到该界面没有成员,这非常困难!)并且其中包含您希望菜单单击处理程序获取的额外的每个视图数据。

然后,您可以通过调用 item.getMenuInfo() 并转换返回的值,从 onContextItemSelected(MenuItem item) 获取此对象。

Your custom view class should override getContextMenuInfo() and return a custom object implementing ContextMenuInfo (not very hard considering the interface has no members!) and which contains the extra per-view data you want the menu-click handler to get.

You can then get at this object from onContextItemSelected(MenuItem item) by calling item.getMenuInfo() and casting the value returned.

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