Android的Linkify一个完全无关的字符串 [英] Android Linkify A Completely Unrelated String

查看:118
本文介绍了Android的Linkify一个完全无关的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要像做
 单击< A HREF ='www.google.com'>此处< / A>

在机器人。我尝试使用以下内容:

in android. I tried using the following:

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 [原文]

but i just end up with a link to www.google.comhere [sic]

推荐答案

只是为了回答我的问题,我发现变换滤镜教程,它适用于我自己的目的:

Just to answer my own question, I found a tutorial on transform filters and adapted it to my own ends:

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

我决定把整个文本进入最后一个大的环节,所以我做匹配整个文本的模式。

I decided I wanted to turn the whole text into one big link in the end, so I did a pattern that matched the whole text.

然后我创建了一个变换滤波器刚刚忽略不管你给它,并返回我想带人到网址

Then I created a transform filter which just ignores whatever you give it and returns the URL I wanted to take people to

该计划也必须是空串,所以没事就末尾添加可获得

The scheme also has to be the empty string so nothing gets added on the end

最后,我用linkify把它一起

Finally I used linkify to put it all together

这篇关于Android的Linkify一个完全无关的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆