First, the Thinking analysis
After testing found that the Click event in JS can only write one, if more than one write, will only respond to the first, if the method of writing is the method of the Android side, when running on the web side, the background will be reported undefined this method error, front click No response.
So, if you want a button on the web to respond to the web side of the JS method, and the Android side of the response is the Android side of the method, you need to manually inject the page after the completion of the JS function to listen.
Second, the Code
Demo3.html
This section of code is placed in the assets folder
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE HTML PUBLIC "-//wapforum//dtd XHTML Mobile 1.0//en" "Http://www.wapforum.org/DTD/xhtml-mobile10.dtd" > < HTML xmlns= "http://www.w3.org/1999/xhtml" >
The interface is displayed as follows
Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" fill_parent " android:o rientation= "vertical" > <webview android:id= "@+id/webview" android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:layout_weight= "9"/></linearlayout>
Mainactivity.javaPackage Wst.webview;import Android.annotation.suppresslint;import Android.app.activity;import Android.content.context;import Android.content.intent;import Android.graphics.bitmap;import Android.os.Bundle; Import Android.webkit.webview;import android.webkit.WebViewClient; @SuppressLint ("setjavascriptenabled") public Class Mainactivity extends Activity {private WebView contentwebview = null; @SuppressLint ("setjavascriptenabled") @ overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); Contentwebview = (WebView) Findviewbyid (R.id.webview);//Enable javascriptcontentwebview.getsettings (). Setjavascriptenabled (TRUE);//load local HTML file, content as above Contentwebview.loadurl ("file:///android_asset/demo3.html");// Add the JS interface class and Alias Imagelistnercontentwebview.addjavascriptinterface (new Javascriptinterface (This), "Imagelistner"); Contentwebview.setwebviewclient (New Mywebviewclient ());} Injected JS function monitoring <span style= "White-space:pre" ></span>//below provides three kinds of JS injection functions, you canSelect the appropriate JS function according to the requirements private void Addimageclicklistner () {//This function of the JS function is to traverse all the IMG points, and add the onclick function, Call the local interface to pass the URL when still executing Contentwebview.loadurl ("javascript: (function () {" + "var objs = document.getElementsByTagName (\ "Img\"); "+" for (Var i=0;i<objs.length;i++) "+" {"+" objs[i].onclick=function () "+" {"+" Window.imagelis Tner.openimage (THIS.SRC); "+"} "+"} "+"});//The function of this JS function is to capture the code in JS, on which the ID is testbtn button, re-add the Click event Contentwebview.loadurl ("javascript: (Function ( {"+" var obj = document.getElementById (\ "testbtn\"); "+" obj.onclick=function () "+" {"+" window.imagelistn Er.clickbutton (); "+"} "+"});//The function of this JS function is to traverse all input points, add click event Contentwebview.loadurl ("javascript: (function () {" + "var Objs = Docum Ent.getelementsbytagname (\ "input\"); "+" for (Var i=0;i<objs.length;i++) "+" {"+" objs[i].onclick=function () "+" {"+" Window.imagelis Tner.clickbutton (); " + " } " + "}" + "})()");} JS Communication interface public class Javascriptinterface {Private context Context;public Javascriptinterface (context context) {This.context = context;} public void Openimage (String img) {System.out.println (img);//intent Intent = new Intent (); Intent.putextra ("Image", IMG) ; Intent.setclass (context, showwebimageactivity.class); context.startactivity (intent); System.out.println (IMG);} public void Clickbutton () {Intent Intent = new Intent (context,secondactivity.class); startactivity (Intent);}} Monitor private class Mywebviewclient extends Webviewclient {@Overridepublic Boolean shouldoverrideurlloading (WebView view , String URL) {return super.shouldoverrideurlloading (view, URL);} @Overridepublic void onpagefinished (WebView view, String URL) {view.getsettings (). Setjavascriptenabled (True); super.onpagefinished (view, URL);//After the HTML loading is complete, add the Click JS function of the Listener image Addimageclicklistner ();} @Overridepublic void onpagestarted (WebView view, String URL, Bitmap favicon) {view.getsettings (). setjavascriptenabled ( true); super.onpagestarted (view, URL, favicon);} @Overridepublic void onreceivedError (WebView view, int errorCode, string description, String failingurl) {super.onreceivederror (view, ErrorCode, Description, Failingurl);}}}
Third, the effect analysisClick on the image, in response to the Android-injected JS function monitoring, but the web-side response is still the original monitoring. The same is true for buttons.
Android interacts with Javascrip (ii)