无法在TextView中插入自定义链接
问题描述:
我正在尝试向TextView插入链接以使它们开始新的活动。我写了一个方法,我Utils
类做到这一点:无法在TextView中插入自定义链接
public static final String tagPattern = "#([^ ]+)";
public static final String tagReplace = "<a href=\"org.openihs.seendroid://display/tag/$1/\">#$1</a>";
public static final String userPattern = "@([^ ]+)";
public static final String userReplace = "<a href=\"org.openihs.seendroid://display/user/$1/\">@$1</a>";
static public void linkify(TextView view) {
String text = view.getText().toString();
text = text.replaceAll(Utils.tagPattern, Utils.tagReplace);
text = text.replaceAll(Utils.userPattern, Utils.userReplace);
view.setMovementMethod(LinkMovementMethod.getInstance());
view.setText(Html.fromHtml(text));
Log.d("SeenDroid", text);
Linkify.addLinks(view, Linkify.ALL);
}
一个例子输出(到logcat的)是:
foo <a href="org.openihs.seendroid://display/tag/bar/">#bar</a> baz http://google.fr/
而真正的UI给人http://google.fr/为纽带(预期的行为),但是#bar
作为普通文本(意外行为)。
TextView在ListView中。
任何想法来解决这个问题?
问候, ProgVal
答
Linkify的工作方式与您尝试使用它的方式有点不同。
public static final String AUTHORITY = "your_package.your_content_provider";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/tag/");
Pattern pattern = Pattern.compile(tagPattern);
Linkify.addLinks(view, pattern, CONTENT_URI.toString());
CONTENT_URI指的是您在清单中注册的ContentProvider。
答
不做内容替换。 Html.fromHtml
已经正确处理标签。
Html.fromHtml'如何知道我希望#tag和@user在用户单击它们时启动SearchTagActivity和ShowUserActivity? –
哦,等等。我忘了提供模式。第一篇文章编辑。 –