Android TextView displays HTML class-parsed Web pages and pictures and custom label usage examples _android

Source: Internet
Author: User
Tags sqlite database

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.

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.