TextView that is linkified and selectable?

跟風遠走 提交于 2019-11-30 00:06:38

Did you try to add this on your TextView xml code?

    <TextView
    ...
    android:autoLink="all"
    android:textIsSelectable="true" />

I've tried it on my code and i can make a call/surf on web/mail and also select all text.

The autoLink attribute has an annoying bug: if you click in your example on the phone number, then return back and click on the second url link - it will open the phone number again. This attribute works so bad with multiple links, that I have implemented my own class, here is the link on Github ClickableLinksTextView.java

In your example you can replace your TextView class by my ClickableLinksTextView class in the xml-layout and change the code like this:

ClickableLinksTextView textView = (ClickableLinksTextView)view.findViewById(R.id.mytext);
textView.setText("My text: +4412345678 Go to website: www.google.com Blah blah");
Linkify.addLinks(textView, Linkify.ALL);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    textView.setMovementMethod(ArrowKeyMovementMethod.getInstance());
    textView.setTextIsSelectable(true);
    // the autoLink attribute must be removed, if you hasn't set it then ok, otherwise call textView.setAutoLink(0);
}

The original cause of the issue in your question is that the LinkMovementMethod class and the textIsSelectable attribute are not compatible at all, even Android OS developers admit this in the Android OS source code.

Do not change the movement method for text that support text selection as it would prevent an arbitrary cursor displacement.

If the text view is selectable, the only 2 correct movement method values are null and ArrowKeyMovementMethod. That's why I set ArrowKeyMovementMethod explicitly in my example, because the Linkify.addLinks sets an incorrect movement method and I should revert it.

As to the bug of the autoLink attribute, it is because android developers haven't copied link detection properly. You can look at the code example in the answer of @cheng yang, the code just takes the first link no matter how many of them you have.

cheng yang

The problem is in Android's TextView. Calling Linkify.addLinks() would not change autolinkmask in TextView itself. I think it's an Android bug that they check mAutoLinkMask. So if you set android:autoLink in xml file, or call setAutoLinkMask() to a non 0 value, it will work.

FYI, TextView's source code:

            if (touchIsFinished && mLinksClickable && mAutoLinkMask != 0 && textIsSelectable) {
            // The LinkMovementMethod which should handle taps on links has not been installed
            // on non editable text that support text selection.
            // We reproduce its behavior here to open links for these.
            ClickableSpan[] links = ((Spannable) mText).getSpans(getSelectionStart(),
                    getSelectionEnd(), ClickableSpan.class);

            if (links.length > 0) {
                links[0].onClick(this);
                handled = true;
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!