如何解决获取android上下文菜单使用的资源ID
我的目标是在Web视图中选择文本时隐藏默认的系统上下文菜单,并拥有自己的自定义菜单,可以触发原始菜单项操作。
为此,我必须能够识别每个菜单项是什么,以便可以将其挂接到相关的自定义按钮上。我已经对具有快捷键的菜单项进行了此操作,但并非全部如此,因此,我求助于嗅探项标题(例如,项ID不稳定)。这种方法的问题在于它不适用于不同的语言。我发现我认为是正确的字符串资源(例如android.R.string.websearch),但是当我用西班牙语进行测试时,菜单项的标题为“Búsquedaen la Web”,而字符串资源为“ Buscar en la Web”。 / p>
因此,我要么需要帮助找到该上下文菜单中各项的正确资源ID /字符串,要么需要帮助找到一种更简单的方法来对其进行检测。这是我的尝试...
for(int n=0;n < systemSelectionMenu.size();n++){
MenuItem item = systemSelectionMenu.getItem(n);
item.setVisible(false); // Hide menu item from system context menu
switch(item.getAlphabeticShortcut()){
case 'c': // copy
contextcopyActionId = item.getItemId();
break;
case 'x': // Cut
contextCutActionId = item.getItemId();
break;
case 'v': // Paste
contextPasteActionId = item.getItemId();
break;
case 'a': // Select all
contextSelectAllActionId = item.getItemId();
break;
default:
// Hack to determine which menu item does what...
Resources res = Resources.getSystem();
// Note some of these resource strings are internal so the only way to get them is through getIdentifier,this means they might change at some point
// Note 2: item.getId() is not stable and cannot be used directly
int webSearchId = res.getIdentifier("websearch","string","android"); // only way to get android.com.internal.R.string.websearch (android.R.string.websearch)
String webSearchString = "";
try {
webSearchString = res.getString(webSearchId);
}catch(NotFoundException e){}
int shareId = res.getIdentifier("share","android"); // only way to get android.com.internal.R.string.websearch (android.R.string.websearch)
String shareString = "";
try {
shareString = res.getString(shareId);
}catch(NotFoundException e){}
int autoFillId = res.getIdentifier("autofill","android"); // only way to get android.com.internal.R.string.websearch (android.R.string.websearch)
String autoFillString = "";
try {
autoFillString = res.getString(autoFillId);
}catch(NotFoundException e){}
if(item.getTitle().equals("Web search") || item.getTitle().equals(webSearchString)) {
contextWebSearchActionId = item.getItemId();
}else if(item.getTitle().equals("Share") || item.getTitle().equals(shareString)) {
contextShareActionId = item.getItemId();
}else if(item.getTitle().equals("Autofill") || item.getTitle().equals(autoFillString)) {
contextAutoFillActionId = item.getItemId();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。