TextView that is linkified and selectable?

前端 未结 3 966
醉酒成梦
醉酒成梦 2020-12-24 12:50

I would like to have a TextView that is both selectable and linkified. When I do both I end up with selectable text but links can\'t be clicked.

EDIT:

3条回答
  •  悲&欢浪女
    2020-12-24 13:27

    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;
                }
            }
    

提交回复
热议问题