TextView that is linkified and selectable?

前端 未结 3 963
醉酒成梦
醉酒成梦 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:49

    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.

提交回复
热议问题