TextView Display HTML Method parsing

Source: Internet
Author: User

Now the network of the prosperous times, light text is not enough to meet people's appetite, pictures, flash, audio, video will become the mainstream display of the Web page, on the phone is the same. On the phone display from the network side of the data obtained from the display, we naturally think of two ways, one is WebView, one is TextView. Of course WebView directly display HTML page, I mainly say TextView display HTML content.
First of all, say TextView exactly support those tags, through the source of the view, found that TextView can parse a portion of HTML tags, such as:

Copy CodeThe code is as follows:
<a href= "..." >
<b>
<big>
<blockquote>
<br>
<cite>
<dfn>
<div align= "..." >
<em>
<font size= "..." color= "..." face= "..." >
<i>

<p>
<small>
<strike>
<strong>
<sub>
<sup>
<tt>
<u>


We want to find out the android.text.Html source code, which is written in one of the following:

Copy CodeThe code is as follows:
private void Handlestarttag (String tag, Attributes Attributes) {
if (tag.equalsignorecase ("BR")) {
We don ' t need to handle this. Tagsoup would ensure that there's a </br> for each <br>
So we can safely emite the linebreaks when we handle the close tag.
} else if (Tag.equalsignorecase ("P")) {
Handlep (Mspannablestringbuilder);
} else if (Tag.equalsignorecase ("div")) {
Handlep (Mspannablestringbuilder);
} else if (Tag.equalsignorecase ("em")) {
Start (Mspannablestringbuilder, New Bold ());
} else if (Tag.equalsignorecase ("B")) {
Start (Mspannablestringbuilder, New Bold ());
} else if (Tag.equalsignorecase ("strong")) {
Start (Mspannablestringbuilder, New Italic ());
} else if (Tag.equalsignorecase ("cite")) {
Start (Mspannablestringbuilder, New Italic ());
} else if (Tag.equalsignorecase ("DFN")) {
Start (Mspannablestringbuilder, New Italic ());
} else if (Tag.equalsignorecase ("I")) {
Start (Mspannablestringbuilder, New Italic ());
} else if (Tag.equalsignorecase ("big")) {
Start (Mspannablestringbuilder, New Big ());
} else if (Tag.equalsignorecase ("small")) {
Start (Mspannablestringbuilder, New Small ());
} else if (tag.equalsignorecase ("Font")) {
Startfont (mspannablestringbuilder, attributes);
} else if (Tag.equalsignorecase ("blockquote")) {
Handlep (Mspannablestringbuilder);
Start (Mspannablestringbuilder, New Blockquote ());
} else if (Tag.equalsignorecase ("tt")) {
Start (Mspannablestringbuilder, New monospace ());
} else if (Tag.equalsignorecase ("a")) {
Starta (mspannablestringbuilder, attributes);
} else if (Tag.equalsignorecase ("U")) {
Start (Mspannablestringbuilder, New Underline ());
} else if (Tag.equalsignorecase ("sup")) {
Start (Mspannablestringbuilder, New Super ());
} else if (Tag.equalsignorecase ("sub")) {
Start (Mspannablestringbuilder, New Sub ());
} else if (tag.length () = = 2 &&
Character.tolowercase (Tag.charat (0)) = = ' h ' &&
Tag.charat (1) >= ' 1 ' && tag.charat (1) <= ' 6 ') {
Handlep (Mspannablestringbuilder);
Start (Mspannablestringbuilder, New Header (Tag.charat (1)-' 1 '));
} else if (Tag.equalsignorecase ("img")) {
Startimg (mspannablestringbuilder, attributes, Mimagegetter);
} else if (Mtaghandler! = null) {
Mtaghandler.handletag (True, tag, Mspannablestringbuilder, Mreader);
}
}
private void Handleendtag (String tag) {
if (tag.equalsignorecase ("BR")) {
Handlebr (Mspannablestringbuilder);
} else if (Tag.equalsignorecase ("P")) {
Handlep (Mspannablestringbuilder);
} else if (Tag.equalsignorecase ("div")) {
Handlep (Mspannablestringbuilder);
} else if (Tag.equalsignorecase ("em")) {
End (Mspannablestringbuilder, Bold.class, New Stylespan (Typeface.bold));
} else if (Tag.equalsignorecase ("B")) {
End (Mspannablestringbuilder, Bold.class, New Stylespan (Typeface.bold));
} else if (Tag.equalsignorecase ("strong")) {
End (Mspannablestringbuilder, Italic.class, New Stylespan (Typeface.italic));
} else if (Tag.equalsignorecase ("cite")) {
End (Mspannablestringbuilder, Italic.class, New Stylespan (Typeface.italic));
} else if (Tag.equalsignorecase ("DFN")) {
End (Mspannablestringbuilder, Italic.class, New Stylespan (Typeface.italic));
} else if (Tag.equalsignorecase ("I")) {
End (Mspannablestringbuilder, Italic.class, New Stylespan (Typeface.italic));
} else if (Tag.equalsignorecase ("big")) {
End (Mspannablestringbuilder, Big.class, New Relativesizespan (1.25f));
} else if (Tag.equalsignorecase ("small")) {
End (Mspannablestringbuilder, Small.class, New Relativesizespan (0.8f));
} else if (tag.equalsignorecase ("Font")) {
Endfont (Mspannablestringbuilder);
} else if (Tag.equalsignorecase ("blockquote")) {
Handlep (Mspannablestringbuilder);
End (Mspannablestringbuilder, Blockquote.class, New Quotespan ());
} else if (Tag.equalsignorecase ("tt")) {
End (Mspannablestringbuilder, Monospace.class,
New Typefacespan ("monospace"));
} else if (Tag.equalsignorecase ("a")) {
Enda (Mspannablestringbuilder);
} else if (Tag.equalsignorecase ("U")) {
End (Mspannablestringbuilder, Underline.class, New Underlinespan ());
} else if (Tag.equalsignorecase ("sup")) {
End (Mspannablestringbuilder, Super.class, New Superscriptspan ());
} else if (Tag.equalsignorecase ("sub")) {
End (Mspannablestringbuilder, Sub.class, New Subscriptspan ());
} else if (tag.length () = = 2 &&
Character.tolowercase (Tag.charat (0)) = = ' h ' &&
Tag.charat (1) >= ' 1 ' && tag.charat (1) <= ' 6 ') {
Handlep (Mspannablestringbuilder);
Endheader (Mspannablestringbuilder);
} else if (Mtaghandler! = null) {
Mtaghandler.handletag (False, Tag, Mspannablestringbuilder, Mreader);
}
}


Through the source can see, in addition to some of the default tags, it also supports custom tags; see the following code:
else if (mtaghandler! = null) {
Mtaghandler.handletag (False, Tag, Mspannablestringbuilder, Mreader);
}
The system calls the Mtaghandler Handletag method. So, we can implement this interface to parse our own defined label types.
Specifically, you can look at the following example:

Copy CodeThe code is as follows:
Package com.mxgsa.tvimg;
Import Org.xml.sax.XMLReader;
Import Android.content.Context;
Import android.content.Intent;
Import android.text.Editable;
Import Android.text.Html.TagHandler;
Import android.text.Spanned;
Import Android.text.style.ClickableSpan;
Import Android.view.View;
Import Android.view.View.OnClickListener;
public class Mxgsataghandler implements taghandler{
private int sindex = 0;
private int eindex=0;
Private final Context Mcontext;
Public Mxgsataghandler (Context context) {
Mcontext=context;
}
public void Handletag (Boolean opening, String tag, Editable output, XMLReader XMLReader) {
TODO auto-generated Method Stub
if (Tag.tolowercase (). Equals ("Mxgsa")) {
if (opening) {
Sindex=output.length ();
}else {
Eindex=output.length ();
Output.setspan (New Mxgsaspan (), Sindex, Eindex, spanned.span_exclusive_exclusive);
}
}
}
Private class Mxgsaspan extends Clickablespan implements onclicklistener{
@Override
public void OnClick (View widget) {
TODO auto-generated Method Stub
Specific code, can be a jump page, can be a pop-up dialog box, the following is the jump page
Mcontext.startactivity (New Intent (Mcontext,mainactivity.class));
}
}
}


Invoke page:

Copy CodeThe code is as follows:
Package com.mxgsa.tvimg;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.text.Html;
Import Android.text.method.LinkMovementMethod;
Import Android.widget.TextView;
public class Mxgsaactivity extends activity{
Private TextView TView;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.mxgsa_activity);
FindControl ();
SetData ();
}
private void FindControl () {
TView = (TextView) Findviewbyid (r.id.tvimage);
}
private void SetData () {
TODO auto-generated Method Stub
Final String stext = "Test Custom label:<br>Tview.settext (html.fromhtml (stext, NULL, new Mxgsataghandler (this));
Tview.setclickable (TRUE);
Tview.setmovementmethod (Linkmovementmethod.getinstance ());
}
}

TextView Display HTML method parsing

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.