频道栏目
IT货架 > > 正文
Android---12---TextView中单击链接弹出Activity
网友分享于:Jan 1, 1970 8:00:00 AM    来源: IT货架   

标签:activity   android   textview   

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

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





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

标签:activity   android   textview   

相关板块推荐 iPhone WindowsPhone

广告服务联系QQ:1134687142 | 网站地图

版权所有: IT货架- 内容来自互联网,仅供用于技术学习,请遵循相关法律法规. 京ICP备11030978号-1