2个Webview的2个不同的上下文菜单

时间:2018-02-07 14:48:53

标签: java android webview contextmenu

我需要创建Conext菜单以在webview中长按时打开链接。但我的代码仅适用于第二次webview。我该怎么办?

以下是代码:

@Override
public void onCreateContextMenu(ContextMenu Menu, View v, ContextMenu.ContextMenuInfo MenuInfo){
    super.onCreateContextMenu(Menu, v, MenuInfo);
    final WebView.HitTestResult result1 = webview1.getHitTestResult();
    final WebView.HitTestResult result2 = webview2.getHitTestResult();
    if(v.getId() == R.id.webview2){
        if (result1.getType() == WebView.HitTestResult.ANCHOR_TYPE || 
            result1.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) { 
            Menu.add(0, 1, 0, "Open Link").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem menuItem) 
                    {
                        webview1.loadUrl(result1.getExtra());
                        return false;
                    }
                });
        }
        else if(v.getId() == R.id.webview1)
        {
            if (result2.getType() == WebView.HitTestResult.ANCHOR_TYPE || 
                result2.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) { 
                Menu.add(0, 1, 0, "Open Link").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem menuItem) 
                        {
                            webview2.loadUrl(result2.getExtra());
                            return false;
                        }
                    });
            }

        }
                }
                }

1 个答案:

答案 0 :(得分:0)

看起来你的第二个 if 用于检查视图是否是webview1是错误的。试试这个:

public void onCreateContextMenu(ContextMenu Menu, View v, ContextMenu.ContextMenuInfo MenuInfo){
    super.onCreateContextMenu(Menu, v, MenuInfo);
    final WebView.HitTestResult result1 = webview1.getHitTestResult();
    final WebView.HitTestResult result2 = webview2.getHitTestResult();
    if(v.getId() == R.id.webview2){
        if (result1.getType() == WebView.HitTestResult.ANCHOR_TYPE ||
                result1.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
            Menu.add(0, 1, 0, "Open Link").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem)
                {
                    webview1.loadUrl(result1.getExtra());
                    return false;
                }
            });
        }
    }
    else if(v.getId() == R.id.webview1)
    {
        if (result2.getType() == WebView.HitTestResult.ANCHOR_TYPE ||
                result2.getType() == WebView.HitTestResult.SRC_ANCHOR_TYPE) {
            Menu.add(0, 1, 0, "Open Link").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem)
                {
                    webview2.loadUrl(result2.getExtra());
                    return false;
                }
            });
        }

    }
}