总结:

Android 调用  js:

在 Android 中创建通往 javascript 的接口;

在 html  中定义要执行的方法;

在  Android  中的具体事件中进行调用。

contentWebView.loadUrl("javascript:javacalljs()");



js中调用Android 方法  :

html中调用 Android 方法则反来,在 Andorid 中定义要调用的方法, html 中绑定事件进行调用。
<button onclick="window.wjj.startFunction()">点击调用java 代码</button>


Android 展示 html 页面

 (1)project 视图下,在 Android 工程中新建目录 assets;

Android 与 js 简单互调

(2)在 assets 目录下新建 html 页面 如  show.html;

(3)Android 界面中在 WebView 中展示   show.html;

(4)Android 原生按钮点击执行 html 中的 js 方法;

Android 与 js 简单互调

 

界面设计:

Android 与 js 简单互调

 html 中的 js:

Android 与 js 简单互调

 

效果:

Android 与 js 简单互调

 


 

js 调用 android 方法

还是需要一个接口:

Android 与 js 简单互调

 

Andorid中定义相应的执行方法(注意带上相应的注解):

Android 与 js 简单互调

 

 html 中进行调用:

Android 与 js 简单互调

 

效果(弹的吐司没截下来):

Android 与 js 简单互调

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2021-09-25
  • 2021-05-22
  • 2022-12-23
  • 2021-11-22
  • 2021-07-21
猜你喜欢
  • 2022-12-23
  • 2021-05-23
  • 2021-12-14
  • 2022-12-23
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案