HTML format converted to TextView supported spanned

Source: Internet
Author: User
Tags image converter

HTML format converted to TextView supported spanned

spanned spanned = html.fromhtml (Html, Getter, New Messagetaghandler ());

string of html:html

Getter (Imagegetter): Image Converter

 Public Abstract Static Interface Android.text.html$imagegetter {    publicabstract  Android.graphics.drawable.Drawable getdrawable (java.lang.String arg0);}
Imagegetter Imggetter =NewHtml.imagegetter () {@Override Publicdrawable getdrawable (String source) {drawable D=NULL; Try{URL Aryuri=NewURL (source); /*Open Connection*/URLConnection Conn=aryuri.openconnection ();               Conn.connect (); /*Change to InputStream*/InputStream is=Conn.getinputstream (); /*Turn the InputStream into a bitmap*/               //Bitmap BM = Bitmapfactory.decodestream (is);               /*Close InputStream*/                             /*Add a picture*/D=drawable.createfromstream (IS, "111");                  Is.close (); } Catch(IOException e) {e.printstacktrace (); } d.setbounds (1, 1, 45, 45); returnD; }

The passed-in parameter is the Souce property of the image tag, typically a URL address

Messagetaghandler (Taghandler): Special label Handling

 Public Abstract Static Interface Android.text.html$taghandler {  publicvoid Handletag (boolean  opening, String tag, Editable output,            XMLReader XMLReader)}

The process by which an interface is called:

The system parses the HTML text

Call once when tag is found like The parameters passed by the interface are Handletag (true, "HTML", output, XmlReader)

When the tag is found to be closed, the calling interface
The parameters passed by the interface are Handletag (false, "HTML", output, XmlReader)

 Public classMyhtmltaghandlerImplementsTaghandler { Public voidHandletag (Booleanopening, String tag, Editable output, XMLReader XMLReader) {            if(Tag.equalsignorecase ("Strike") | | tag.equals ("s")) {processstrike (opening, output); }        }        Private voidProcessstrike (Booleanopening, Editable output) {            intLen =output.length (); if(opening) {Output.setspan (NewStrikethroughspan (), Len, Len, Spannable.span_mark_mark); } Else{Object obj= GetLast (output, Strikethroughspan.class); intwhere =Output.getspanstart (obj);                Output.removespan (obj); if(Where! =Len) {Output.setspan (NewStrikethroughspan (), where, Len, spannable.span_exclusive_exclusive); }            }        }        PrivateObject getlast (Editable text, Class kind) {object[] Objs= Text.getspans (0, Text.length (), kind); if(Objs.length = = 0) {                return NULL; } Else {                 for(inti = objs.length;i>0;i--) {                    if(Text.getspanflags (objs[i-1]) = =Spannable.span_mark_mark) {                        returnObjs[i-1]; }                }                return NULL; }        }}

Custom span

 Public classStrikethroughspanextendsClickablespan {PrivateOnmylistener Mlistener; @Override Public voidOnClick (View widget) {if(Mlistener! =NULL) {mlistener.onreply (); }    }     Public voidSetonmylistener (Onreplylistener l) {Mlistener=l; }     Public InterfaceOnmylistener { Public voidonmy (); }}

Put the spanned on Android's TextView control to show

    Private voidInithtmltextview (TextView text, spanned spanned) {Spannablestringbuilder SSB=NewSpannablestringbuilder (spanned); Strikethroughspan[] Replyspans= Ssb.getspans (0, Spanned.length (), Strikethroughspan.class);  for(FinalStrikethroughspan Span:replyspans) {Span.setonmylistener (NewOnmylistener () {@Override Public voidonmy () {...}        }); } urlspan[] Spans= Ssb.getspans (0, Spanned.length (), Urlspan.class);  for(FinalUrlspan Span:spans) {            intStart =Ssb.getspanstart (span); intEnd =ssb.getspanend (span); intFlags =ssb.getspanflags (span); Clickablespan span2=NewClickablespan () {@Override Public voidOnClick (View widget) {String URL=Span.geturl ();            ...... }            };            Ssb.removespan (span);        Ssb.setspan (span2, start, end, flags); } imagespan[] Imagespans= Ssb.getspans (0, Spanned.length (), Imagespan.class); Finalarraylist<string> URLs =NewArraylist<string>();  for(FinalImagespan Span:imagespans) {            intStart =Ssb.getspanstart (span); intEnd =ssb.getspanend (span); intFlags =ssb.getspanflags (span); FinalString URL =Span.getsource ();            Urls.add (URL); Clickablespan span2=NewClickablespan () {@Override Public voidOnClick (View widget) {...}            }; Clickablespan[] Click_spans= Ssb.getspans (Start, End, Clickablespan.class); if(Click_spans.length! = 0) {                 for(Clickablespan C_span:click_spans) {Ssb.removespan (C_span);        }} ssb.setspan (Span2, start, end, flags);        } text.settext (SSB);    Text.setmovementmethod (TextViewFixTouchConsume.LocalLinkMovementMethod.getInstance ()); }

HTML format converted to TextView supported spanned

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.