安卓课程十四 TextView单击链接弹出Activity

先建立跳转后的两个activity,分别是:

import android.app.Activity;
import android.os.Bundle;

public class Active1 extends Activity {
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("Active1");
	}
}
import android.app.Activity;
import android.os.Bundle;

public class Active2 extends Activity {
	public Active2() {
	}
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("Active2");
	}
}

 AndroidManifest.xml内容是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.textview3"
    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.textview3.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=".Active1"></activity>
        <activity android:name=".Active2"></activity>
    </application>
</manifest>

 主视图MainActivity内容是:

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		TextView textView1 = (TextView) this.findViewById(R.id.textView1);
		TextView textView2 = (TextView) this.findViewById(R.id.textView2);
		
		String text1 = "显示Activity1";
		String text2 = "显示Activity2";
//		 主要用来拆分字符串
		SpannableString spannableString = new SpannableString(text1);
		SpannableString spannableString2 = new SpannableString(text2);
		
		spannableString.setSpan(new ClickableSpan() {
			
			public void onClick(View widget) {
				 Intent intent = new Intent(MainActivity.this,Active1.class);
				 startActivity(intent) ;
				
			}
		}, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
	 
		spannableString2.setSpan(new ClickableSpan() {
			
			public void onClick(View widget) {
				 Intent intent = new Intent(MainActivity.this,Active2.class);
				 startActivity(intent) ;
				
			}
		}, 0, text2.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
		
		textView1.setText(spannableString );
		textView2.setText(spannableString2 );
		
		textView1.setMovementMethod(LinkMovementMethod.getInstance());
		textView2.setMovementMethod(LinkMovementMethod.getInstance());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

 执行效果如图:


安卓课程十四 TextView单击链接弹出Activity
 点击后:


安卓课程十四 TextView单击链接弹出Activity
 

相关推荐