1. Write HTML and Android layouts
<Button Android:id="@+id/btn"Android:layout_width="wrap_content"Android:layout_height="wrap_content"/> <WebView Android:id="@+id/webview"Android:layout_width="match_parent"Android:layout_height="wrap_content"> </WebView>
<!doctype html>
2. Writing activity
Private WebView WebView; Private Webappinterface Webappinterface;
classwebappinterface{PrivateContext Mcontext; PublicWebappinterface (Context context) { This. mcontext=context; } @JavascriptInterface Public voidSayHello (String name) {Toast.maketext (Mcontext,name,toast.length_short). Show (); } Public voidshowname (final String name) {Runonuithread (NewRunnable () {@Override Public voidrun () {Webview.loadurl ("javascript:showname ('"+name+"')"); } }); } }webview=Findviewbyid (R.id.webview); String URL="Http://a.st"; Webview.loadurl (URL); Webview.getsettings (). setjavascriptenabled (true); Webappinterface=NewWebappinterface ( This); Webview.addjavascriptinterface (Webappinterface,"app"); Button Button=Findviewbyid (R.ID.BTN); Button.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Webappinterface.showname ("Wirhiutrhi"); } });To look at the setting configuration of the WebView control to better set
Can be implemented to call each other, can do hybird pull
Mutual invocation of Android and HTML