0

我想在用户选择选项菜单项时打开上下文菜单。但是registerForContextMenu();需要查看对象。当用户选择选项菜单项时,有没有办法打开上下文菜单?

4

1 回答 1

0

第 1 步:创建 ContextMenu 方法

@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case DELETE_ID:
                AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        }
        return super.onContextItemSelected(item);
    }

第 2 步:创建对话框

CharSequence[] items = {"item1","item2","item3"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Options");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int position) {
                    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            }
        }).show();
于 2013-09-24T10:49:49.640 回答