代码改变世界

TextView单击链接弹出Activity

2014-03-19 21:12  kingshow  阅读(282)  评论(0编辑  收藏  举报

 

    在TextView中的文字,点击后,怎么弹出Activity?

一、创建工程,如图

二、activity_main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp" />
    
    <TextView
        android:id="@+id/textview2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp" 
        android:layout_marginTop="20dp"
        />

</LinearLayout>
View Code

三、AndroidManifest.xml中代码

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

</manifest>
View Code

四、MainActivity.java中代码

package com.study.android_textview3;

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 textView = (TextView)this.findViewById(R.id.textview);
        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() {
            
            @Override
            public void onClick(View widget) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainActivity.this, Activity1.class);
                startActivity(intent);
            }
        }, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableString2.setSpan(new ClickableSpan() {
            
            @Override
            public void onClick(View widget) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainActivity.this, Activity2.class);
                startActivity(intent);
            }
        }, 0, text2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(spannableString2);
        textView2.setText(spannableString2);
        textView.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;
    }
    
}
View Code

五、Activity1和Activity2中代码

package com.study.android_textview3;

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

public class Activity1 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setTitle("Activity1");
    }
}
View Code
package com.study.android_textview3;

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

public class Activity2 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setTitle("Activity2");
    }
}
View Code

六、效果图