获取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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...