用户单击选项菜单项时打开上下文菜单

时间:2013-09-24 10:30:18

标签: android contextmenu

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

1 个答案:

答案 0 :(得分: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();