Textview hyperlink output

Source: Internet
Author: User

Textview in Android is similar to label in ASP. NET and <label> </lable> in HTML.

IfCodeTo do this:

 Package Idroidgame. activitytest; Import Android. App. activity; Import Android. OS. Bundle; Import Android. widget. textview; Public   Class Activitytest Extends Activity {textview TV = Null ; /** Called when the activity is first created .*/ @ OverridePublic   Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); setcontentview (R. layout. Main ); TV = (textview)This. Findviewbyid (R. Id. tvhelloworld); string STR ="<A href = 'HTTP: // www.xnadevelop.com '> </a>"; TV. settext (STR ); }}

You will find that<A href = 'HTTP: // www.xnadevelop.com '> </a>The original mode is output.

You mustProgramYou can do this by displaying hyperlinks in.

1. Add Android: autolink = "all" to the textview flag"

<? XML version ="1.0"Encoding ="UTF-8"?> <Linearlayout xmlns: Android ="Http://schemas.android.com/apk/res/android"Android: Orientation ="Vertical"Android: layout_width ="Fill_parent"Android: layout_height ="Fill_parent"> <Textviewandroid: Id ="@ + ID/tvhelloworld"Android: layout_width ="Fill_parent"Android: layout_height ="Wrap_content"Android: text ="@ String/Hello"Android: autolink ="All"/> </Linearlayout>

The A flag is not required. The application determines whether to set the text as a hyperlink Based on http: //.

TV = (textview) This. findviewbyid (R. Id. tvhelloworld); string STR = "http://www.xnadevelop.com"; TV. settext (STR );

2. textview does not need to be modified in the layout file. It is implemented in the program:

 Package Idroidgame. activitytest; Import Android. App. activity; Import Android. OS. Bundle; Import Android. Text. util. linkify; Import Android. widget. textview; Public   Class Activitytest Extends Activity {textview TV = Null ;/** Called when the activity is first created .*/ @ Override Public   Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); setcontentview (R. layout. Main); TV = (textview) This . Findviewbyid (R. Id. tvhelloworld ); TV. setautolinkmask (linkify. All ); String STR =" Http://www.xnadevelop.com ";
TV. setmovementmethod (linkmovementmethod. getinstance ()); TV. settext (STR );}}

Linkify has many options, corresponding to the layout File

Options

3. Format output text

 Package Idroidgame. activitytest; Import Android. App. activity; Import Android. OS. Bundle; Import Android. widget. textview; Import Android. Text. html; Public   Class Activitytest Extends Activity {textview TV = Null ; /** Called when the activity is first created .*/ @ Override Public   Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); setcontentview (R. layout. Main); TV = (textview) This . Findviewbyid (R. Id. tvhelloworld ); String STR ="<A href = 'HTTP: // www.xnadevelop.com '> xnadevelop.com </a>"; TV. setmovementmethod (linkmovementmethod. getinstance (); TV. settext (html. fromhtml (STR )); }}

In this case, unlike the first two methods, a flag is not allowed for the output hyperlink.

4. Use spannable

 Package Idroidgame. activitytest; Import Android. App. activity; Import Android. OS. Bundle; Import Android. widget. textview; Import Android. Text. spannablestring; Import Android. Text. spanned; Import Android. Text. style. urlspan; Public   Class Activitytest Extends Activity {textview TV = Null ; /** Called when the activity is first created .*/ @ OverridePublic   Void Oncreate (bundle savedinstancestate ){ Super . Oncreate (savedinstancestate); setcontentview (R. layout. Main); TV = (textview) This . Findviewbyid (R. Id. tvhelloworld ); Spannablestring Ss =NewSpannablestring ("Http://xnadevelop.com"); SS. setspan (NewUrlspan ("Http://www.xnadevelop.com"), 0, 21, spanned. span_exclusive_exclusive );          TV. setmovementmethod (linkmovementmethod. getinstance ()); TV. settext (SS );}}

This method can only display hyperlink styles, not vertices.

 

Note: TV. setmovementmethod (linkmovementmethod. getinstance () must be added to methods 2, 3, and 4 ());

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.