最近接到一个任务,是在聊天的过程,通过正则表达式匹配出所有的url,加上下划线,点击之后并跳转。一开始使用的是在xml文件中,改textview加上autolink的属性,但是加上属性之后,你会发现 ,我输入 ”我是www.baidu.com“,整句话都会加上下划线,导致跳转不正确。后面根据老大的提示,找到google匹配url的正则表达式,用代码去匹配发现是正常的。http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.0_r1/android/text/util/Regex.java#Regex.digitsAndPlusOnly%28java.util.regex.Matcher%29
后面,使用
float textSize =Textview.getTextSize();//获取textview的字体大小
SpannableString checkInSpanStr = new SpannableString(data);
matchUrl(checkInSpanStr, textSize);
Textview.setText(checkInSpanStr);
Textview.setMovementMethod(LinkMovementMethod.getInstance());
public void matchUrl(SpannableString data, float textSize) {
String pat3 = "(((?:(http|https|Http|Https|rtsp|Rtsp):\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)"
+ "\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_"
+ "\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?"
+ "((?:(?:[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}\\.)+" // named host
+ "(?:" // plus top level domain
+ "(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
+ "|(?:biz|b[abdefghijmnorstvwyz])"
+ "|(?:cat|com|coop|c[acdfghiklmnoruvxyz])"
+ "|d[ejkmoz]"
+ "|(?:edu|e[cegrstu])"
+ "|f[ijkmor]"
+ "|(?:gov|g[abdefghilmnpqrstuwy])"
+ "|h[kmnrtu]"
+ "|(?:info|int|i[delmnoqrst])"
+ "|(?:jobs|j[emop])"
+ "|k[eghimnrwyz]"
+ "|l[abcikrstuvy]"
+ "|(?:mil|mobi|museum|m[acdghklmnopqrstuvwxyz])"
+ "|(?:name|net|n[acefgilopruz])"
+ "|(?:org|om)"
+ "|(?:pro|p[aefghklmnrstwy])"
+ "|qa"
+ "|r[eouw]"
+ "|s[abcdeghijklmnortuvyz]"
+ "|(?:tel|travel|t[cdfghjklmnoprtvwz])"
+ "|u[agkmsyz]"
+ "|v[aceginu]"
+ "|w[fs]"
+ "|y[etu]"
+ "|z[amw]))"
+ "|(?:(?:25[0-5]|2[0-4]" // or ip address
+ "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]"
+ "|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1]"
+ "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
+ "|[1-9][0-9]|[0-9])))"
+ "(?:\\:\\d{1,5})?)" // plus option port number
+ "(\\/(?:(?:[a-zA-Z0-9\\;\\/\\?\\:\\@\\&\\=\\#\\~" // plus option query params
+ "\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?"
+ "(?:\\b|$))";
Pattern pattern = Pattern.compile(pat3, Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(data);
int start, end;
while (matcher.find()) {
start = matcher.start(1);
end = matcher.end(1);
final String u = data.toString().substring(start, end);
data.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
Intent intent = new Intent();
intent.setClass(mActivity,
xxx.class);
Bundle bundle = new Bundle();
bundle.putString("url", u);
intent.putExtras(bundle);
mActivity.startActivity(intent);
}
}, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//一定要注意代码顺序,先设置点击事件,然后在设置字体的大小和颜色
ColorStateList csl = mActivity.getResources().getColorStateList(R.color.color_list);//color_list就是一个color的selecter
data.setSpan(new TextAppearanceSpan("default", android.graphics.Typeface.NORMAL, (int) textSize, csl, null ), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//五个参数分别是,字体名称,字体样式,字体大小,字体颜色,字体链接颜色
}
}
遇到一个问题:长按事件会触发点击事件,