Android:如何为自定义视图创建上下文菜单
我想显示一个自定义视图的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的自定义视图类应覆盖
getContextMenuInfo()
并返回一个实现ContextMenuInfo
的自定义对象(不是考虑到该界面没有成员,这非常困难!)并且其中包含您希望菜单单击处理程序获取的额外的每个视图数据。然后,您可以通过调用
item.getMenuInfo()
并转换返回的值,从onContextItemSelected(MenuItem item)
获取此对象。Your custom view class should override
getContextMenuInfo()
and return a custom object implementingContextMenuInfo
(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 callingitem.getMenuInfo()
and casting the value returned.