这是我自己封的俩个类,只要调用就能使用,相当简单:
package com.example.spanable_textview;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.example.spanable_textview.MainActivity.spannableListener;
//这个类是获取SpannableString对象,以及添加颜色,跟点击事件的
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
public class getSpannableString{
/**
*
* @param s textView的内容
* @param str 你所要改变的字符串,其实主要是要的他的长度
* @param start_ 开始的位置
* @param listener 监听器
* @return
*/
//这个是你所要改变的内容
public static SpannableString getClickableSpan(CharSequence s, String str,int start_,OnClickListener listener) {
int start = start_;
int end = str.length();
SpannableString spanableInfo = new SpannableString(s);
spanableInfo.setSpan(new Clickable(listener),
start, end+start, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
spanableInfo.setSpan(Color.BLUE, start, end+start,
Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
return spanableInfo;
}
//这个是网络
public static SpannableString getClickableHttpSpan (CharSequence charSequence,OnClickListener listener){
SpannableString spanableInfo = new SpannableString(charSequence);
Pattern pattern =
Pattern.compile("((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\[email protected]#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\[email protected]#$%^&*+?:_/=<>]*)?)");
Matcher matcher =pattern.matcher(charSequence);
if (matcher.find()) {
System.out.println("start="+matcher.start());
System.out.println("end=="+matcher.end());
spanableInfo.setSpan(new Clickable(listener), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spanableInfo.setSpan(new ForegroundColorSpan(Color.BLUE), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spanableInfo;
}
//这是手机号
public static SpannableString getClickablePhoneSpan(CharSequence charSequence, OnClickListener listener){
SpannableString spannableString =new SpannableString(charSequence);
Pattern pattern = Pattern.compile("(\\(86\\))?(13[0-9]|15[0-35-9]|18[0125-9])\\d{8}");
Matcher matcher =pattern.matcher(charSequence);
if (matcher .find()) {
spannableString.setSpan(new Clickable(listener), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString .setSpan(new ForegroundColorSpan(Color.BLUE), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spannableString;
}
}
还有一个ClickableSpan的实现类,主要是为了实现点击鲜果
package com.example.spanable_textview;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.View.OnClickListener;
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);
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(ds.linkColor);
ds.setUnderlineText(false); // 去掉下划线
}
}
这里有我的demo。有兴趣的可以看看http://download.csdn.net/download/u012808234/8356749