Public classJavaandjsactivityextendsActivityImplementsView.onclicklistener {PrivateEditText Etnumber; PrivateEditText Etpassword; PrivateButton Btnlogin; /*** Load Web page or say H5 page*/ PrivateWebView WebView; /*** Find the layout<br/> * <br/> * auto-created on 2016-07-28 11:43:37 by Androi D Layout Finder * (Http://www.buzzingandroid.com/tools/android-layout-finder) */ Private voidfindviews () {Setcontentview (R.LAYOUT.ACTIVITY_JAVA_AND_JS); Etnumber=(EditText) Findviewbyid (R.id.et_number); Etpassword=(EditText) Findviewbyid (R.id.et_password); Btnlogin=(Button) Findviewbyid (R.id.btn_login); Btnlogin.setonclicklistener ( This); Initwebview (); } /*** Handle button click events<br/> * <br/> * auto-created on 2016-07-28 11:43:37 by Android Layout Finder * (Http://www.buzzingandroid.com/tools/android-layout-finder) */@Override Public voidOnClick (View v) {if(v = =Btnlogin) { //Handle clicks for Btnloginlogin (); } } Private voidLogin () {String Numebr=Etnumber.gettext (). toString (). Trim (); String Password=Etpassword.gettext (). toString (). Trim (); if(Textutils.isempty (NUMEBR) | |textutils.isempty (password)) {Toast.maketext (javaandjsactivity). This, "The account or password is empty", Toast.length_short). Show (); } Else { //Loginlogin (NUMEBR); } } /*** Java calls JavaScript * *@paramNumebr*/ Private voidLogin (String numebr) {Webview.loadurl ("Javascript:javacalljs (" + "'" + Numebr + "'" + ")"); Setcontentview (WebView); } Private voidInitwebview () {WebView=NewWebView ( This); WebSettings websettings=webview.getsettings (); //set support JavaScript footstep languageWebsettings.setjavascriptenabled (true); //Double-click Support-provided the page is supported for display//Websettings.setusewideviewport (TRUE); //Support for zoom buttons-provided the page is supported for displayWebsettings.setbuiltinzoomcontrols (true); //set client-do not jump to the default browserWebview.setwebviewclient (Newwebviewclient ()); //Set Support JS call JavaWebview.addjavascriptinterface (NewAndroidandjsinterface (), "Android"); //Load network resources//Webview.loadurl ("http://10.0.2.2: 8080/assets/javaandjavascriptcall.html ");
Load resources under the assets folder
Webview.loadurl ("file:///android_asset/JavaAndJavaScriptCall.html"); //Show Page//Setcontentview (webView);} @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Findviews (); } /*** JS can call the method of this class*/ classandroidandjsinterface{@JavascriptInterface Public voidShowtoast () {Toast.maketext (javaandjsactivity. This, "I was called by JS.", Toast.length_short). Show (); } }}
Javaandjavascriptcall.html
<HTML><Head> <Metahttp-equiv= "Content-type"content= "Text/html;charset=utf-8"> <Scripttype= "Text/javascript"> functionJavacalljs () {document.getElementById ("content"). InnerHTML+= "<br\>java called the JS parameterless function"; } functionJavacalljs (Arg) {document.getElementById ("content"). InnerHTML= ("Welcome:"+Arg); } functionShowDialog () {alert ("Hello, I'm from JavaScript."); } </Script></Head><Body><DivAlign= "Left"ID= "Content">555</Div><DivAlign= "Right">To</Div><P><imgsrc= "Http://atguigu.com/images/logo.gif"></P><inputtype= "button"value= "Click on Android to be called"onclick= "window. Android.showtoast () " /></Body></HTML>
Java and JS Intermodulation WebView