Textview设置电话号码可点击

    xiaoxiao2022-07-03  107

    android:textColorLink="#01a1ef"//设置可点击文本颜色 android:autolink = "phone"//手机号可点击 /* * 设置textview的手机号可点击 * @param textView * @param textview设置的字符串 * */ private void setTextLink(TextView textView, String textString){ SpannableString ss = new SpannableString(textString); //判断是否为手机号 String regex = "\\d{7,18}|\\d{3,4}-\\d{7,18}|\\d{5,6}-\\d{3,6}|(\\d{3,6}-){2,3}\\d{3,6}"; final List<String> list = getAllSatisfyStr(textString,regex); if(list.size() > 0){ for (int i = 0 ; i < list.size();i++){ int start = textString.indexOf(list.get(i)); final int finalI = i; ss.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { showDialog("", list.get(finalI), "呼叫", "取消", new ConfirmDialog.CancelClickListener() { @Override public void doCancel() { dismissDialog(); } }, new ConfirmDialog.ConfirmClickListener() { @Override public void doConfirm() { Intent intent = new Intent(Intent.ACTION_DIAL); Uri data = Uri.parse("tel:" + list.get(finalI)); intent.setData(data); startActivity(intent); dismissDialog(); } }); } },start,start+(list.get(i).length()), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } textView.setText(ss); textView.setMovementMethod(LinkMovementMethod.getInstance()); } /* * 获取所有满足正则表达式的字符串 * @param textString 需要被获取的字符串 * @param regex 正则表达式 * @return 所有满足正则表达式的字符串 * */ private List<String> getAllSatisfyStr(String textString, String regex){ if (TextUtils.isEmpty(textString)) { return null; } ArrayList<String> allSatisfyStr = new ArrayList<>(); if (TextUtils.isEmpty(regex)) { allSatisfyStr.add(textString); return allSatisfyStr; } Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(textString); while (matcher.find()) { allSatisfyStr.add(matcher.group()); } return allSatisfyStr; }
    最新回复(0)