在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(安卓)源码查看
- 关于android:configChanges="keyboardHidden|orienta...
- Android的移动存储解决方案之SharedPreferences
- Android(安卓)Duplicate files copied in APK
- 设置Activity全屏与不全屏
- 向模拟器发短信打电话的方法
- Android(安卓)更新UI的两个方法
- Android完整弹窗界面
- android读取本地网页