1. Program Structure diagram
2, the code in the Mainactivity.java
PackageCom.example.setlinkdemo;Importjava.io.IOException;Importorg.xmlpull.v1.XmlPullParserException;Importandroid.app.Activity;Importandroid.content.Intent;Importandroid.content.res.ColorStateList;ImportAndroid.content.res.XmlResourceParser;ImportAndroid.os.Bundle;Importandroid.text.Html;Importandroid.text.SpannableString;Importandroid.text.Spanned;ImportAndroid.text.method.LinkMovementMethod;ImportAndroid.text.style.ClickableSpan;ImportAndroid.text.style.TextAppearanceSpan;ImportAndroid.view.View;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {PrivateTextView tv_content; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Tv_content= (TextView) This. Findviewbyid (r.id.tv_content); Setlink (); } Public voidSetlink () {//TODO auto-generated Method Stub//Create a Spannablestring objectSpannablestring SP =Newspannablestring (html.fromhtml (getString (R.string.set_account_check))); Sp.setspan (NewClickablespan () {@Override Public voidOnClick (View widget) {//TODO auto-generated Method StubStartActivity (NewIntent (mainactivity. This, Testactivity.class)); } }, 11, 15, spanned.span_inclusive_exclusive); Colorstatelist CSL=NULL; Xmlresourceparser Xpplinkcolor=getresources (). GETXML (R.color.link_color); Try{CSL=Colorstatelist.createfromxml (Getresources (), xpplinkcolor); } Catch(xmlpullparserexception E1) {e1.printstacktrace (); } Catch(IOException E2) {e2.printstacktrace (); } sp.setspan (NewTextappearancespan ("Monospace", Android.graphics.Typeface.NORMAL, (int) tv_content. GetTextSize (), CSL, CSL),11, 15, spanned.span_exclusive_exclusive); Tv_content.settext (SP); //do not add this sentence, hyperlinks cannot be clickedTv_content.setmovementmethod (Linkmovementmethod.getinstance ()); }}
3, the code in the Testactivity.java
package Com.example.setlinkdemo; import android.app.Activity; import Android.os.Bundle; public class testactivity extends Activity {@Override protected void OnCreate (Bundle savedinstancestate) { // TODO auto-generated method stub super .oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); }}
4. Code in the Link_color.xml file
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_pressed=" true " android:color=" #0099cc "/> <!--pressed- - <item android:state_focused= "true" android:color= "#0099cc"/> <!--focused- Default --></selector>
6. Code in the Strings.xml file
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >setlinkdemo</ string> <string name= "Hello_world" >hello world!</string> <string name= "Action_ Settings ">Settings</string> <string name=" Set_account_check "> continue means that you have agreed to the service agreement </string> </resources>
7. Code in the Androidmanifest.xml file
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.setlinkdemo"Android:versioncode= "1"Android:versionname= "1.0" > <uses-SDK Android:minsdkversion= "8"android:targetsdkversion= "/>" <Application Android:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" > <Activity Android:name=". Mainactivity "Android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MA In "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter& Gt </activity> <activity android:name= ". Testactivity "> </activity> </application></manifest>
(48) text in Android TextView through spannablestring to set hyperlinks, colors, fonts and other properties