任何视图的Android上下文菜单



据我所知,不仅可以为listView注册上下文菜单。今天,我正在尝试为自定义ImageView注册上下文菜单。这样:

// In onCreateView() method of activity
registerForContextMenu(mViewHolder.profileImageView);
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    switch (v.getId()) {
        case R.id.profile_imageview:
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
            menu.add(Menu.NONE, CONTEXT_MENU_ITEM_CHANGE_PICTURE, 0, "Change Picture");
    }
}
@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    switch (item.getItemId()) {
        case CONTEXT_MENU_ITEM_CHANGE_PICTURE:
            //Toast.makeText(mParentActivity, "Delete " + info.position + "th item", Toast.LENGTH_LONG).show()
            Toast.makeText(this, "Go to library", Toast.LENGTH_SHORT).show();
            return true;
        default:
            return true;
    }
}

我没有找到没有ListView的注册上下文菜单的任何示例。有人会给我一个例子还是可以找出我缺少什么?

您需要写super.onCreateContextMenu(menu, v, menuInfo);毕竟所有代码如下:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    switch (v.getId()) {
        case R.id.profile_imageview:
            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
            menu.add(Menu.NONE, CONTEXT_MENU_ITEM_CHANGE_PICTURE, 0, "Change Picture");
            super.onCreateContextMenu(menu, v, menuInfo);
    }
}

相关内容

  • 没有找到相关文章

最新更新