如何在 Android 上的 ListActivity 中实现上下文菜单?
如何实现通过长按或点击使用内置布局和 ListAdapter 的 ListActivity 触发的上下文菜单?
How do you implement a context menu triggered by a long click or tap on a ListActivity that is using the built in layouts and a ListAdapter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 onCreate 方法上调用 registerForContextMenu像这样:
然后填充菜单 onCreateContextMenu(ContextMenu 菜单,View 视图,ContextMenuInfo menuInfo)。 menuInfo 参数可以通过这种方式提供有关哪个项目被长按的信息:
并且您可以以通常的方式调用 menu.add:
当用户选择一个选项时,onContextItemSelected 被调用。 还有 onMenuItemSelected 和文档中没有明确解释这一事实,只是说您使用其他方法从上下文菜单接收调用; 请注意,不要共享 ID。
在 onContextItemSelected 上,您可以通过调用 getMenuInfo():
On the onCreate method call registerForContextMenu like this:
and then populate the menu on onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo). The menuInfo argument can provide information about which item was long-clicked in this way:
and you add menu items in the usual way calling menu.add:
and when the user picks an option, onContextItemSelected is called. Also onMenuItemSelected and this fact is not explicitly explained in the documentation except to say that you use the other method to receive the calls from the context menu; just be aware, don't share ids.
On onContextItemSelected you can get ahold of the MenuInfo and thus the id of the item selected by calling getMenuInfo():
您还应该查看 Activity.registerForContextMenu (查看)。
You should also look at Activity.registerForContextMenu(View).