美文网首页
在Android中使用Html5

在Android中使用Html5

作者: 柯翰辰K | 来源:发表于2016-09-27 13:14 被阅读650次

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

      本文链接:https://www.haomeiwen.com/subject/tabwlttx.html