gpt4 book ai didi

android - 是否可以在同一个 Activity 中有 2 个上下文菜单?

转载 作者:行者123 更新时间:2023-11-29 15:23:49 24 4
gpt4 key购买 nike

我有一个带有 TextView 和图像图标的 Activity 。我已经通过覆盖 onCreateContextMenu() 为图像图标创建了上下文菜单.

不过,我还想为 TextView 添加另一个上下文菜单。但是,我注意到我已经在使用 onCreateContextMenu():

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(getString(R.string.option1));
menu.add(0, v.getId(), 0, getString(R.string.option2));
menu.add(0, v.getId(), 0, getString(R.string.options3));
}

因此,是否可以为 TextView 添加另一个上下文菜单?

最佳答案

您可以使用 onCreateContextMenu 中传递的 View 对象来确定菜单的所有者并相应地填充菜单。

您的代码应如下所示:

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
super.onCreateContextMenu(menu, v, menuInfo);
switch (v.getId()) {
case R.id.imageIconId:
menu.setHeaderTitle(getString(R.string.option1));
menu.add(0, v.getId(), 0, getString(R.string.option2));
menu.add(0, v.getId(), 0, getString(R.string.options3));
break;
case R.id.textViewid:
// do whatever you want with the menu object.
break;
}
}

关于android - 是否可以在同一个 Activity 中有 2 个上下文菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15177074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com