0

我想做类似的事情 Click <a href='www.google.com'>Here</a>

在安卓。我尝试使用以下内容:

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 [原文如此] 的链接

4

1 回答 1

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把它们放在一起

于 2011-04-05T17:43:47.150 回答