在Android中使用Html5

1.在Android中和和H5的的交互:  Android中H5开发    Android和js交互

开发中存在native和H5界面之间的交互问题,例如点击h5的界面弹出native组件,如toast。

使用传统的JSinterface通信: 

Android界面:acticity加载webview,支持javascript

    WebSettings webSettings = webView.getSettings();

    webSettings.setJavaScriptEnabled(true);

然后通过WebView的addJavascriptInterface方法去注入一个我们自己写的interface

        webView.addJavascriptInterface(newJsInterface(),"control");

        webView.loadUrl("file:///android_asset/interact.html");


html代码中js的代码:

functionshowToast(toast){

javascript:control.showToast(toast);

}

这里的control就是我们的那个interface,调用了interface的showToast方法

我们自己写的interface

publicclassJsInterface{

@JavascriptInterface

publicvoidshowToast(String toast){

Toast.makeText(MainActivity.this, toast, Toast.LENGTH_SHORT).show();

log("show toast success");

}

你可能感兴趣的:(在Android中使用Html5)