总结:
Android 调用 js:
在 Android 中创建通往 javascript 的接口;
在 html 中定义要执行的方法;
在 Android 中的具体事件中进行调用。
contentWebView.loadUrl("javascript:javacalljs()");
js中调用Android 方法 :
html中调用 Android 方法则反来,在 Andorid 中定义要调用的方法, html 中绑定事件进行调用。
点击调用java 代码
Android 展示 html 页面
(1)project 视图下,在 Android 工程中新建目录 assets;
![a8270f3cdb8ae882ff138e89890e96e3.png](https://img-blog.csdnimg.cn/img_convert/a8270f3cdb8ae882ff138e89890e96e3.png)
image
(2)在 assets 目录下新建 html 页面 如 show.html;
(3)Android 界面中在 WebView 中展示 show.html;
(4)Android 原生按钮点击执行 html 中的 js 方法;
![ab5ed228d26cd2483e324ef4553efcec.png](https://img-blog.csdnimg.cn/img_convert/ab5ed228d26cd2483e324ef4553efcec.png)
image
界面设计:
![4e7a2f54099f3f07b01bec7d820e4859.png](https://img-blog.csdnimg.cn/img_convert/4e7a2f54099f3f07b01bec7d820e4859.png)
image
html 中的 js:
![f19604bd716bbe7a751294607e97a4b8.png](https://img-blog.csdnimg.cn/img_convert/f19604bd716bbe7a751294607e97a4b8.png)
image
效果:
![fccf74eaaffee138ac4d8ed12ed89798.png](https://img-blog.csdnimg.cn/img_convert/fccf74eaaffee138ac4d8ed12ed89798.png)
image
js 调用 android 方法
还是需要一个接口:
![466f1963cdb90ca6cd42781b5d146bb0.png](https://img-blog.csdnimg.cn/img_convert/466f1963cdb90ca6cd42781b5d146bb0.png)
image
Andorid中定义相应的执行方法(注意带上相应的注解):
![60d47054c4ddb27345bb8dfca22edd2c.png](https://img-blog.csdnimg.cn/img_convert/60d47054c4ddb27345bb8dfca22edd2c.png)
image
html 中进行调用:
![013604342d3e44138dbeb4aa113c558b.png](https://img-blog.csdnimg.cn/img_convert/013604342d3e44138dbeb4aa113c558b.png)
image
效果(弹的吐司没截下来):
![031fa1c2645edb76603c6393ca1eee00.png](https://img-blog.csdnimg.cn/img_convert/031fa1c2645edb76603c6393ca1eee00.png)