Android Linkify一个完全不相关的字符串

时间:2011-03-09 16:39:40

标签: android linkify

我想做点什么  Click <a href='www.google.com'>Here</a>

在android中。我尝试使用以下内容:

TextView sponsoredlink = new TextView(this);
                sponsoredlink.setId(R.id.details_sponsored_link);
                sponsoredlink.setText("Click Here");

                Pattern pattern =Pattern.compile("Here");

                String link = "www.google.com";

                Linkify.addLinks(sponsoredlink, pattern, link);

但我最终得到了www.google.comhere [sic]

的链接

1 个答案:

答案 0 :(得分:2)

为了回答我自己的问题,我找到了一个关于转换过滤器的教程,并将其改编为我自己的目的:

TextView sponsoredlink = new TextView(this);
                sponsoredlink.setId(R.id.details_sponsoredlink);
                sponsoredlink.setText("Click Here");

                TransformFilter mentionFilter = new TransformFilter() {
                    public final String transformUrl(final Matcher match, String url) {
                        return new String("http://www.google.com");
                    }
                };

                // Match @mentions and capture just the username portion of the text.
                Pattern pattern = Pattern.compile(".");
                String scheme = "";
                Linkify.addLinks(sponsoredlink, pattern, scheme, null, mentionFilter);

我决定最后将整个文本转换为一个大链接,所以我做了一个与整个文本相匹配的模式。

然后我创建了一个变换过滤器,它忽略了你给它的任何东西并返回我想带人去的URL

该方案也必须是空字符串,以便在最后添加任何内容

最后我使用了linkify将它们放在一起

相关问题