Parsing the implementation method of adding custom HTML tags to textview in Android _android

Source: Internet
Author: User
Tags html tags

The TextView in Android itself supports part of the HTML format tag. This includes commonly used font size color settings, text links, and so on. It's also easier to use, just use the HTML class to convert it. For example:

Textview.settext (html.fromhtml (str));


However, there is a situation where the default supported labels may not be sufficient. For example, we need to click on a link in the TextView, return to an application in an interface, not just network connection, how to achieve?


After hours of research into the HTML-class source code in Android, a solution was found and the test passed.

First look at the source code of the HTML class has this paragraph:

Copy Code code as follows:

/**
* is notified when HTML tags are encountered that parser does
* not know you to interpret.
*/
public static interface Taghandler {
/**
* This method would be called Whenn the HTML parser encounters
* A tag that it does not know you to interpret.
*/
public void Handletag (Boolean opening, String tag,
Editable output, XMLReader XMLReader);

here defines an interface, what is the interface for?

Then continue to look at the code and see the code for parsing the HTML tag:

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);
}
}

You can see that if it is not the default label, the Mtaghandler Handletag method is invoked. So, we can implement this interface to resolve our own defined tag types.

Take another look at the example code I implemented to parse the <game> tag:

Copy Code code as follows:

public class Gametaghandler implements Taghandler {
private int startIndex = 0;
private int stopindex = 0;
@Override
public void Handletag (Boolean opening, String tag, Editable output,
XMLReader XMLReader) {
if (Tag.tolowercase (). Equals ("Game")) {
if (opening) {
Startgame (tag, output, xmlReader);
} else {
Endgame (tag, output, xmlReader);
}
}

}
public void Startgame (String tag, Editable output, XMLReader XMLReader) {
StartIndex = Output.length ();
}

public void Endgame (String tag, Editable output, XMLReader XMLReader) {
Stopindex = Output.length ();
Output.setspan (New Gamespan (), StartIndex, Stopindex,
spanned.span_exclusive_exclusive);
}

Private class Gamespan extends Clickablespan implements Onclicklistener {

@Override
public void OnClick (View v) {
Jump to a page
}
}

The above code is to parse the custom label for <game>...</game>.


Specific calling Method:

Textview.settext (html.fromhtml ("Click <game> here </game> jump to Game",

NULL, New Gametaghandler ());

Textview.setclickable (TRUE);

Textview.setmovementmethod (Linkmovementmethod.getinstance ());


After running, you can see the string in the text "Here" with a hyperlink, click on the link, the Gamespan class of the OnClick () method is called. You can jump in this method.

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.