Android js交互
来源:互联网 发布:易语言表格源码 编辑:程序博客网 时间:2024/06/08 06:58
一、Android调用js
在html中声明方法
function javaCallJs(arg){ document.getElementById("content").innerHTML = ("显示数据:--------"+arg ); }
在java中设置WebSettings settings = webView.getSettings();//支持JSsettings.setJavaScriptEnabled(true);
//java调用JS方法webView.loadUrl("javascript:javaCallJs(haha)");二、js调用Android
在Android中设置方法
private class JSInterface { //JS需要调用的方法 @JavascriptInterface public void showToast(String arg, String arg1) { Toast.makeText(JavaScripActivity.this, arg + arg1, Toast.LENGTH_SHORT).show(); } @JavascriptInterface public void goNext() { goHtmlActivity(); }}添加方法(对象,标识参数)
webView.addJavascriptInterface(new JSInterface(), "Android");
在html中调用方法window.标识参数.方法名
<input type="button" value="跳转下一个界面" onclick="window.Android.goNext()"/>
阅读全文