如何在OnContextItemSelected事件中获取View?

时间:2013-07-17 00:47:30

标签: android

在public void onCreateContextMenu(ContextMenu菜单,View v,ContextMenuInfo menuInfo}事件中,

我想我可以通过arg View v使用以下内容知道哪个控件启动onCreateContextMenu事件,对吧? ImageView imageview =(ImageView)v

但是在public boolean onContextItemSelected(MenuItem item)中,我找不到相同的arg,我该怎么办?谢谢!

2 个答案:

答案 0 :(得分:17)

您可以像这样使用ContextMenu.ContextMenuInfo

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    int index = info.position;
}

您还可以获得显示菜单的确切视图:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    int index = info.position;
    View view = info.targetView;
}

看看这些问题:

Android: How to find the position clicked from the context menu

Identifying the view selected in a ContextMenu (Android)

答案 1 :(得分:0)

您可以使用

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);
  //  }
  }