/**
* 获得电话号码的正则表达式:包括固定电话和移动电话,和400电话
*
* @return 电话号码的正则表达式
*/
public static String isPhoneRegexp() {
String regexp = "";
//能满足最长匹配,但无法完成国家区域号和电话号码之间有空格的情况
String mobilePhoneRegexp = getMobilePhoneRegexp();
//固定电话正则表达式
String landlinePhoneRegexp = getLandlinePhoneRegexp();
//400电话正则表达式
String servicePhoneRegexp = getServicePhoneRegexp();
regexp += "(?:" + mobilePhoneRegexp + "|" + landlinePhoneRegexp + "|" + servicePhoneRegexp + ")";
return regexp;
}
/**
* 移动电话正则表达式
* 能满足最长匹配,但无法完成国家区域号和电话号码之间有空格的情况
* 86+‘-’+11位电话号码(包括XXX-XXXX-XXXX)
* 86+11位正常的电话号码(包括XXX-XXXX-XXXX)
* 11位正常电话号码(包括XXX-XXXX-XXXX)
* (+86) + 11位电话号码(包括XXX-XXXX-XXXX)
* (86) + 11位电话号码(包括XXX-XXXX-XXXX)
*
* @return
*/
public static String getMobilePhoneRegexp() {
String mobilePhoneRegexp = "(?:(\\(\\+?86\\))((13[0-9]{1})|(15[0-9]{1})|(170)|(18[0,5-9]{1}))+(\\-?[0-9]{4})+(\\-?[0-9]{4}))|" +
"(?:86-?((13[0-9]{1})|(15[0-9]{1})|(170)|(18[0,5-9]{1}))+(\\-?[0-9]{4})+(\\-?[0-9]{4}))|" +
"(?:((13[0-9]{1})|(15[0-9]{1})|(170)|(18[0,5-9]{1}))+(\\-?[0-9]{4})+(\\-?[0-9]{4}))";
return mobilePhoneRegexp;
}
/**
* 固定电话正则表达式
* 区号 + ‘-’ + 固定电话 + ‘-’ + 分机号
* 区号 + ‘-’ + 固定电话
* 区号 + 固定电话
*
* @return
*/
public static String getLandlinePhoneRegexp() {
String landlinePhoneRegexp = "(?:(\\(\\+?86\\))(0[0-9]{2,3}\\-?)+([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)|" +
"(?:(86-?)?(0[0-9]{2,3}\\-?)+([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)";
return landlinePhoneRegexp;
}
/**
* 400服务电话正则表达式
* 400-XXX-XXXX
* 400-XXXX-XXXX
* 400X-XXX-XXXX
*
* @return
*/
public static String getServicePhoneRegexp() {
String servicePhoneRegexp = "(?:((400)+(\\-?[0-9]{3})+([0-9]{1})?+(\\-?[0-9]{4})))|" +
"(?:((400)+([0-9]{1})?+(\\-?[0-9]{3})+(\\-?[0-9]{4})))";
return servicePhoneRegexp;
}
// 正则表达式,提取我们所有匹配的内容;
private static CharSequence checkPhoneText(Context context, String text) {
if (TextUtils.isEmpty(text)) {
return null;
}
String phone = text;
//获得固定电话和移动电话的正则表达式
String regexp = VZMobileNumberUtil.isPhoneRegexp();
SpannableString sp = new SpannableString(phone);
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(phone);
int start = 0;
//遍历取出字符串中所有的符合条件的;
while (matcher.find(start)) {
start = matcher.end();
sp.setSpan(new PhoneSpan(context, matcher.group()), matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (start >= text.length()) {
break;
}
}
SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
stringBuilder.append(sp);
return stringBuilder;
}
static class PhoneSpan extends ClickableSpan {
private String phone;
private Context context;
PhoneSpan(Context context, String phone) {
this.context = context;
this.phone = phone;
}
@Override
public void updateDrawState(TextPaint ds) {
ds.linkColor = Color.parseColor("#5297FC");
super.updateDrawState(ds);
}
//点击链接下划线,弹出dialog,提示提电话,或者发短信;
@Override
public void onClick(View widget) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
//textView中应用
txtContent.setMovementMethod(
TextViewFixTouchConsume.LocalLinkMovementMethod.getInstance()
);
txtContent.setText(checkPhoneText(context, text));