31

我有需要链接的文本视图。这就是我在做什么..

TextView text = (TextView) view.findViewById(R.id.name);
text.setText("Android...Update from Android");
Pattern pattern = Pattern.compile("Android");
String scheme = "www.android.com";
Linkify.addLinks(text, pattern, scheme);

文本视图与文本“Android...Update from Android”正确显示,但我面临两个问题。

1)我的文本字符串有两个字符串“Android”的实例。因此,两个文本都是链接的。我只希望第一次出现被链接。我应该怎么做?

2)当我单击链接文本时,它会打开浏览器,但 URL 很奇怪。它试图打开的网址是“www.comandroid”。我不知道这里出了什么问题。正在替换 URL 中的文本“android”。链接文本时我做错了什么吗?

任何帮助将不胜感激。

4

9 回答 9

25

The easy way is put autoLink in XML layout.

  android:autoLink="web" 
于 2012-11-19T16:09:55.413 回答
24

我创建了一个简单的静态方法:

/**
 * Add a link to the TextView which is given.
 * 
 * @param textView the field containing the text
 * @param patternToMatch a regex pattern to put a link around
 * @param link the link to add
 */
public static void addLink(TextView textView, String patternToMatch,
        final String link) {
    Linkify.TransformFilter filter = new Linkify.TransformFilter() {
        @Override public String transformUrl(Matcher match, String url) {
            return link;
        }
    };
    Linkify.addLinks(textView, Pattern.compile(patternToMatch), null, null,
            filter);
}

OP可以简单地使用它:

addLink(text, "^Android", "http://www.android.com");
于 2011-04-26T18:09:43.077 回答
9

我对 Linkify 没有太多经验。到目前为止,我想做的只是将文本中的 url 变成链接,由不同版本的addLinks.

但是,您可以通过调整正则表达式将您用于匹配“Android”的正则表达式更改为仅匹配开始字符串的那个:

Pattern pattern = Pattern.compile("^Android");

注意添加了“^”。这告诉正则表达式匹配器仅在开始字符串时匹配模式“Android”。如果这适用于您的琴弦,那就太好了。如果您有其他情况是您要链接的单词不在行首,您将需要更多地探索正则表达式。我推荐这个网站来了解更多正则表达式信息

于 2011-01-20T11:24:44.413 回答
9

如果你想打开所有包含在文本中的 url,那么你可以这样做..

            TextView textview=(TextView)findViewById(R.id.text);

    textview.setText("Android....Update from android...www.android.com");
    Linkify.addLinks(textview, Linkify.WEB_URLS);
    textview.setMovementMethod(LinkMovementMethod.getInstance());

它可能适用于所有网址。

于 2011-01-20T11:24:52.170 回答
5

从 Shawns 的回答和 SpunkerBaba 的评论中,我得出了这个助手类。这停止了​​将您正在更改的内容添加到您的 url 的双重附加问题。

package com.your.package;

public class Linkify {

    /**
     * @param textView
     *            textView who's text you want to change
     * @param linkThis
     *            a regex of what text to turn into a link
     * @param toThis
     *            the url you want to send them to
     */
    public static void addLinks(TextView textView, String linkThis, String toThis) {
        Pattern pattern = Pattern.compile(linkThis);
        String scheme = toThis;
        android.text.util.Linkify.addLinks(textView, pattern, scheme, new MatchFilter() {
            @Override
            public boolean acceptMatch(CharSequence s, int start, int end) {
                return true;
            }
        }, new TransformFilter() {

            @Override
            public String transformUrl(Matcher match, String url) {
                return "";
            }
        });
    }

}

用途:

 Linkify.addLinks(profileTextView, "BlundellApps.com", "http://www.BlundellApps.com");
于 2012-11-03T18:25:36.100 回答
3

我认为你真正需要的是简单的东西:

final TextView textView = ((BodyViewHolder) holder).textView;
textView.setText(text);
Linkify.addLinks(textView, Linkify.WEB_URLS); // I would do this only during view creation, btw.

您正在专门跟踪一个 URL。有些人建议使用 Linkigy.ALL,但你不希望这样:

public static final int ALL = WEB_URLS | EMAIL_ADDRESSES | PHONE_NUMBERS | MAP_ADDRESSES;

此外,其他人建议添加:

textview.setMovementMethod(LinkMovementMethod.getInstance());

添加链接规则后,但addLinks已经在其内部这样做了;-)

干杯!

于 2016-06-01T15:45:39.493 回答
2

问题解决了。第一个问题的解决方案是 Ian Leslie 提供的答案。第二个问题的解决方案如下。API“L​​inkify.addLinks”的行为是,它将要链接的字符串附加到url的末尾。例如……如果您想将文本“Android”与“www.android.com”链接起来。最终 URL 是“www.android.comAndroid”,这不是我需要的。所以我使用了

public static final void addLinks (TextView text, Pattern p, String scheme, Linkify.MatchFilter matchFilter, Linkify.TransformFilter transformFilter)
TransformFilter transformFilter = new TransformFilter() {
public final String transformUrl(final Matcher match, String url) { 
return ""; 
} }; 

api transformFilter 返回一个空字符串。所以我的最终网址是“www.android.com”

于 2012-11-20T05:48:53.223 回答
1

对于带有 lambda 表达式的 Koltin,它会是这样的

val pattern = Pattern.compile(link.title)
Linkify.addLinks(view, pattern, null, null, { matcher, url -> link.href })
于 2016-10-04T09:23:57.847 回答
1
TextView noteView = (TextView) findViewById(R.id.noteview);
noteView.setText(someContent);
Linkify.addLinks(noteView, Linkify.ALL);
于 2015-09-10T10:27:07.177 回答