Now the network of prosperous times, the light text is unable to satisfy people's appetite, pictures, flash, audio, video on the mainstream of the Web page is shown, on the phone is the same. On the mobile phone display from the network data obtained from the display, we naturally think of two ways, one is WebView, one is TextView. Of course WebView directly display HTML page on the line, I mainly said that the TextView display HTML content.
First of all, the next textview in the end to support those tags, through the source of the view, found TextView can parse part of the HTML tags, such as:
Copy Code code 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>
You want to find out that they can view the android.text.Html source code, which has a paragraph to write:
Copy Code code as follows:
private void Handlestarttag (String tag, Attributes Attributes) {
if (tag.equalsignorecase ("BR")) {
We don ' t need to handle this. Tagsoup'll ensure that there ' s </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);
}
}
From the source you can see that, in addition to some of the default tags, it also supports the custom label; look at the following code:
else if (Mtaghandler!= null) {
Mtaghandler.handletag (False, Tag, Mspannablestringbuilder, Mreader);
}
The system invokes the Mtaghandler Handletag method. So, we can implement this interface to resolve our own defined tag types.
Specifically, you can look at the following examples:
Copy Code code 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 {
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 a jump page
Mcontext.startactivity (New Intent (Mcontext,mainactivity.class));
}
}
}
Call Page:
Copy Code code 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 ());
}
}
The next chapter will show you the HTML text with the picture! Cond.....