标签:
/** 要注入的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);};}})()"); }
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");
webview.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); injectImgClick(); } });
标签:
原文地址:http://blog.csdn.net/android_love/article/details/45665893