从 Dialog 调用 onContextItemSelected 时未触发 window

onContextItemSelected is not triggered when it is called from Dialog window

Dialog dialog;

private void opendialog() {
    dialog = new Dialog(MainActivity.this);
    dialog.setContentView(R.layout.popup);
    dialog.setTitle(R.string.msettings);
    RelativeLayout reply_layout = (RelativeLayout) dialog
            .findViewById(R.id.reply_layout);
    final RelativeLayout ringtone_layout = (RelativeLayout) dialog
            .findViewById(R.id.ringtone_layout);
    registerForContextMenu(ringtone_layout);

    ringtone_layout.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            openContextMenu(ringtone_layout);
        }
    });
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Select The Action");
    menu.add(0, v.getId(), 0, "Edit");
    menu.add(0, v.getId(), 1, "Delete");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    System.out.println("Inside onContextItemSelected");
    return super.onContextItemSelected(item);
}

在对话框中使用上下文菜单时永远不会调用 onContextItemSelected。我的代码有什么问题吗?提前致谢..

注意:由于这个答案似乎得到了一些关注(赞成票),我正在编辑代码片段以反映对问题的更简洁的答案。

您正在尝试为对话框中的视图项注册上下文菜单,但来自 activity。这种做法是错误的。您实际上需要子类化 Dialog ,然后在那里创建和扩展您的视图,然后在那里覆盖 onCreateContextMenu() 来完成您的工作并为上下文菜单注册您的视图。然后,您应该在此处创建该对话框的实例。所以它会是这样的:

public class Mydialog extends Dialog {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup);
        dialog.setTitle(R.string.msettings);
        RelativeLayout reply_layout = (RelativeLayout) findViewById(R.id.reply_layout);
        final RelativeLayout ringtone_layout = (RelativeLayout) findViewById(R.id.ringtone_layout);
        registerForContextMenu(ringtone_layout);
        ringtone_layout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                openContextMenu(ringtone_layout);
            }
        });
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Select The Action");
        menu.add(0, v.getId(), 0, "Edit");
        menu.add(0, v.getId(), 1, "Delete");
    }

    // You should do the processing for the selected context item here. The
    // selected context item gets passed in the MenuItem parameter in 
    // the following method. In my answer I am force calling the onContextItemSelected()
    // method but you are free to do the actual processing here itself
    @Override 
    public boolean onMenuItemSelected(int aFeatureId, MenuItem aMenuItem) {
        if (aFeatureId==Window.FEATURE_CONTEXT_MENU)
            return onContextItemSelected(aMenuItem);
        else 
            return super.onMenuItemSelected(aFeatureId, aMenuItem);
    } 

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // Avoid using System.out.println() - instead use Android Logging
        return super.onContextItemSelected(item);
    }
}

现在,您可以创建此对话框的实例,并且您的视图将成功注册上下文项。所以你的 openDialogMethod() 现在看起来像:

private void opendialog() {
    dialog = new MyDialog(MainActivity.this);
    // the context menu will now be registered
    dialog.show();
}

虽然您最初将 activity 的上下文传递给您正在创建的对话框,但您不能像那样传递上下文菜单创建侦听器。为此,您必须像我上面显示的那样子类化您的对话框。

编辑: 看了 Zsolt 的回答后,我发现我忽略了这行你没有的代码。您需要:

ringtone_layout.setOnCreateContextMenuListener(this);

你说的强行调用上下文菜单是真的。您只是调用常规菜单,然后将该 id 传递给上下文菜单回调。 if 子句通过,因为 id 来自上下文菜单功能。

经过进一步阅读,您似乎应该在 onMenuItemSelected() 而不是 onContextItemSelected() 中进行菜单处理。所以你现在拥有的是正确的,你不需要强行调用另一个方法。只需在 onMenuItemSelected() 中进行处理即可。

public class MusicsetDialog extends Dialog implements OnClickListener {
    private RelativeLayout ringtone_layout;

    public MusicsetDialog(Context context) {
        super(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_popup);
        setTitle(R.string.msettings);

        ringtone_layout = (RelativeLayout) findViewById(R.id.ringtone_layout);

        ringtone_layout.setOnClickListener(MusicsetDialog.this);

        registerForContextMenu(ringtone_layout);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.ringtone_layout:
            openContextMenu(ringtone_layout);
            break;          
        default:
            break;
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Select The Action");
        // groupId, itemId, order,title
        menu.add(0, v.getId(), 0, "Edit");
        menu.add(0, v.getId(), 1, "Delete");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override 
    public boolean onMenuItemSelected(int aFeatureId, MenuItem aMenuItem) {
        if (aFeatureId==Window.FEATURE_CONTEXT_MENU)
            return onContextItemSelected(aMenuItem);
        else 
            return super.onMenuItemSelected(aFeatureId, aMenuItem);
    } 

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // System.out.println("onContextItemSelected");
        Log.d("MusicsetDialog", "onContextItemSelected");
        return super.onContextItemSelected(item);
    }
}

我已经为mydialog创建了一个子类并扩展了Dialog,并遵循了Sunil的过程。但是仍然 onContextItemSelected 没有被调用。所以在做了一些研究之后,我覆盖 onMenuItemSelected 并将选定的 MenuItem 传递给 onContextItemSelected 并且它工作正常。可能是我在强行呼叫 onContextItemSelected。我不知道。我是 android 的新手,如果能解释为什么 onContextItemSelected 仍未自动调用,我将不胜感激。谢谢..

此问题已在以下 SO 线程中解决: onContextItemSelected doesn't get called and onContextItemSelected never called using a Dialog with a ListView.

如果 none 个答案对您有帮助,请试试这个: return false 在您的 activity 及其 片段 .

中的 onContextItemSelected 方法中

试试这个

new AlertDialog.Builder(this) 
        .setSingleChoiceItems(items, 0, null) 
        .setPositiveButton(R.string.ok_button_label, new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
                int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
                // Do something useful withe the position of the selected radio button 
            } 
        }) 
        .show();