安卓:Linkify的TextView [英] Android: Linkify TextView
问题描述
我有TextView的,我需要linkify。下面是我在做什么。
I have textview which I need to linkify. Here's what I am doing..
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的安卓...更新的文字显示,但我面临两个问题。
The text-view is displayed correctly with the text "Android...Update from Android", but I am facing two problems.
1)我的文本字符串的字符串机器人的两个实例。因此,无论是文字的linkified。我想只有第一次出现被linkified。我应该怎么做呢?
1) My text string has two instances of the string "Android". So, both the text are linkified. I want only the first occurrence to be linkified. How should I go about it?
2)当我点击linkfy文本,它会打开浏览器,但是URL是怪异。它试图打开URL是www.comandroid。我不知道怎么回事错在这里做。在URL文本机器人将被替换。我是不是做错了什么Linknifying的文本时。
2) When I click the linkfy text, it opens the browser, but the URL is weird. The url it tries to open is "www.comandroid". I don't know whats going wrong here. The text "android" in the URL is being replaced. Am I doing something wrong when Linknifying the text.
任何帮助将是非常美联社preciated。
Any help will be highly appreciated.
推荐答案
我创造,这是否只是一个静态方法:
I created a static method that does this simply:
/**
* 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);
}
和运能利用它简单:
And the OP could use it simply:
addLink(text, "^Android", "http://www.android.com");
这篇关于安卓:Linkify的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!