WebView个人使用总结

一、首先了解一下webview的一些基本属性


authorize_wechat.getSettings().setJavaScriptEnabled(true);//支持js
        authorize_wechat.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); //支持弹窗


        authorize_wechat.getSettings().setUseWideViewPort(true);//将图片调整到webview大小
        authorize_wechat.getSettings().setLoadWithOverviewMode(true);//缩放至屏幕大小


        authorize_wechat.getSettings().setLoadsImagesAutomatically(true); //支持自动加载图片
        authorize_wechat.getSettings().setDefaultTextEncodingName("utf-8");//设置编码格式

//缩放操作
        authorize_wechat.getSettings().setSupportZoom(true); //支持缩放,默认为true。是下面那个的前提。
        authorize_wechat.getSettings().setBuiltInZoomControls(true); //设置内置的缩放控件。若为false,则该WebView不可缩放
        authorize_wechat.getSettings().setDisplayZoomControls(false); //隐藏原生的缩放控件

authorize_wechat.loadUrl(mUrl);  //调用系统浏览器打开网页


二、直接在webview打开链接,不调用系统浏览器

authorize_wechat.loadUrl(mUrl);

        authorize_wechat.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                    return true;

            }
        });
 

以上这些属性,应该说可以满足简单的webview使用,在webview跳转链接了;

三、webview拓展

    //访问网页发生错误时,会调用onReceivedSslError方法,这个方法一般不需要理会

    @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                super.onReceivedSslError(view, handler, error);
                handler.proceed();
            }


//访问网页成功后,会调用onPageFinished方法,这个方法用的比较多,如果在加载网页的时候本地给了加载窗,那么可以这里面结束加载窗

//我用的时候,项目需要我在网页加载成功后获取到这个网页的HTML源码,不细讲,直接上代码

    @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                view.loadUrl("javascript:window.local_obj.showSource('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
            }

//showSource是js调用的一个对获取到的HTML代码的处理


总结:以上是个人对webview的使用总结,比较基础的东西,主要熟悉它的一些必要属性和基本方法,用起来不会太难,webview和js相互调用的只是我还没用的太多,

   以后用到会再总结,这次先写到这里,以上有错误的地方希望广大网友及时指出,不胜感激!


猜你喜欢

转载自blog.csdn.net/dennyzhang2/article/details/78618729