Android detects <img tags in rich text and achieves click Effects

Source: Internet
Author: User

This article is intended to: by clicking on a picture toast output location and URL link.

Gossip Less, the implementation of the principle is probably Jiangzi:: Through the regular expression detection of the rich text within the collection of pictures merged to get the URL, after the src= "xxx" Add the OnClick method, as for JS how to load into oneself is stitching up the context of an HTML tag

JS Call Java Method Please do your own search is not covered in this article.

 Public classHtmlutils {/** * Get all pictures in HTML * @param compattext * @return*/     Public StaticList<string>filterimages (String compattext) {List<String> uList =NewArraylist<>(); if(! Textutils.isempty (Compattext) &&compattext.contains ("")){            //get img srcPattern p = pattern.compile ("]+src\\s*=\\s*[' \ '] ([^ ' \ ']+) [' \ ']][^>]*>");//]*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)); }            }        }        returnuList; }    /** 1. Insert the execution function into the rich text SayHello * 2. The modified text is inserted */     Public Staticstring Constructexecactionjs (String compattext) {StringBuffer sb=NewStringBuffer (); Sb.append (""+"<script type=\ "text/javascript\" >"+"function showImage (position, url) {\ n"+"window.control.showImage (position,url) \ n"+"    }"+"</script>"); //Insert Functionsb.append (Insertexecactionjs (Compattext)); Sb.append (""); returnsb.tostring (); }     Public Staticstring Insertexecactionjs (String compattext) {string Searchattrib="src"; String Regxpfortag="]+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=NewStringBuffer (); Boolean result=Matcherfortag.find (); intpos =0 ;  while(Result) {StringBuffer sbreplace=NewStringBuffer (); 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 ()); returnsb.tostring (); }}

The code in activity is as follows:

Mwebview.getsettings (). Setdefaulttextencodingname ("Utf-8");        Mwebview.getsettings (). Setjavascriptenabled (true);        Mwebview.addjavascriptinterface (New Jsinteration (), "control");

  

 Public class jsinteration {        @JavascriptInterface        publicvoid showImage (int  Position, String URL) {            toast.maketext (Getapplicationcontext (), Position+ ":" +URL, toast.length_long ). Show ();        }    }

Call:

Webviewutils.setcontenttowebview (Mwebview,htmlutils.constructexecactionjs (Blogdetail.getblogcontent ()));

Android detects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.