Activity code:
Package Com.example.reg;import Android.app.activity;import Android.os.bundle;import android.util.log;import Android.webkit.webview;import Com.example.reg.bean.myobject;public class Webviewactivity extends Activity{private Static final String tag = WebViewActivity.class.getSimpleName (); @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.webview); WebView WebView = (WebView) Findviewbyid (R.id.web_view); Webview.getsettings (). Setjavascriptenabled (true);// Webview.loadurl ("http://www.baidu.com"); Webview.addjavascriptinterface (new MyObject (This), "MYOBJ"); Webview.loadurl ("file:///android_asset/html/test.html"); LOG.D (tag, "Execution end:");}}
MyObject Code:
Package Com.example.reg.bean;import Android. R;import android.app.alertdialog;import Android.content.context;import Android.widget.toast;public class MyObject { Context Mcontext;public MyObject (context Mcontext) {super (); this.mcontext = Mcontext;} public void Showtoast (String name) {toast.maketext (Mcontext, Name, Toast.length_long). Show (); public void Showlist () {new Alertdialog.builder (Mcontext). Settitle ("book list"). SetIcon (R.drawable.ic_btn_speak_now). Setitems (New string[]{"xx1", "xx2", "xx3"}, NULL). Setpositivebutton ("OK", null). Create (). Show ();}
HTML path:
\reg\assets\html\test.html \reg is the project name
HTML code:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >< Html>
If you want to introduce an external CSS file in a local page, add the following code:
<!--introducing CSS files--
<link rel= "stylesheet" href= "File:///android_asset/html/style.css" type= "Text/css"/>
Android uses local page to invoke Android code