WebView隨學筆記

來源:互聯網
上載者:User

標籤:

對於WebView而言我們需要掌握的內容是:

/** 學習目標:webView * 1).將WebView加入到應用中 * 2).使用WebView載入頁面 * 3).擷取網路訪問 * 4).在WebView中使用Javascript * 5).處理頁面導航 * 6).後退與前進 * 7).判斷頁面載入過程 * 8).WebView緩衝的運用 */

對此一一記錄

1).加入WebView:  XML布局檔案中添加&在Activity中綁定

    <WebView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/webView"        android:layout_alignParentTop="true"        android:layout_centerHorizontal="true" />

  

2).使用WebView載入頁面:使用webview.loadUrl(url)方法

 webview.loadUrl("http://www.baidu.com");

3).擷取網路存取權限:在AndroidManifest.xml檔案中添加

<uses-permission android:name="android.permission.INTERNET"/>

整檔案:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.as.webviewtest">    <!--網路許可權-->    <uses-permission android:name="android.permission.INTERNET"/>    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

4).在WebView中使用JavaScript

  需要擷取一個WebSettings的執行個體,調用其方法setJavaScriptEnabled(true);

  代碼:

WebSettings ws=webview.getSettings();ws.setJavaScriptEnabled(true);

5).處理頁面導航,不使用系統本地瀏覽器或第三方瀏覽器(需要在擷取網路存取權限的前提下):

  調用webview.setWebViewClient(new WebViewClient(){});通過重寫方法來修改

  代碼:

webview.setWebViewClient(new WebViewClient(){            //重寫方法     public boolean shouldOverrideUrlLoading(WebView view, String url) {                //true在WebView中開啟,false在系統或第三方瀏覽器中開啟                view.loadUrl(url);                return true;            }        });

6).後退與前進:

  重寫onKeyDown事件,主要是webview的goback()和goforward()方法

  代碼:

  public boolean onKeyDown(int keyCode, KeyEvent event) {      if(keyCode==KeyEvent.KEYCODE_BACK){          if(webview.canGoBack()){                webview.goBack();                return true;          }else{                System.exit(0);          }      }

 

7).判斷頁面載入過程:

   調用&重寫

   代碼:

webview.setWebChromeClient(new WebChromeClient(){    public void onProgressChanged(WebView view,int newProgress){        if(newProgress==0){                         //載入完成        }else{        //載入中            }    }  });    

 

8).緩衝的運用:

  WebSettings.setCacheMode(傳入預置參數即可);

  參數有:

 

 

  

 

WebView隨學筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.