确定在文本菜单(安卓)选择的视图 [英] Identifying the view selected in a ContextMenu (Android)

查看:118
本文介绍了确定在文本菜单(安卓)选择的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android中, onContextItemSelected 有一个菜单项参数,因此目前还不清楚如何识别视图选中。 MenuItem.getMenuInfo 可以访问的 Contextmenu.ContextMenuInfo ,但在已知子类提供访问目标来看,似乎没有被在接口上的存取器。

In Android, onContextItemSelected has a single MenuItem argument and so it isn't clear how to identify the view selected. MenuItem.getMenuInfo provides access to Contextmenu.ContextMenuInfo, but while both known subclasses provide access to the target view, there does not appear to be an accessor on the interface.

一种选择是保存查看 onCreateContextMenu 中,依赖于<$ C私有类变量提供$ C> onCreateContextMenu 未在活动再次呼吁前 onContextItemSelected 。另一种是使用的ID 查看为<一个的的itemId 参数href="http://developer.android.com/reference/android/view/Menu.html#add%28int,%20int,%20int,%20int%29"相对=nofollow> ContextMenu.add 。如果我们这样做,我们会再需要使用它(可能是国际化)的标题,以确定从上下文菜单中选择的选项。

One alternative is to save the View provided in onCreateContextMenu in a private class variable which relies on onCreateContextMenu not being called again in the activity before onContextItemSelected. Another is to use the id of the View for the itemId argument of ContextMenu.add. If we do this, we would then need to identify the option selected from the context menu by using its (possibly internationalised) title.

什么是确定最佳方法查看 onContextSelected

What is the best method for identifying the View selected in onContextSelected?

推荐答案

有没有这样的概念,如标识选定的视图在Android的任一选项菜单或上下文菜单。因此,这是相当难以回答你的问题。所以,我会采取一些猜测。

There is no such concept as "identifying the View selected" for either option menus or context menus in Android. Hence, it is rather difficult to answer your question. So, I'll take some guesses.

如果按确定视图中选择你的意思是该菜单选项被选中,也就是 getItemId()菜单项传递给 onOptionsItemSelected() onContextItemSelected()

If by "identifying the View selected" you mean which menu choice was selected, that is getItemId() on the MenuItem that is passed to onOptionsItemSelected() or onContextItemSelected().

如果按确定选择视野中的你的意思是它在的ListView行是一个长抽头,弹出右键菜单,投 getMenuInfo()(呼吁菜单项)以 AdapterView.AdapterContextMenuInfo ,然后请使用 ID 位置值适当根据您的适配器。 在这里看到,使用该技术的示例项目

If by "identifying the View selected" you mean which row in a ListView was the one long-tapped on to bring up the context menu, cast getMenuInfo() (called on the MenuItem) to AdapterView.AdapterContextMenuInfo, then use either the id or the position values as appropriate based on your adapter. See here for a sample project that uses this technique.

如果按确定选择视野中的你的意思是你有一个以上的非 - 的ListView 在活动上下文菜单中,我不会使用UI技术

If by "identifying the View selected" you mean you have more than one non-ListView context menu in an activity, I would not use that UI technique.

这篇关于确定在文本菜单(安卓)选择的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆