在一个Activity里面的TextView上面添加网页链接,启动后到另一个Activity里面!
2015-06-02 17:29
363 查看
转载自
点击打开链接http://blog.csdn.net/gdky005/article/details/7614153
Android 在 TextView 中设置超链接、颜色、字体、图片 /content/1435675.html
可以添加很多的属性,样式或者是什么的,目前要完成的功能是 点击TextView里面的某个文字链接,进入另外一个Activity里面!
例如你可以做微博里面的 @XXX; 点击后进入他的个人主页!
下面都是Activity:
[java] view
plaincopyprint?
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);
}
}
}
这是设置文件的颜色和样式的
[html] view
plaincopyprint?
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(LinkMovementMet
点击打开链接http://blog.csdn.net/gdky005/article/details/7614153
Android 在 TextView 中设置超链接、颜色、字体、图片 /content/1435675.html
可以添加很多的属性,样式或者是什么的,目前要完成的功能是 点击TextView里面的某个文字链接,进入另外一个Activity里面!
例如你可以做微博里面的 @XXX; 点击后进入他的个人主页!
下面都是Activity:
[java] view
plaincopyprint?
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);
}
}
}
这是设置文件的颜色和样式的
[html] view
plaincopyprint?
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(LinkMovementMet
相关文章推荐
- JPA中one-to-one关系的单向映射示例
- Python脚本文件打包成可执行文件的方法
- Windows 10常见问题解答
- [leetcode] 73.Set Matrix Zeroes
- 利用solrj操作solr的API
- 倒计时
- onmouseover和onmouseout的烦恼
- IOS 判断手机号 身份证号
- JAVA并行框架学习之ForkJoin
- 使用boost做post/get请求开发笔记
- Android Volley完全解析(四),带你从源码的角度理解Volley
- php加载速度提高方法
- CDSN markdown 不能设置字体大小?
- 新家地址 http://vizee.org
- 【转】分享II→IV FPGA本人的几个版本电源模块设计的方案
- [Java] 环境变量设置
- hdu 1298 T9
- 详细分析SAT阅读的突破途径
- mysql时间操作函数和存储过程
- 平安科技移动开发二队技术周报(第二期)