自定义组件中的上下文菜单

发布于 2024-11-01 23:36:21 字数 1010 浏览 1 评论 0原文

我正在构建一个自定义组件,我想在其中添加上下文菜单。到目前为止,我已经成功创建并显示了上下文菜单,执行以下操作:

public class CustomComponent extends LinearLayout implements OnClickListener, OnCreateContextMenuListener   {

private final MenuInflater menuInflator;

public CustomComponent(final Context context) {
    this(context, null);
}

public CustomComponent(final Context context, final AttributeSet attrs) {
    super(context, attrs);

    menuInflator = new MenuInflater(context);

    final LayoutInflater inf = LayoutInflater.from(context);
    inf.inflate(R.layout.component, this, true);

    setOnClickListener(this);
    setOnCreateContextMenuListener(this);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    menuInflator.inflate(R.menu.menu_context, menu);
}

@Override
public void onClick(View v) {
    showContextMenu();
}

}

我现在面临的问题是我不知道如何在该组件内调用 onContextItemSelected() 函数。我正在查看 Activity 中的一些源代码,注意到它有一个 mWindow 成员来保存这些回调。我不太确定这种方式是否可行。有什么建议吗?

I'm building my a custom component where i want to have a context menu on. So far i've succesfully created and shown the context menu doing the following:

public class CustomComponent extends LinearLayout implements OnClickListener, OnCreateContextMenuListener   {

private final MenuInflater menuInflator;

public CustomComponent(final Context context) {
    this(context, null);
}

public CustomComponent(final Context context, final AttributeSet attrs) {
    super(context, attrs);

    menuInflator = new MenuInflater(context);

    final LayoutInflater inf = LayoutInflater.from(context);
    inf.inflate(R.layout.component, this, true);

    setOnClickListener(this);
    setOnCreateContextMenuListener(this);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    menuInflator.inflate(R.menu.menu_context, menu);
}

@Override
public void onClick(View v) {
    showContextMenu();
}

}

The problem i'm facing now is that i don't know how to get the onContextItemSelected() function to be called inside this component. I was looking through some source in Activity and noticed that it has a mWindow member that holds these callbacks. I'm not really sure if it is possible to it this way. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后彩虹 2024-11-08 23:36:21

在搜索其他选项后,我发现了一个非常简单的解决方案:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    menuInflator.inflate(R.menu.menu_context, menu);
    menu.getItem(0).setOnMenuItemClickListener(this);
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    // Do something
    return false;
}

当然,您需要实现 OnMenuItemClickListener 才能使其工作

Well after searching for other options i found a very simpel solutions:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    menuInflator.inflate(R.menu.menu_context, menu);
    menu.getItem(0).setOnMenuItemClickListener(this);
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    // Do something
    return false;
}

ofcouse you need to implement OnMenuItemClickListener for this to work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文