Android Linkify - Clickable telephone numbers

自古美人都是妖i 提交于 2019-12-05 13:08:23

The autolink mask needs to include a search for phone numbers:

mContactDetailsText.setAutoLinkMask(Linkify.PHONE_NUMBERS);

Then you'll need to set the links to be clickable:

mContactDetailsText.setLinksClickable(true);

You might also need movement method set like so:

mContactDetailsText.setMovementMethod(LinkMovementMethod.getInstance())

You should be able to accomplish what you want with the other answers, but this will definitely work and will give you more control over the display of the text and what will happen when you click the number.

 String text = "T. ";
 StringBuilder stringBuilder = new StringBuilder(text);
 int phoneSpanStart = stringBuilder.length();
 String phoneNumber = "0123 4567890"
 stringBuilder.append(phoneNumber);
 int phoneSpanEnd = stringBuilder.length();

 ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + phoneNumber.replace(" ", "")));
                startActivity(intent); 
            }

            public void updateDrawState(TextPaint ds) {// override updateDrawState
                ds.setUnderlineText(false); // set to false to remove underline
                ds.setColor(Color.BLUE);
            }
        };
   SpannableString spannableString = new SpannableString(stringBuilder);
   spannableString.setSpan(clickableSpan, phoneSpanStart, phoneSpanEnd,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

 myTextView.setText(spannableString);
 myTextView.setMovementMethod(LinkMovementMethod.getInstance());

You need to set an onClickListener() on your TextViews. Then they will respond to clicks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!