在webview加载完成后,给所有的img便签加上本地点击事件
代码语言:javascript复制 /** 要注入的js代码
function(){
var objs = document.getElementsByTagName('img');
for(var i = 0; i <objs.length; i ) {
objs[i].onclick = function() {window.toolbox.openImage(i,this.src);
}
};
**/
// 注入js函数监听
public void injectImgClick() {
mWebView.loadUrl("javascript:(function(){var objs = document.getElementsByTagName('img');for(var i = 0; i <objs.length; i ) {objs[i].onclick = function() {window.toolbox.openImage(i,this.src);};}})()");
}
然后
代码语言:javascript复制 webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new Object(){
@JavascriptInterface
public void openImage(int i, String src) {
ArrayList<String> list = new ArrayList<String>();
list.add(src);
mActivity.startActivity(new Intent(mActivity, PhotoViewActivity.class).putExtra(PhotoViewActivity.EXTRA_PHOTOS, list)
.putExtra(PhotoViewActivity.EXTRA_TYPE, PhotoViewActivity.TYPE_VIEW));
}
}, "toolbox");
在页面数据加载完成后,注入脚本代码
代码语言:javascript复制 webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
injectImgClick();
}
});