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