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