Android WebView的使用

WebView可以使得網頁輕鬆的內嵌到app裏,還可以直接跟js相互調用。

webview有兩個方法:setWebChromeClient 和 setWebClient

setWebClient:主要處理解析,渲染網頁等瀏覽器做的事情

setWebChromeClient:輔助WebView處理Javascript的對話框,網站圖標,網站title,加載進度等

WebViewClient就是幫助WebView處理各種通知、請求事件的。

使用WebView:可以在代碼中和佈局中使用。

代碼中:創建WebView的實例加入到Activity view tree中

WebView webview = new WebView(this);  
setContentView(webview); 

佈局中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
    android:id="@+id/wv_shop"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</WebView>

</LinearLayout>

對WebView的設置

        wvShop.loadUrl("http://demo3.tp-shop.cn");
        WebSettings webSettings = wvShop.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        wvShop.setWebViewClient(mWebViewClientBase);
        wvShop.setWebChromeClient(mWebChromeClientBase);

         private WebViewClientBase mWebViewClientBase = new WebViewClientBase();

    private class WebViewClientBase extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.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);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
        }

        @Override
        public void doUpdateVisitedHistory(WebView view, String url,
                                           boolean isReload) {
            super.doUpdateVisitedHistory(view, url, isReload);
        }
    }

    private WebChromeClientBase mWebChromeClientBase = new WebChromeClientBase();

    private class WebChromeClientBase extends WebChromeClient {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            TpShopActivity.this.setProgress(newProgress * 1000);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
        }

        @Override
        public void onReceivedTouchIconUrl(WebView view, String url,
                                           boolean precomposed) {
            super.onReceivedTouchIconUrl(view, url, precomposed);
        }

        @Override
        public boolean onCreateWindow(WebView view, boolean isDialog,
                                      boolean isUserGesture, Message resultMsg) {
            return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
        }

    }

對返回鍵的處理:

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        if (wvShop.canGoBack()){
            wvShop.goBack();
        }else{
            finish();
        }
    }

參考文章:1.http://blog.csdn.net/yaodong379/article/details/51960451 使用方法詳細。
2.http://blog.csdn.net/typename/article/details/39030091 有對webview方法的介紹。
3.http://zouhuajian01.blog.163.com/blog/static/117698772012111511449668/ 關於WebView的縮放

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章