Android---12---TextView中单击链接弹出Activity

MainActivity.java:


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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.MenuItem;
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) findViewById(R.id.textview1);
		TextView textView2 = (TextView) findViewById(R.id.textview2);
		
		String text = "显示Activity1";
		String text2 = "显示Activity2";
		
		SpannableString spannableString = new SpannableString(text);
		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, text.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
		
		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_INCLUSIVE);
		
		
		textView.setText(spannableString);
		textView2.setText(spannableString2);
		
		textView.setMovementMethod(LinkMovementMethod.getInstance());
		textView2.setMovementMethod(LinkMovementMethod.getInstance());
		
	}

}


activity_mina.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.textviewdemo0.MainActivity" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:textSize="50sp"/>
    <TextView 
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:textColor="#000FFF"/>

</LinearLayout>


activity1.java:


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");
	}
}



activity2.java:


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");
	}
}

需要对这两个活动进行注册。





郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。