This example describes the Android TextView display HTML class parsing pages and pictures and custom tags. Share to everyone for your reference, specific as follows:
The Android system displays the best controls for HTML pages as WebView, and sometimes needs to display HTML pages, images, and parsing custom tags in textview to meet specific needs.
1, TextView display HTML class resolution of the Web page
Charsequence RichText = html.fromhtml ("<strong> Blog of radish cabbage </strong>--<a href= ' http://orgcent.com ' > Http://orgcent.com</a> ");
Mtvtext.settext (RichText);
This line must, otherwise hyperlinks cannot be clicked, Scrollingmovementmethod implements the scroll bar
mtvtext.setmovementmethod (Linkmovementmethod.getinstance ());
PS: If you want to make content scrollable and hyperlinks clickable, just set Linkmovementmethod. Because it inherits the Scrollingmovementmethod. About the Scrollingmovementmethod description, you can view Android to achieve TextView vertical or horizontal scrolling
2, TextView display HTML resolution of the picture and custom labels
Final String html = "Radish Cabbage blog <mytag color= ' Blue ' > Custom </
Mytag> "; Handle unknown tags, usually the system defaults cannot process the label final Html.taghandler Taghandler = new Html.taghandler () {int contentindex = 0;/** * Opening: Yes No to start tag * Tag: Label name * Output: Outputs information, used to save the processed information * XmlReader: Read the current label information, such as properties/public void Handletag (Boolean opening, String t
AG, Editable output, XMLReader XMLReader) {if ("MyTag". Equals (tag)) {if (opening) {//Get the content start position of the current label
ContentIndex = Output.length ();
try {final String color = (String) xmlreader.getproperty ("Color");
catch (Exception e) {e.printstacktrace ();
} else {final int length = Output.length ();
String content = output.subsequence (contentindex, length). toString ();
spannablestring spanstr = new spannablestring (content); Spanstr.setspan (New Foregroundcolorspan (Color.green), 0, Content.length (), Spannable.span_inclusive_excLusive);
Output.replace (contentindex, length, spanstr);
} System.out.println ("Opening:" + opening + ", tag:" + tag + ", Output:" + output);};
Parse Picture final Html.imagegetter imagegetter = new Html.imagegetter () {public drawable getdrawable (String source) {
The picture must be loaded asynchronously here drawable d = null;
try {InputStream is = new Defaulthttpclient (). Execute (new HttpGet (source)). GetEntity (). getcontent ();
Bitmap BM = Bitmapfactory.decodestream (IS);
D = new bitmapdrawable (BM);
SetBounds (0, 0, bm.getwidth (), Bm.getheight ());
D.setbounds (0, 0, 200, 300);
catch (Exception e) {e.printstacktrace ();}
return D;
}
};
RichText = html.fromhtml (Html, Imagegetter, Taghandler);
Mtvtext.settext (RichText);
More interested readers of Android related content can view the site: "Summary of the use of Android controls," "The Summary of Android view tips," "Android File Operation tips Summary", "Android operation SQLite Database Skills Summary", " Android operations JSON format Data tips summary, "Android Database Operation skills Summary", "Android programming activity Operation Skills Summary", "Android programming development of SD card operation method Summary", "Android Development and advanced tutorial And the Android resource Operation Skills Summary
I hope this article will help you with the Android program.