讲一下主要思路,通过手机号正则匹配循环找出所有的字符串,并循环通过SpannableString给目标字符串设置样式和点击事件
核心代码:
private void setNote(String note, TextView textView) {
if (TextUtils.isEmpty(note)) {
return;
}
SpannableString spannableString = new SpannableString(note);
String strNote = note;
String phoneNum = StringUtils.getPhoneNum(strNote);
while (!TextUtils.isEmpty(phoneNum)) {
setClick(spannableString, phoneNum, textView, note);
strNote = strNote.substring(strNote.indexOf(phoneNum) + 12);
phoneNum = StringUtils.getPhoneNum(strNote);
}
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
private void setClick(SpannableString spannableString, String phoneNum, TextView textView,
String note) {
spannableString.setSpan(new ClickableSpan() {
@Override public void updateDrawState(@NonNull TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(getResources().getColor(R.color.colorNumberMarkPress)); //设置电话号码字体颜色
ds.setUnderlineText(false); //设置电话号码下划线
}
@Override public void onClick(@NonNull View widget) {
//电话号码点击事件
outcallPop.show(phoneNum);
}
}, note.indexOf(phoneNum), note.indexOf(phoneNum) + 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(
new ForegroundColorSpan(getResources().getColor(R.color.colorNumberMarkPress)),
note.indexOf(phoneNum), note.indexOf(phoneNum) + 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
然后是正则判断是否是手机号:
private static final String REGEX_MOBILE = "^[1][3|4|5|6|7|8|9][0-9]{9}";
public s