/**
* 设置具体某个关键字可点击
*
* @param textView
* @param spannableString
* @param pattern
* @param clickableSpan
*/
public static void setKeyworkClickable(Context context, TextView textView, SpannableString spannableString, Pattern pattern,
ClickableSpan clickableSpan) {
Matcher matcher = pattern.matcher(spannableString.toString());
while (matcher.find()) {
String key = matcher.group();
if (!"".equals(key)) {
int start = spannableString.toString().indexOf(key);
int end = start + key.length();
setClickTextView(context, textView, spannableString, start, end, clickableSpan);
}
}
}
/**
* 设置TextView中的字段可点击
*
* @param textView
* @param spannableString
* @param start
* @param end
* @param clickableSpan
*/
public static void setClickTextView(Context context, TextView textView, SpannableString spannableString, int start, int end,
ClickableSpan clickableSpan) {
spannableString.setSpan(clickableSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#5A7089")), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setHighlightColor(context.getResources().getColor(R.color.book_list_category_name_text_selector));
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
通过实现setKeyWordClickable()方法来实现某段文字可点击。
具体使用方法可以参考如下:
SpannableString spannableString = new SpannableString(mCustomStr);
/**
* 设置替换掉的文字 高亮显示 且支持跳转
*/
AppUtils.setKeyworkClickable(mContext, holder.mCustomStr, spannableString, Pattern.compile("#\\w+#"),
new TextViewClickSpan(new OnTextViewClickListener() {
@Override
public void setStyle(TextPaint ds) {
// 设置是否显示超链接的下划线
ds.setUnderlineText(false);
}
@Override
public void clickTextView() {
if (isHasClick()) {
return;
}
Intent intent = new Intent(mContext, TypeListActivity.class);
intent.putExtra(CATEGORY_ID, orderBean.getCategoryId());
intent.putExtra(CATEGORY_NAME, orderBean.getCategoryName());
mContext.startActivity(intent);
}
}));