Android本地调用JS
webView.loadUrl("javascript:showMsg()");
JS调用Android本地代码
Android本地代码
webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new MyJSObject(), "MyJSObject");
class MyJSObject{ /** * web页面js调用本地方法 * * void */ @JavascriptInterface public void cameraShow(){ Toast.makeText(MainActivity.this, "JS调用本地代码成功", Toast.LENGTH_SHORT).show(); } }
html代码
<%@page language="java" contentType="text/html; charset=utf8"%> <!DOCTYPE HTML> <html> <head> </head> <script type="text/javascript"> function cameraShow(){ window.MyJSObject.cameraShow(); }
function showMsg(){
document.getElementById("cxt").value="android...";
}
</script>
<body> This is my JSP page. <br> <button type="button" onclick="cameraShow()">播放</button> </body> </html>
时间: 2024-10-27 07:17:25