Local WebView notation
WebView = (webview) Findviewbyid (R.id.webview); Webview.loadurl ("http://192.168.1.103/index.html"); Webview.getsettings (). setjavascriptenabled (true// allow JS script to run Webview.setscrollbarstyle (view.scrollbars_inside_overlay); Webview.getsettings (). Setbuiltinzoomcontrols (true); Webview.getsettings (). setjavascriptenabled (true); Webview.addjavascriptinterface (new Jstojava (Mcontext), "Mobject"); // Add a mapping object to JS
JS Code
Body> <a>js Call local method </a> <script> function Funfromjs () { document.getElementById ("Helloweb"). Innerhtml= "Hellowebview,i ' m from JS"; } = document.getElementsByTagName (' a ') [0]; Atag.addeventlistener (' click ', Function () { // call Android Local method Myobj.fun1fromandroid ("Call Android native method Fun1fromandroid (String name)!! "); return false ; false ); </script> <p></p> <div id= "Helloweb" > </div></body>
Local and JS mapped objects:
Public class javascriptobject { Context mcontxt; Public Javascriptobject (Context mcontxt) { this. mcontxt = mcontxt; } Public void fun1fromandroid (String name) { Toast.maketext (mcontxt, Name, Toast.length_long). Show (); Public void fun2 (String name) { "call fun2:" + name, Toast.length_short). Show (); }}
JS and Android interaction