android - 从对话框窗口调用时不会触发 onContextItemSelected

标签 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);
}

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

最佳答案

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

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

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);
    }
}

现在,您可以创建此对话框的实例,并且您的 View 将成功注册上下文项。所以你的 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() 中进行处理即可。

关于android - 从对话框窗口调用时不会触发 onContextItemSelected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29586995/

相关文章:

java - android fileoutputstream 找不到文件异常

android - 按下后退时关闭 AlertDialog

java - Android:Dialog Builder 多选设置数组

java - 带有游戏分数的自定义对话框 android

android - 如何创建由少数应用程序共享的 sharedpref?

创建模拟器时 Android SDK 错误

java - 带圆角的 Android 对话框 - 仍然显示没有圆角半径的背景

android - 如何防止屏幕旋转时显示对话框

android - 以编程方式从 Activity/服务/接收器更新小部件

android - 在 sqlite android 中转换为整数不返回准确结果