Android miscellaneous-textview processing HTML tags

Source: Internet
Author: User
Tags encode string

Sometimes you need to display the string with HTML tags in textview and want to automatically convert the HTML tag to a clickable link.

You can handle the problem as follows:

Charsequence richtext = html. fromhtml (Message );
Mtext. setmovementmethod (linkmovementmethod. getinstance ());

In addition, if the string has passed the encode, the actual display should be garbled, similar to the string with the % 20 method below, so you can re-decode it.

The Code is as follows:

Public class mainactivity extends activity {private textview mtext; private string message; // Private string beginstr = "always % 20 bear % 20In % 20 mind % 20 that % 20 your % 20own % 20 Resolution % 20to % 20 success % 20is % 20 more % 20 important % 20 than % 20anything. \ n "+ //" Please % 20% 3ca % 20 href % 3d % 22 HTTP % 3A % 2f % 2fwww.baidu.com % 22% 3 econtact % 20us % 3C % 2fa % 3E. "; private string beginstr =" huarang's blog <a href = 'HTTP: // www.baidu.com '> stamp me </a> "; @ Override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); mtext = (textview) findviewbyid (R. id. hello); try {/*** the % 20 string above is set intentionally, this is an encode string *. Therefore, decode is required to display the correct content */message = urldecoder. decode (beginstr, "UTF-8");} catch (unsupportedencodingexception e) {// todo auto-generated Catch Block E. printstacktrace ();} Charsequence richtext = html. fromhtml (Message);/** if you can click a hyperlink, you need to set the following settings * if you want to make the content scroll and click a hyperlink at the same time, you just need to set linkmovementmethod. * Because it inherits scrollingmovementmethod. For details about scrollingmovementmethod, you can view the vertical or horizontal scrolling of textview implemented by Android */mtext. setmovementmethod (linkmovementmethod. getinstance (); mtext. settext (richtext );}}

 

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.