从Dialog窗口调用onContextItemSelected时不会触发它

Sai*_*Sai 14 android android-dialog

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);
}
Run Code Online (Sandbox Code Playgroud)

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

ucs*_*nil 7

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

您正尝试在对话框中注册视图项的上下文菜单,但是要从活动中注册.这种方法是错误的.您实际上需要子类化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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

虽然您最初将活动的上下文传递给您正在创建的Dialog,但您无法传递这样的上下文菜单创建侦听器.要做到这一点,你必须按照我上面的说明继承你的对话框.

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

ringtone_layout.setOnCreateContextMenuListener(this);
Run Code Online (Sandbox Code Playgroud)

你有关强行调用上下文菜单的说法实际上是正确的.您只是调用常规菜单,然后将该ID传递给上下文菜单回调.if子句通过,因为id来自上下文菜单功能.

在进一步阅读之后,看起来您应该在onMenuItemSelected()上进行菜单处理,而不是在onContextItemSelected()中进行.所以你现在拥有的是正确的,你不需要强行调用其他方法.只需在onMenuItemSelected()中进行处理即可.