對於android初學者應該都了解webView這個組件。之前我也是對其進行了一些簡單的了解,但是在一個項目中不得不用webview的時候,發現了webview的強大之處,今天就分享一下使用webview的一些經驗。
1、首先了解一下webview。
webview介紹的原文如下:A View that displays web pages. This class is the basis upon which you can roll your own web browser or simply display some online content within your Activity. It uses the WebKit rendering engine to display web pages and includes methods to navigate forward and backward through a history, zoom in and out, perform text searches and more.
從上面你應該了解到了基本功能,也就是顯示網頁。之所以我說webview功能強大是因為它和js的交互非常方便,很簡單就可以實現。
2、webview能做什么?
①webView可以利用html做界面布局,雖然目前還比較少人這么使用,不過我相信當一些客戶端需要復雜的圖文(圖文都是動態生成)混排的時候它肯定是個不錯的選擇。
②直接顯示網頁,這功能當然也是它最基本的功能。
③和js交互。(如果你的js基礎比java基礎好的話那么采用這種方式做一些復雜的處理是個不錯的選擇)。
3、如何使用webview?
這里直接用一個svn上取下的demo,先上demo后講解。demo的結構圖如下:
WebViewDemo.java
package com.google.android.webviewdemo; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.webkit.JsResult; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; /** * Demonstrates how to embed a WebView in your activity. Also demonstrates how * to have javascript in the WebView call into the activity, and how the activity * can invoke javascript. * <p> * In this example, clicking on the android in the WebView will result in a call into * the activities code in {@link DemoJavaScriptInterface#clickOnAndroid()}. This code * will turn around and invoke javascript using the {@link WebView#loadUrl(String)} * method. * <p> * Obviously all of this could have been accomplished without calling into the activity * and then back into javascript, but this code is intended to show how to set up the * code paths for this sort of communication. * */ public class WebViewDemo extends Activity { private static final String LOG_TAG = "WebViewDemo"; private WebView mWebView; private Handler mHandler = new Handler(); @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); mWebView = (WebView) findViewById(R.id.webview); WebSettings webSettings = mWebView.getSettings(); webSettings.setSavePassword(false); webSettings.setSaveFormData(false); webSettings.setJavaScriptEnabled(true); webSettings.setSupportZoom(false); mWebView.setWebChromeClient(new MyWebChromeClient()); mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo"); mWebView.loadUrl("file:///android_asset/demo.html"); } final class DemoJavaScriptInterface { DemoJavaScriptInterface() { } /** * This is not called on the UI thread. Post a runnable to invoke * loadUrl on the UI thread. */ public void clickOnAndroid() { mHandler.post(new Runnable() { public void run() { mWebView.loadUrl("javascript:wave()"); } }); } } /** * Provides a hook for calling "alert" from javascript. Useful for * debugging your javascript. */ final class MyWebChromeClient extends WebChromeClient { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { Log.d(LOG_TAG, message); result.confirm(); return true; } } }
demo.html
<html> <script language="javascript"> /* This function is invoked by the activity */ function wave() { alert("1"); document.getElementById("droid").src="android_waving.png"; alert("2"); } </script> <body> <!-- Calls into the javascript interface for the activity --> <a onClick="window.demo.clickOnAndroid()"><div style="width:80px; margin:0px auto; padding:10px; text-align:center; border:2px solid #202020;" > <img id="droid" src="android_normal.png"/><br> Click me! </div></a> </body> </html>
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/intro" android:padding="4dip" android:textSize="16sp" /> <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> </LinearLayout>
4、如何交互?
①android如何調用js。
調用 形式:
mWebView.loadUrl("javascript:wave()");
其中wave()是js中的一個方法,當然你可以把這個方法改成其他的方法,也就是android調用其他的方法。
②js如何調用android。
調用形式:
<a onClick="window.demo.clickOnAndroid()">
mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");
代碼中的clickOnAndroid()是“demo”對應的對象:new DemoJavaScriptInterface() 中的一個方法。
③雙向交互。
當然是把前面的兩種方式組合一下就可以了。
5、講解demo。
現在你一定了解了android和js的交互了。是時候分析一些demo了,根據上面講的你也應該比較清楚了。具體交互流程如下:
①點擊圖片,則在js端直接調用android上的方法clickOnAndroid();
②clickOnAndroid()方法(利用線程)調用js的方法。
③被②調用的js直接控制html。
個人總結:利用webView的這種方式在有些時候UI布局就可以轉成相應的html代碼編寫了,而html布局樣式之類有DW這樣強大的工具,而且網上很多源碼,很多代碼片。在UI和視覺效果上就會節省很多時間,重復發明輪子沒有任何意義。
歡迎各位朋友留言交流。
(轉自:http://www.cnblogs.com/vanezkw/archive/2012/07/02/2572799.html)