Android js交互

一、Android调用js

在html中声明方法

function javaCallJs(arg){
        document.getElementById("content").innerHTML =
            ("显示数据:--------"+arg );

   }

在java中设置

WebSettings settings = webView.getSettings();
//支持JS
settings.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()"/>

猜你喜欢

转载自blog.csdn.net/androidwubo/article/details/78901773