Create a new page in the WebView window label is target = "_ blank" when there is no response

If WebView page linked <a> label is target = "_ blank", and your WebView and did not do any treatment, this time will be invalid clicks situation, is not it somehow?

target defined by a plurality of types
_blank: Link said it would open a new window
_self: Enables the current window or frame where the link (the default)
_parent: Enables link in the parent window or framework
_top: to open a new link at the top level representation framework
It also can directly specify the frame name to the target, but also represents a link in an open framework, such as csdn click on the left tree, open a new link is this on the right!

The solution is straightforward, inheritance WebChromeClient, rewrite onCreateWindow is the key,

1, first set up, or will not go callbacks:

webSetting.setSupportMultipleWindows(true);
Second, then look at the code:
@Override
public boolean onCreateWindow(WebView webView, boolean isDialog, boolean isUserGesture, Message resultMsg) {

    X5WebView x5WebView = new X5WebView(activity);


    X5WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
    x5WebView.setWebChromeClient(new CustomWebChromeClient(activity));
    x5WebView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //拦截url,跳转新窗口
            if (activity != null) {
                Intent intent = new Intent(activity, MyWebActivity.class);
                intent.putExtra(Constants.INTENT_KEY_URL, url);
                activity.startActivity(intent);
            }
            //防止触发现有界面的WebChromeClient的相关回调
            return true;
        }
    });
    transport.setWebView(x5WebView);
    resultMsg.sendToTarget();

    return true;
}

Published 59 original articles · won praise 88 · views 190 000 +

Guess you like

Origin blog.csdn.net/geofferysun/article/details/79570182