Get WebView Object
Invokes the GetSettings () method of the WebView object to get the WebSettings object
Invokes the setjavascriptenabled () method of the WebSettings object, setting JS available, Parameters: Boolean
In determining whether to support JS, do not use alert (), the default does not work, you can first use document.write () test
Invokes the Addjavascriptinterface (obj, InterfaceName) method of the WebView object, adding the JS interface, Parameters: Object, string Interface name (alias for this object in JS)
Define an internal class Myjavascript
Define a Method Showtoast (), show the toast, API version greater than 17 need to add annotations @javascriptinterface
Java code:
Package Com.tsh.mywebview;
Import Android.annotation.SuppressLint;
Import android.app.Activity;
Import Android.app.ProgressDialog;
Import Android.graphics.Bitmap;
Import Android.os.Bundle;
Import android.view.KeyEvent;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.Window;
Import Android.webkit.JavascriptInterface;
Import android.webkit.WebSettings;
Import Android.webkit.WebView;
Import android.webkit.WebViewClient;
Import Android.widget.Toast; public class Mainactivity extends activity {private WebView webview; private progressdialog PD; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_
TITLE);
Setcontentview (R.layout.activity_main);
Pd=new ProgressDialog (this);
Pd.setmessage ("Loading ...");
The simple setting of WebView webview= (WebView) Findviewbyid (r.id.wv_internet);
http://100.65.187.106/test.php Webview.loadurl ("http://100.65.187.106/test.php"); WebSettings websettings=webview.geTsettings ();
Websettings.setsupportzoom (TRUE);
Websettings.setbuiltinzoomcontrols (TRUE);
JS Interaction New Myjavascript (). Showtoast ("111");
Websettings.setjavascriptenabled (TRUE);
Webview.addjavascriptinterface (New Myjavascript (), "Android");
Webview.loadurl ("Javascript:documentwrite (' Test ')"); Webview.setwebviewclient (New Webviewclient () {@Override public void onpagestarted (webview view, String URL, Bitmap
Favicon) {pd.show ()} @Override public void onpagefinished (webview view, String URL) {Pd.dismiss ();}}); The functional interface to JS is public class myjavascript{//Show toast//If Target is greater than or equal to API 17, you need to add the following annotation @JavascriptInterface public void Showtoa St (String text) {Toast.maketext (mainactivity.this, text, 1). Show ();//display loading @JavascriptInterface public void SHOWP
Rogressdialog (String text) {pd.setmessage (text); Pd.show ();}} The Back key @Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode==keyevent.keycode_back&& Webview.cangoback ()) {webview.goback (); return true;} return SupeR.onkeydown (KeyCode, event); }//Menu key @Override public boolean oncreateoptionsmenu (0, 0, 0, refresh); menu.add (0, 0, 1, "back"); menu.a
DD (0, 0, 2, "forward");
return Super.oncreateoptionsmenu (menu); //Menu Click event @Override Public boolean onoptionsitemselected (MenuItem item) {switch (Item.getorder ()) {case 0:webview.relo
AD ();
Break
Case 1:if (Webview.cangoback ()) {webview.goback ()};
Case 2:if (Webview.cangoforward ()) {Webview.goforward ()};
return super.onoptionsitemselected (item); }
}
JS Code:
The above content is small to introduce the android in the WebView and JS Interactive implementation method, I hope that the above help!