Java and JS Intermodulation WebView

Source: Internet
Author: User
Tags gettext

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.