How to get View in OnContextItemSelected event?
在public void onCreateContextMenu(ContextMenu菜单,视图V,ContextMenuInfo menuInfo事件,
我想我可以知道arg view v使用以下方法启动onCreateContextMenu事件的哪个控件,对吗?imageview imageview=(imageview)v
但是在公共Boolean OnContextItemSelected(menuitem item)中,我找不到相同的参数,我该怎么做?谢谢!
您可以这样使用
1 2 3 4 5 | @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); int index = info.position; } |
您还可以获得显示菜单的确切视图:
1 2 3 4 5 6 | @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); int index = info.position; View view = info.targetView; } |
看看这些问题:
Android:如何从上下文菜单中找到点击的位置
标识在ContextMenu(Android)中选择的视图
U可以使用
1 2 3 4 5 6 7 8 | View v2; @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); v2=v; getMenuInflater().inflate(R.menu.cnt_menu, menu); // } } |