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());
textview显示不同的颜色、样式来表示,需要用到SpannableString对象来处理
下面是从网上找的一段代码,已验证过:
mTextView = (TextView)findViewById(R.id.test);
SpannableString tSS = new SpannableString(“SpannableString学习中”);
tSS.setSpan(new BackgroundColorSpan(Color.RED), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //红色高亮
tSS.setSpan(new UnderlineSpan(), 15, 18,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //下划线
tSS.setSpan(new StyleSpan(android.graphics.Typeface.BOLD_ITALIC), 2, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //斜体
mTextView.setText(tSS);
关键方法:
public void setSpan (Object what, int start, int end, int flags)
主要是start跟end
start是起始位置,无论中英文,都算一个。从0开始计算起。end是结束位置,所以处理的文字,包含开始位置,但不包含结束位置。
下面解决点击不同文字跳转到不同activity的问题:
直接贴代码:
package com.feel;
import android.app.Activity;
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.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class TextViewJump2Activity extends Activity {
/** Called when the activity is first created. */
private TextView mClickableText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
mClickableText = new TextView(this);
mClickableText.setClickable(true);
mClickableText.setTextSize(20);
layout.addView(mClickableText);
setContentView(layout);
mClickableText.setText(getClickableSpan());
mClickableText.setMovementMethod(LinkMovementMethod.getInstance());
}
private SpannableString getClickableSpan() {
View.OnClickListener l = new View.OnClickListener() {
//如下定义自己的动作
public void onClick(View v) {
Toast.makeText(TextViewJump2Activity.this, "Click Success", Toast.LENGTH_SHORT).show();
//在这里就可以做跳转到activity或者弹出对话框的操作了
}
};
SpannableString spanableInfo = new SpannableString("This is a test, Click Here!");
int start = 16;
int end = spanableInfo.length();
spanableInfo.setSpan(new Clickable(l), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return spanableInfo;
}
}
class Clickable extends ClickableSpan implements OnClickListener {
private final View.OnClickListener mListener;
public Clickable(View.OnClickListener l) {
mListener = l;
}
@Override
public void onClick(View v) {
mListener.onClick(v);
}
}