Android WebView interaction with Java and JavaScript

Source: Internet
Author: User


Webview.getsettings (). setjavascriptenabled (true); You can use the JS function when you open it.

 

Webview.getsettings (). setjavascriptenabled (true); You can use the JS function when you open it.


JS code, placed in the asset folder, can also be on the server side, so change the content, just change the server side, the client does not have to modify

 

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<script type="Text/javascript">

function Btn_test () {

/* print, the tag in the Logcat is the Web Console */

Console.log ("Btn_test");

}

function Call_java () {

/* JS call Java code, need interface */

Window.interface_test.showToastFromWeb ("Hello I Am javascript!");

}

function Test_alert () {

/* Popup dialog box */

Alert ("Hello I Am javascript!");

}

function Test_confirm () {

/* Popup dialog box with OK and cancel button */

var v = confirm ("Hello I Am javascript!");

Console.log ("onfirm" + V);

}

function Test_prompt () {

/* Popup dialog box with input box */

var v = prompt ("Input you name!", "Tom");

Console.log ("prompt" + V);

}

</Script>

</head>

<body>

<button onclick="btn_test ()">test</button>

<button onclick="Call_java ()">call_java</button>

<button onclick="Test_alert ()">test_alert</button>

<button onclick="test_confirm ()">test_confirm</button>

<button onclick="test_prompt ()">test_prompt</button>

<label tel:800800284>tel:8008233</label>

</body>

</html>

 

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">

<title>insert title here</title>

<script type="Text/javascript">

function Btn_test () {

/* print, the tag in the Logcat is the Web Console */

Console.log ("Btn_test");

}

function Call_java () {

/* JS call Java code, need interface */

Window.interface_test.showToastFromWeb ("Hello I Am javascript!");

}

function Test_alert () {

/* Popup dialog box */

Alert ("Hello I Am javascript!");

}

function Test_confirm () {

/* Popup dialog box with OK and cancel button */

var v = confirm ("Hello I Am javascript!");

Console.log ("onfirm" + V);

}

function Test_prompt () {

/* Popup dialog box with input box */

var v = prompt ("Input you name!", "Tom");

Console.log ("prompt" + V);

}

</Script>

</head>

<body>

<button onclick="btn_test ()">test</button>

<button onclick="Call_java ()">call_java</button>

<button onclick="Test_alert ()">test_alert</button>

<button onclick="test_confirm ()">test_confirm</button>

<button onclick="test_prompt ()">test_prompt</button>

<label tel:800800284>tel:8008233</label>

</body>

</html>

In the Java code

Webview.addjavascriptinterface (new Object () {

Public void Showtoastfromweb (String msg) {

Showtoast (msg);

}

}, "Interface_test");

 

Webview.addjavascriptinterface (new Object () {

Public void Showtoastfromweb (String msg) {

Showtoast (msg);

}

}, "Interface_test");

Webview.setwebchromeclient (new Webchromeclient () {

@Override

Public void onprogresschanged (WebView view, int newprogress) {

This last post has been mentioned.

}

@Override

Public Boolean Onjsalert (WebView view, string URL, string message,

Jsresult result) {

TODO auto-generated Method Stub

return super.onjsalert (view, URL, message, result);

}

@Override

Public Boolean onjsconfirm (WebView view, String URL,

String message, jsresult result) {

TODO auto-generated Method Stub

return super.onjsconfirm (view, URL, message, result);

}

@Override

Public Boolean onjsprompt (WebView view, string URL, string message,

String DefaultValue, jspromptresult result) {

TODO auto-generated Method Stub

return super.onjsprompt (view, URL, message, defaultvalue, result);

}

});

Webview.setwebchromeclient (new Webchromeclient () {

@Override

Public void onprogresschanged (WebView view, int newprogress) {

This last post has been mentioned.

}

@Override

Public Boolean Onjsalert (WebView view, string URL, string message,

Jsresult result) {

TODO auto-generated Method Stub

return super.onjsalert (view, URL, message, result);

}

@Override

Public Boolean onjsconfirm (WebView view, String URL,

String message, jsresult result) {

TODO auto-generated Method Stub

return super.onjsconfirm (view, URL, message, result);

}

@Override

Public Boolean onjsprompt (WebView view, string URL, string message,

String DefaultValue, jspromptresult result) {

TODO auto-generated Method Stub

return super.onjsprompt (view, URL, message, defaultvalue, result);

}

});

Webview.loadurl ("Javascript:btn_test ()");

Webview.loadurl ("file:///android_asset/test.html");

Webview.loadurl ("Javascript:btn_test ()");

Webview.loadurl ("file:///android_asset/test.html");

Android WebView interaction with Java and JavaScript

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.