在一个Activity里面的TextView上面添加网页链接,启动后到另一个Activity里面!

可以添加很多的属性,样式或者是什么的,目前要完成的功能是 点击TextView里面的某个文字链接,进入另外一个Activity里面!
例如你可以做微博里面的 @XXX; 点击后进入他的个人主页!

下面都是Activity:

package wq.gdky005;

import java.util.ArrayList;

import android.R.color;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class AbcActivity extends Activity {
	private TextView tv;
	private static Context ctx;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		tv = new TextView(this);
		tv.setClickable(false);
		ctx = this;

		String htmlLinkText = "";

		htmlLinkText = "aaaaaaaaaa"
				+ "<a style=\"color:red;\" href=\"我是超链接……\">@超链接点击事件;</a>" +
				"     <a style=\"color:red;\" href=\"我是超链接2……\">@超链接点击事件;</a>"
				+ "aaaaaaaaaaaaaaaa";
		// 文字的样式(style)被覆盖,不能改变……

		tv.setText(Html.fromHtml(htmlLinkText));
		tv.setMovementMethod(LinkMovementMethod.getInstance());
		CharSequence text = tv.getText();
		if (text instanceof Spannable) {
			int end = text.length();
			Spannable sp = (Spannable) tv.getText();
			URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
			SpannableStringBuilder style = new SpannableStringBuilder(text);
			style.clearSpans();// should clear old spans
			for (URLSpan url : urls) {
				MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
				style.setSpan(myURLSpan, sp.getSpanStart(url),
						sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			}
			tv.setText(style);
		}
		setContentView(tv);

	}

	private static class MyURLSpan extends ClickableSpan {

		private String mUrl;

		MyURLSpan(String url) {
			mUrl = url;
		}

		@Override
		public void onClick(View widget) {
			Toast.makeText(ctx, mUrl, Toast.LENGTH_LONG).show();
			widget.setBackgroundColor(Color.parseColor("#00000000"));
			
			Intent intent = new Intent(AbcActivity.ctx, AAAActivity.class);
			ctx.startActivity(intent);
			
			
		}
	}
}


 这是设置文件的颜色和样式的

TextView是用来显示文本的,有时需要给TextView中的个别字设置为超链接,或者设置个别字的颜色、字体等,那就需要用到Spannable对象,可以借助Spannable对象实现以上设置

myTextView = (TextView) this.findViewById(R.id.myTextView);   
    
  //创建一个 SpannableString对象   
  SpannableString sp = new SpannableString("这句话中有百度超链接,有高亮显示,这样,或者这样,还有斜体.");   
  //设置超链接   
  sp.setSpan(new URLSpan("http://www.baidu.com"), 5, 7,   
  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);   
  //设置高亮样式一   
  sp.setSpan(new BackgroundColorSpan(Color.RED), 17 ,19,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);   
  //设置高亮样式二   
  sp.setSpan(new ForegroundColorSpan(Color.YELLOW),20,24,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);   
  //设置斜体   
  sp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 27, 29, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);   
  //SpannableString对象设置给TextView   
  myTextView.setText(sp);   
  //设置TextView可点击   
  myTextView.setMovementMethod(LinkMovementMethod.getInstance());   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值