本文介绍: <WebView android:id=”@+id/webview” android:layout_width=”fill_parent” android:layout_height=”wrap_content” />初始化,这里读的是文本 @SuppressLint(“JavascriptInt…
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
@SuppressLint("JavascriptInterface")
private void initWebView(String html) {
// 启用javascript
contentWebView.getSettings().setJavaScriptEnabled(true);
contentWebView.getSettings().setDefaultTextEncodingName("UTF-8");
// // 随便找了个带图片的网站
// contentWebView.loadUrl("https://www.pmcaff.com/discuss/3394893931276352?newwindow=1");
contentWebView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);//加载html数据
// 添加js交互接口类,并起别名 imagelistner
// contentWebView.addJavascriptInterface(new JavascriptInterface(this), "imagelistner");
contentWebView.addJavascriptInterface(new JavascriptInterface2(this), "imageListener");
contentWebView.setWebViewClient(new MyWebViewClient());
}
// 监听
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
view.getSettings().setJavaScriptEnabled(true);
addImageClickListner2();
super.onPageFinished(view, url);
// html加载完成之后,添加监听图片的点击js函数
// addImageClickListner();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
view.getSettings().setJavaScriptEnabled(true);
super.onPageStarted(view, url, favicon);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
// 注入js函数监听
private void addImageClickListner2() {
//网页加载完成 走JS代码
contentWebView.loadUrl("javascript:(function(){" +
"var objs = document.getElementsByTagName("img"); " +
"var imgUrl = "";"+
"var filter = ["img//EventHead.png","img//kong.png","hdtz//button.png"];"+
"var isShow = true;"+
"for(var i=0;i<objs.length;i++){" +
"for(var j=0;j<filter.length;j++){"+
"if(objs[i].src.indexOf(filter[j])>=0) {"+
"isShow = false; break;}}"+
"if(isShow && objs[i].width>1){"+
"imgUrl += objs[i].src + ',';isShow = true;"+
" objs[i].οnclick=function() " +
" { "
+ " window.imageListener.openImage(imgUrl,this.src);" +
" }" +
"}" +
"}" +
"})()"
);
}
// js通信接口
public class JavascriptInterface2 {
private Context context;
public JavascriptInterface2(Context context) {
this.context = context;
}
@android.webkit.JavascriptInterface
public void openImage(String imageUrl,String img) {
int position = 0;
String[] imgs = imageUrl.split(",");
ArrayList<String> imgUrlList = new ArrayList<>();
for (String s : imgs) {
imgUrlList.add(s);
}
for (int i = 0;i<imgUrlList.size();i++){
if (img.equals(imgUrlList.get(i))){
position = i;
}
}
ImageHandlerUtil.browseImages(getContext(), imgUrlList, position);
// Intent intent = new Intent(MainActivity.this, ImageActivity.class);
// intent.putExtra("number",position);
// intent.putExtra("list",(Serializable) imgUrlList);
// startActivity(intent);
}
}
原文地址:https://blog.csdn.net/iromkoear/article/details/124750709
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_7331.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。