Display Textview with html text in Android --- [HTML Tag]

Source: Internet
Author: User

In the flourishing age of the Internet, optical text cannot satisfy people's appetite. images, flash, audio, and video have become the mainstream display of web pages, and are also the same on mobile phones. Display the data obtained from the network on your mobile phone. You naturally think of either webview or TextView. Of course, webView can directly display html pages. The TextView I mainly talk about shows html content.

First, let's talk about the tags that TextView supports. By checking the source code, we can find that Textview can parse some html tags, such:

 

<A href = "...">
<B>
<Big>
<Blockquote>
<Br>
<Cite>
<Dfn>
<Div align = "...">
<Em>
<Font size = "..." color = "..." face = "...">
<H1>
<H2>
<H3>
<H4>
<H5>
<H6>
<I>

<P>
<Small>
<Strike>
<Strong>
<Sub>
<Sup>
<Tt>
<U>

You can check the source code of android. text. Html, which is written as follows:

 

Private void handleStartTag (String tag, Attributes attributes ){
If (tag. inclusignorecase ("br ")){
// We don't need to handle this. TagSoup will 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. inclusignorecase ("p ")){
HandleP (mSpannableStringBuilder );
} Else if (tag. inclusignorecase ("div ")){
HandleP (mSpannableStringBuilder );
} Else if (tag. inclusignorecase ("em ")){
Start (mSpannableStringBuilder, new Bold ());
} Else if (tag. inclusignorecase ("B ")){
Start (mSpannableStringBuilder, new Bold ());
} Else if (tag. inclusignorecase ("strong ")){
Start (mSpannableStringBuilder, new Italic ());
} Else if (tag. canonical signorecase ("cite ")){
Start (mSpannableStringBuilder, new Italic ());
} Else if (tag. inclusignorecase ("dfn ")){
Start (mSpannableStringBuilder, new Italic ());
} Else if (tag. inclusignorecase ("I ")){
Start (mSpannableStringBuilder, new Italic ());
} Else if (tag. inclusignorecase ("big ")){
Start (mSpannableStringBuilder, new Big ());
} Else if (tag. inclusignorecase ("small ")){
Start (mSpannableStringBuilder, new Small ());
} Else if (tag. inclusignorecase ("font ")){
StartFont (mSpannableStringBuilder, attributes );
} Else if (tag. inclusignorecase ("blockquote ")){
HandleP (mSpannableStringBuilder );
Start (mSpannableStringBuilder, new Blockquote ());
} Else if (tag. inclusignorecase ("tt ")){
Start (mSpannableStringBuilder, new Monospace ());
} Else if (tag. inclusignorecase ("")){
StartA (mSpannableStringBuilder, attributes );
} Else if (tag. inclusignorecase ("u ")){
Start (mSpannableStringBuilder, new Underline ());
} Else if (tag. inclusignorecase ("sup ")){
Start (mSpannableStringBuilder, new Super ());
} Else if (tag. inclusignorecase ("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. inclusignorecase ("img ")){
StartImg (mSpannableStringBuilder, attributes, mImageGetter );
} Else if (mTagHandler! = Null ){
MTagHandler. handleTag (true, tag, mSpannableStringBuilder, mReader );
}
}

Private void handleEndTag (String tag ){
If (tag. inclusignorecase ("br ")){
HandleBr (mSpannableStringBuilder );
} Else if (tag. inclusignorecase ("p ")){
HandleP (mSpannableStringBuilder );
} Else if (tag. inclusignorecase ("div ")){
HandleP (mSpannableStringBuilder );
} Else if (tag. inclusignorecase ("em ")){
End (mSpannableStringBuilder, Bold. class, new StyleSpan (Typeface. BOLD ));
} Else if (tag. inclusignorecase ("B ")){
End (mSpannableStringBuilder, Bold. class, new StyleSpan (Typeface. BOLD ));
} Else if (tag. inclusignorecase ("strong ")){
End (mSpannableStringBuilder, Italic. class, new StyleSpan (Typeface. ITALIC ));
} Else if (tag. canonical signorecase ("cite ")){
End (mSpannableStringBuilder, Italic. class, new StyleSpan (Typeface. ITALIC ));
} Else if (tag. inclusignorecase ("dfn ")){
End (mSpannableStringBuilder, Italic. class, new StyleSpan (Typeface. ITALIC ));
} Else if (tag. inclusignorecase ("I ")){
End (mSpannableStringBuilder, Italic. class, new StyleSpan (Typeface. ITALIC ));
} Else if (tag. inclusignorecase ("big ")){
End (mSpannableStringBuilder, Big. class, new RelativeSizeSpan (1.25f ));
} Else if (tag. inclusignorecase ("small ")){
End (mSpannableStringBuilder, Small. class, new RelativeSizeSpan (0.8f ));
} Else if (tag. inclusignorecase ("font ")){
EndFont (mSpannableStringBuilder );
} Else if (tag. inclusignorecase ("blockquote ")){
HandleP (mSpannableStringBuilder );
End (mSpannableStringBuilder, Blockquote. class, new QuoteSpan ());
} Else if (tag. inclusignorecase ("tt ")){
End (mSpannableStringBuilder, Monospace. class,
New TypefaceSpan ("monospace "));
} Else if (tag. inclusignorecase ("")){
EndA (mSpannableStringBuilder );
} Else if (tag. inclusignorecase ("u ")){
End (mSpannableStringBuilder, Underline. class, new UnderlineSpan ());
} Else if (tag. inclusignorecase ("sup ")){
End (mSpannableStringBuilder, Super. class, new SuperscriptSpan ());
} Else if (tag. inclusignorecase ("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 from the source code that apart from some default labels, it also supports custom tags. See the following code:

Else if (mTagHandler! = Null ){
MTagHandler. handleTag (false, tag, mSpannableStringBuilder, mReader );
}

The system calls the handleTag method of mTagHandler. Therefore, we can implement this interface to parse the custom tag type.

For more information, see the following examples:

 

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, which can be a jump page or a pop-up dialog box. Below is a jump page
MContext. startActivity (new Intent (mContext, MainActivity. class ));
}
}

}

Call page:

 

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 tag: <br> TView. setText (Html. fromHtml (sText, null, new MxgsaTagHandler (this )));
TView. setClickable (true );
TView. setMovementMethod (LinkMovementMethod. getInstance ());
}

}

The next article will show html text with images!

 

 

 

 

 

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.