记得在Manifest添加Activity:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.textview_showactivity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.textview_showactivity.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Activity1" > </activity> <activity android:name=".Activity2" > </activity> </application> </manifest>
1 package com.example.textview_showactivity; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.content.Intent; 6 import android.text.SpannableString; 7 import android.text.Spanned; 8 import android.text.method.LinkMovementMethod; 9 import android.text.style.ClickableSpan; 10 import android.view.Menu; 11 import android.view.View; 12 import android.widget.TextView; 13 14 public class MainActivity extends Activity { 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 19 TextView textView1 = (TextView) findViewById(R.id.textview1); 20 TextView textView2 = (TextView) findViewById(R.id.textview2); 21 22 String text1 = "显示Activity1"; 23 String text2 = "显示Activity2"; 24 25 // 主要用于拆分字符串 26 SpannableString spannableString1 = new SpannableString(text1); 27 SpannableString spannableString2 = new SpannableString(text2); 28 29 // 设置触发链接的文字从第一个到String的尾部 30 spannableString1.setSpan(new ClickableSpan() { 31 public void onClick(View widget) { 32 Intent intent1 = new Intent(MainActivity.this, Activity1.class); 33 startActivity(intent1); 34 } 35 }, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 36 37 spannableString2.setSpan(new ClickableSpan() { 38 public void onClick(View widget) { 39 Intent intent2 = new Intent(MainActivity.this, Activity2.class); 40 startActivity(intent2); 41 } 42 }, 0, text2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); 43 44 textView1.setText(spannableString1); 45 textView2.setText(spannableString2); 46 47 textView1.setMovementMethod(LinkMovementMethod.getInstance()); 48 textView2.setMovementMethod(LinkMovementMethod.getInstance()); 49 } 50 51 public boolean onCreateOptionsMenu(Menu menu) { 52 getMenuInflater().inflate(R.menu.activity_main, menu); 53 return true; 54 } 55 56 }