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