<body>
<!-- Calls into the javascript interface for the activity -->
<!-- 从HTML文件中调用activity中的函数 -->
<!-- 也就是从HTML到android程序 留意window.demo.clickOnAndroid()这句话 -->
<a onClick="window.demo.clickOnAndroid()">Click</a>
</body>
mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo")
final class DemoJavaScriptInterface {
DemoJavaScriptInterface() {
}
/**
* This is not called on the UI thread. Post a runnable to invoke
*
* loadUrl on the UI thread.
*
*/
public void clickOnAndroid() { // 注意这里的名称。它为clickOnAndroid(),注意,注意,严重注意
mHandler.post(new Runnable() {
public void run() {
Toast.makeText....
}
})
}
}
1、初始化WebView
X5WebView webView =new X5WebView(this,null)我这里用的是腾讯的TBS浏览服务.详情参考上一篇文章: https://www.jianshu.com/writer#/notebooks/10278901/notes/42868406
2、设置初始化WebSetting
3、给WebView添加addJavascriptInterface()
例如: