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");

}

更多相关文章

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

随机推荐

  1. android 跳转时出现短暂的黑屏继承AppCom
  2. Android(安卓)Studio 1.2正式版发布,要如
  3. Android(安卓)开发中的日常积累
  4. Android(安卓)面试题总结之View(一)
  5. Android强制弹出,隐藏输入法.
  6. Android(安卓)string-array数据源简单使
  7. android微信支付源码分享
  8. Android游戏开发系统控件-Dialog
  9. TabLayout的基本使用
  10. 统一样式的View应该用style修饰