WebView使用

WebView简单使用

基本功能


        //【1】post发送数据
        String params = "name=mlb&age=18";
        webView.postUrl("file:///android_asset/index.html", params.getBytes());

        //【2】加载HTML数据
        String data = "hellohello";
        webView.loadData(data, "text/html", "utf-8");

        //【3】【设置选项】
webView.loadUrl("http://www.baidu.com");
WebSettings ws = webView.getSettings();
        assert ws !=null;
        //设置是否支持JavaScript
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(true);
        //显示缩放按钮
        ws.setBuiltInZoomControls(true);
        //双击放大和缩小
        ws.setUseWideViewPort(true);
        webView.requestFocus();
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        //为webview设置监听
        webView.setWebViewClient(new WebViewClient(){
            //设置点击链接在当前webview中显示
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                webView.loadUrl(url);
                return true;
            }

            //开始加载网页
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            //网页加载结束
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });

        //解决5.0部分图片不显示
        ws.setAllowFileAccess(true);
        ws.setSupportMultipleWindows(true);

        //【解决html5网页不能加载(空白或显示不完全)】
        ws.setDomStorageEnabled(true);
        ws.setBlockNetworkImage(false);//解决图片不显示

        //【解决WebView的Thread警告】
        //A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread.
        webView.post(new Runnable() {
            @Override
            public void run() {
                webviewID.loadUrl(data);
            }
        });

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP{
            ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

        webView.setWebChromeClient(new WebChromeClient(){

            //获取网页标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }

            //获取网页加载进度0-100
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
        });


    //设置回退键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK && webView.canGoBack()){
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

JavaScript交互

//为webview添加JavaScript交互
        webView.addJavascriptInterface(new MyObject(), "obj");

public class MyObject {

        @JavascriptInterface
        public void clickOnAndroid() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    webView.loadUrl("javascript:myfun()");
                }
            });
        }
    }

//此为android assets目录下的index.html文件。
//通过点击调用当前窗口下的Java对象obj的方法,进而调用JavaScript的myfun方法,获取到将当前图片document.getElementById("imgid"),将其src替换为vla.jpg


    
        hello
        
    
    
        hello webview!
        "window.obj.clickOnAndroid()">
            "mlb.jpg" id="imgid" width="300" height="300">
        
    

适应手机屏幕

String s = "\n" +
                "<html>\n" +
                "<head>\n" +
                "\t<style type=\"text/css\">\n" +
                "\t\t.img-ks-lazyload{\n" +
                /*"\t\t\ttext-align: center;\n" +*/
                "\t\t}\n" +
                "\t\t.img-ks-lazyload img{\n" +
                "\t\t\twidth: 100% !important;\n" +
                "\t\t}\n" +
                ".img-ks-lazyload p,\n" +
                ".img-ks-lazyload div,\n" +
                ".img-ks-lazyload table {width: 100% !important; font-size: 32px !important;}.img-ks-lazyload span{font-size: 32px !important;}\n" +

                ".img-ks-lazyload { min-width: 320px; max-width: " + mScreenWidth + "px; overflow: hidden;}\n" +
                ".img-ks-lazyload embed[type=\"application/x-shockwave-flash\"] { display: none;}\n" +
                "\tstyle>\n" +
                "head>\n" +
                "<body>" +
                "<div class=\"img-ks-lazyload\">" +
                goodsBody +
                "div>" +
                "body>\n" +
                "html>";

webview.loadDataWithBaseURL(null, s, "text/html", "utf-8", null);

你可能感兴趣的:(安卓组件)