Android检测富文本中的<img标签并实现点击效果
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android检测富文本中的<img标签并实现点击效果,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3416字,纯文字阅读大概需要5分钟。
内容图文
本文旨在:通过点击一张图片Toast输出位置与url链接。
闲话少说,实现原理大概是酱紫的::通过正则表达式检测富文本内的图片集合并获取url,在src=“xxx” 后面添加 onclick方法,至于js如何load进去本人是自己拼接了一个html标签的上下文
js调用java方法请自行搜索不在本文讨论范围。
public class HtmlUtils { /* * * 获取html中的所有图片 * @param compatText * @return */ public static List<String> filterImages(String compatText){ List<String> uList = new ArrayList<>(); if(!TextUtils.isEmpty(compatText)&&compatText.contains("<img")){ //get img src Pattern p = Pattern.compile("<img[^>]+src\\s*=\\s*[‘\"]([^‘\"]+)[‘\"][^>]*>");//<img[^<>]*src=[\‘\"]([0-9A-Za-z.\\/]*)[\‘\"].(.*?)>"); Matcher m = p.matcher(compatText); String searchAttrib = "src"; String regxpForTagAttrib = searchAttrib + "\\s*=\\s*[\"|‘]http://([^\"|‘]+)[\"|‘]";//"=[\"|‘]([^[\"|‘]]+)[\"|‘]"; Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib); while(m.find()){ Matcher matcherForAttrib = patternForAttrib.matcher(m.group()); if (matcherForAttrib.find()) { System.out.println("poe " +"http://" +matcherForAttrib.group(1)); uList.add("http://" +matcherForAttrib.group(1)); } } } return uList; } /** * 1.向富文本中插入执行函数sayHello * 2.修改后的文本插入<html></html> 组合为新的页面 . * @param compatText */publicstatic String constructExecActionJs(String compatText){ StringBuffer sb = new StringBuffer(); sb.append("<html> " + "<script type=\"text/javascript\"> " + " function showImage(position , url) {\n" + " window.control.showImage(position,url)\n" + " }"+ "</script>"); //插入函数 sb.append(insertExecActionJs(compatText)); sb.append("</html>"); return sb.toString(); } publicstatic String insertExecActionJs(String compatText){ String searchAttrib = "src"; String regxpForTag ="<img[^>]+src\\s*=\\s*[‘\"]([^‘\"]+)[‘\"][^>]*>"; Pattern patternForTag = Pattern.compile(regxpForTag); String regxpForTagAttrib = searchAttrib + "\\s*=\\s*[\"|‘]http://([^\"|‘]+)[\"|‘]";//"=[\"|‘]([^[\"|‘]]+)[\"|‘]"; Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib); Matcher matcherForTag = patternForTag.matcher(compatText); StringBuffer sb = new StringBuffer(); boolean result = matcherForTag.find(); int pos = 0 ; while (result) { StringBuffer sbreplace = new StringBuffer(); System.out.println(matcherForTag.group()); Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag.group()); if (matcherForAttrib.find()) { System.out.println("ll"+matcherForAttrib.group()); matcherForAttrib.appendReplacement(sbreplace, searchAttrib+"=\"http://" + matcherForAttrib.group(1) +"\""+ " onclick=\"showImage(" + +pos+"," + "‘http://" +matcherForAttrib.group(1) +"‘" +")\""); } matcherForAttrib.appendTail(sbreplace); matcherForTag.appendReplacement(sb, sbreplace.toString()); result = matcherForTag.find(); pos++; } matcherForTag.appendTail(sb); System.out.println("poe: "+sb.toString()); return sb.toString(); } }
Activity中代码如下:
mWebView.getSettings().setDefaultTextEncodingName("utf-8"); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.addJavascriptInterface(new JsInteration(),"control");
public class JsInteration { @JavascriptInterface public void showImage(int position ,String url) { Toast.makeText(getApplicationContext(), position+":"+url, Toast.LENGTH_LONG).show(); } }
调用:
WebViewUtils.setContentToWebView(mWebView,HtmlUtils.constructExecActionJs(blogDetail.getBlogContent()));
标签并实现点击效果' ref='nofollow'>Android检测富文本中的标签并实现点击效果
原文:http://www.cnblogs.com/poe-blog/p/5544929.html
内容总结
以上是互联网集市为您收集整理的Android检测富文本中的<img标签并实现点击效果全部内容,希望文章能够帮你解决Android检测富文本中的<img标签并实现点击效果所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。