如同上面的发的图一样,无非是网页上面添加js的回调方法,然后webView通过js接口来实现。
举例说明的话,首先在html页面中加入回调方法,比如:
$(function () {
$("a.wiki").click(function (e) {
event.preventDefault();
var context=$(this).text();
window.jump.adc(0,context);
});
});
加入一个jquery的点击监听事件,在里面写下上面的代码,然后执行
window.jump.adc(0,context);这个方法就是在WebView里面需要调用的回调。
再调用WebiView的位置添加如下的方法
web_content.addJavascriptInterface(new WebEvent(this), "jump");
其中的“jump”就是html中window.jump的jump
然后再看WebEvent里面做了什么
@JavascriptInterface
public void adc(int type,String args){
String type_txt;
Intent it=new Intent(context,ADCListActivity.class);
if(type==0){
type_txt="city like ?";
args="%"+args+"%";
}else{
type_txt="type = ?";
}
it.putExtra("type",type_txt);
it.putExtra("args",args);
context.startActivity(it);
}
这里面写了一个方法,用作跳转。实现了这三个后会发生什么呢?
你在html的点击事件发生后便会跳转到指定的Activity了,这样就完成了Android程序对html界面的事件监听了。