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