Android interacts with Javascrip (i)

Source: Internet
Author: User

First, the JS code is as follows

Demo2.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE HTML PUBLIC "-//wapforum//dtd XHTML Mobile 1.0//en" "Http://www.wapforum.org/DTD/xhtml-mobile10.dtd" > < HTML xmlns= "http://www.w3.org/1999/xhtml" > 
<pre name= "code" class= "HTML" ><span style= "White-space:pre" ></span>onclick= " Window.demo.clickButton () "<span style=" font-family:arial, Helvetica, Sans-serif; " ></span>
Onclick= "Test2 ()"/><input onclick= "test ()" type= "button" id= "testbtn2" value= "I am button 2"/></body>< Script language= "javascript" >function Test () {Alert ("click button 2");} function Test2 () {Alert ("click Button 1");} Function Wave () {alert ("clicked Button in Andorid");} </script>

The above button 1 sets the method of 2 Click events, one is the method in Android, one is the method in JS, but the Click event will only respond once.

Here's how to put the methods on Android in front of you. Open this HTML file in the browser, click on button 1, respond to the event from the previous search, placed in front of the method in the Andoid, the browser is not found in nature, so will not respond, continue to look backward, the second click event is JS method, can be executed, so to respond. Running in the app, the first method found is the executable method, so it responds, the subsequent Click event method can be executed, but because the click event has already responded, it is no longer executed.

Second, realize the function

1. Click on the button in the Andorid to respond to the JS method

2. Click the JS button in response to the Android method

Main.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical" >    <button        android:id= "@+id/mcalljs"        android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "Calljs"/>    <webview        android:id= "@+id/ WebView "        android:layout_width=" fill_parent "        android:layout_height=" Fill_parent "/></ Linearlayout>

Methodmutual2.java

Package Com.example.mutal;import Android.app.activity;import Android.app.alertdialog;import Android.content.dialoginterface;import Android.os.bundle;import Android.os.handler;import Android.util.Log;import Android.view.view;import Android.view.view.onclicklistener;import Android.webkit.jsresult;import Android.webkit.webchromeclient;import Android.webkit.websettings;import Android.webkit.webview;import Android.widget.button;import Android.widget.toast;public class MethodMutual2 extends Activity {private WebView Mwebview;private static final String Log_tag = "Webviewdemo";/** called when the activity is first created. */@Overridepublic void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); loadassethtml ();} public void loadassethtml () {Mwebview = (WebView) Findviewbyid (R.id.webview); WebSettings websettings = Mwebview.getsettings (); Websettings.setsavepassword (false); Websettings.setsaveformdata ( FALSE); websettings.setjavascriptenabled (True); Websettings.setsupportzoom (false); Mwebview.setwebchromeclient (new Mywebchromeclient ());// Binds a Java object to a JavaScript object, the JavaScript object name is InterfaceName, and the scope is Global.mWebView.addJavascriptInterface (new Demojavascriptinterface (), "demo"); Mwebview.loadurl ("file:///android_asset/demo2.html");// By applying the button click on the Trigger JS function response button Mcalljs = (button) Findviewbyid (R.ID.MCALLJS); Mcalljs.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {mwebview.loadurl ("Javascript:wave ()");});} private int i = 0;final class Demojavascriptinterface {demojavascriptinterface () {}/** * This isn't called on the UI thre Ad. Post a runnable to invoke * Loadurl on the UI thread. */public void Clickbutton () {new Alertdialog.builder (methodmutual2.this). Setmessage ("You clicked Button 2"). Create (). Show ();} /** * Provides a hook for calling ' alert ' from JavaScript. Useful for debugging * your JavaScript. */final class Mywebchromeclient extends Webchromeclient {@Overridepublic Boolean onjsalert (WebView view, String URL, String message, jsresult result) {//own Intercept Dialog processing//Toast.maketext (Getapplicationcontext (), message, 0). Show ();/ Result.confirm ();//return true;//Use JS dialog box return super.onjsalert (view, URL, message, result);}}}
Analysis:

Click on the Android button--respond to The wave () method in JS

Click the button in the 1--android app to respond to the Clickbutton () method, and the Test2 () method in the browser in response to JS

Click button 2-response JS in the test () method

Android interacts with Javascrip (i)

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.