Android Linkify both web and @mentions all in the same TextView

前端 未结 6 1395
半阙折子戏
半阙折子戏 2020-12-02 13:59

Ok so I asked this yesterday:

AutoLink @mentions in a twitter client

I got my @mentions linking correctly. But in order to get it to work I had to take andro

6条回答
  •  攒了一身酷
    2020-12-02 14:16

    I had to Linkify.ALL before Linkify-ing the @ mentions for it to work.

    Also, do not use 'android:autoLink' in the TextView in your XML layout file

    TextView textView = (TextView) findViewById(R.id.tweet);
    
    Pattern atMentionPattern = Pattern.compile("@([A-Za-z0-9_]+)");
    String atMentionScheme = "http://twitter.com/";
    
    TransformFilter transformFilter = new TransformFilter() {
            //skip the first character to filter out '@'
            public String transformUrl(final Matcher match, String url) {
                    return match.group(1);
            }
    };
    
    Linkify.addLinks(textView, Linkify.ALL);
    Linkify.addLinks(textView, atMentionPattern, atMentionScheme, null, transformFilter);
    

    My TextView looks like this:

    
    

提交回复
热议问题