For Android Beginners should all understand webview this component. I also had some simple understanding of it before, but when I had to use WebView in a project, I discovered the WebView's strength and shared some experience with WebView today.
1, first understand the webview.
WebView introduces the following text: 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 ACT Ivity. It uses the WebKit rendering engine to display Web pages and includes methods to navigate forward and backward through a h istory, zoom in and out, perform text searches and more.
From the above you should be aware of the basic function, which is to display the Web page. The reason why I say webview is powerful because it and JS interaction is very convenient, very simple can be achieved.
2. What can webview do?
①webview can use HTML to do the interface layout, although it is still relatively few people so use, but I believe that when some clients need complex graphics (graphics and text are dynamically generated) mixed with it is certainly a good choice.
② directly display the Web page, this function is of course its most basic function.
③ and JS interaction. (If your JS base is better than Java, it's a good idea to do some complicated processing in this way.)
3, how to use WebView?
Here, a demo is taken off the SVN, and the demo is explained first. The demo structure is shown below:
Webviewdemo.java
PackageCom.google.android.webviewdemo;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.util.Log;ImportAndroid.webkit.JsResult;Importandroid.webkit.WebChromeClient;Importandroid.webkit.WebSettings;ImportAndroid.webkit.WebView;/*** Demonstrates how to embed a WebView in your activity. Also demonstrates how * to has JavaScript in the WebView call into the activity, and how the activity * can invoke Javas Cript. * <p> * In this example, clicking on the android in the WebView would result in a call to * the activities code in {@linkdemojavascriptinterface#clickonandroid ()}. This code * would turn around and invoke JavaScript using the {@linkWebview#loadurl (String)} * method. * <p> * Obviously all of this could has been accomplished without calling into the activity * and then back into JA Vascript, but this code was intended to show how to set up the * code paths for this sort of communication. * */ Public classWebviewdemoextendsActivity {Private Static FinalString Log_tag = "Webviewdemo"; PrivateWebView Mwebview; PrivateHandler Mhandler =NewHandler (); @Override Public voidonCreate (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 (Newmywebchromeclient ()); Mwebview.addjavascriptinterface (NewDemojavascriptinterface (), "demo"); Mwebview.loadurl ("File:///android_asset/demo.html"); } Final classdemojavascriptinterface {demojavascriptinterface () {}/*** This isn't called on the UI thread. Post a runnable to invoke * Loadurl on the UI thread. */ Public voidclickonandroid () {Mhandler.post (NewRunnable () { Public voidrun () {Mwebview.loadurl ("Javascript:wave ()"); } }); } } /*** Provides a hook for calling ' alert ' from JavaScript. Useful for * debugging your JavaScript. */ Final classMywebchromeclientextendswebchromeclient {@Override Public BooleanOnjsalert (WebView view, string URL, string message, jsresult result) {LOG.D (log_tag, message); Result.confirm (); return true; } }}
Demo.html
<HTML> <Scriptlanguage= "JavaScript"> /*This function was invoked by the activity*/ functionWave () {alert ("1"); document.getElementById ("Droid"). SRC="Android_waving.png"; Alert ("2"); } </Script> <Body> <!--Calls into the JavaScript interface for the activity - <aOnClick= "window.demo.clickOnAndroid ()"><Divstyle= "width:80px; margin:0px Auto; padding:10px; Text-align:center; border:2px solid #202020; " > <imgID= "Droid"src= "Android_normal.png"/><BR>Click me! </Div></a> </Body></HTML>
Main.xml
<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent" > <TextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "@string/intro"android:padding= "4dip"android:textsize= "16SP" /> <WebViewAndroid:id= "@+id/webview"Android:layout_width= "Fill_parent"Android:layout_height= "0dip"Android:layout_weight= "1" /> </LinearLayout>
4, how to interact?
①android how to invoke JS.
Invocation form:
Mwebview.loadurl ("Javascript:wave ()");
where Wave () is a method of JS, of course, you can change this method to other methods, that is, Android calls other methods.
②js how to call Android.
Invocation form:
<onClick= "window.demo.clickOnAndroid ()">
The "demo" in the code is the name of the call specified in Android,
Mwebview.addjavascriptinterface (new demojavascriptinterface (), "demo");
The Clickonandroid () in the code is a method in the "demo" corresponding to the object: New Demojavascriptinterface ().
③ bidirectional Interaction.
Of course, the first two ways to combine it.
5, explain the demo.
Now you must understand the interaction of Android and JS. It is time to analyze some demos, according to the above you should also be more clear. The specific interaction flow is as follows:
① Click on the image, the JS end of the direct call on the Android Method Clickonandroid ();
The ②clickonandroid () method invokes the JS method (using the thread).
③ is directly controlled HTML by the JS called by ②.
Personal Summary: The use of webview in this way in some cases, the UI layout can be translated into the corresponding HTML code, and HTML layout style such as a powerful tool such as DW, and many online source code, a lot of coding. It saves a lot of time on the UI and visual effects, and it doesn't make sense to reinvent the wheel.
Welcome all Friends Message exchange.
(Transferred from: http://www.cnblogs.com/vanezkw/archive/2012/07/02/2572799.html)
WebView and JS Interaction