webview使用总结

2016-12-03  本文已影响12人  水瓶鱼

WebView myWebView = (WebView) findViewById(R.id.webview);  
WebSettings webSettings = myWebView.getSettings();  
webSettings.setJavaScriptEnabled(true);  

WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface('提供javasript可执行的方法的对象', "javascript中的对象名称");

    * 在javascript中调用APP native方法
* 使用webview打开链接

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());

    * webview设置webviewClient后默认所有链接在webview中打开,如果想对不同链接做不同处理可以重载webviewClient的shouldOverrideUrlLoading方法,eg:
    ```
private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
    }
}
    ```
* app调用javascript方法

webview.evaluteJavascript(String script,ValueCallback<String> resultCaallback)

上一篇 下一篇

猜你喜欢

热点阅读