我可以用Linkify后更改TextView的链接文字? [英] Can I change TextView link text after using Linkify?

查看:182
本文介绍了我可以用Linkify后更改TextView的链接文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时,可以使用Linkify创建链接后更改TextView的文本?我有一些我想要的网址有两个领域,一个名称和ID,但我只是想显示的名称的文本。

Is is possible to change TextView text after using Linkify to create links? I have something where I want the url to have two fields, a name and id, but then I just want the text to display the name.

所以,我与文字的TextView,包括姓名和身份证开始,并linkify创造与这两个领域的相应链接。但是对于显示器,我不想显示的ID。

So I start off with a textview with text that includes both name and id, and linkify to create the appropriate links with both fields. But for the display, I don't want to show the id.

这可能吗?

推荐答案

这是一种痛苦,但肯定的。所以Linkify基本上做了几件事情。首先它扫描的TextView的匹配一个URL字符串的内容。接着,它会创建UrlSpan的和ForegroundColorSpan的那些与之相匹配的部分。然后,它设置的TextView的MovementMethod。

It's kind of a pain but yes. So Linkify basically does a few things. First it scans the contents of the textview for strings that match that of a url. Next it creates UrlSpan's and ForegroundColorSpan's for those sections that match it. Then it sets the MovementMethod of the TextView.

这里最重要的部分是UrlSpan的。如果你把你的TextView和调用的getText(),注意它返回的CharSequence。这是最有可能的某种跨越式的。从跨区你可以问,getSpans()和比较特别的UrlSpans。一旦你知道了所有的跨度可以超过遍历列表,找到并使用新的跨越对象替换旧的跨度对象。

The important part here are the UrlSpan's. If you take your TextView and call getText(), notice it returns a CharSequence. It's most likely some sort of Spanned. From the Spanned you can ask, getSpans() and specifcally the UrlSpans. Once you know all those spans you can than loop through the list and find and replace the old span objects with your new span objects.

mTextView.setText(someString, TextView.BufferType.SPANNABLE);
if(Linkify.addLinks(mTextView, Linkify.ALL)) {
 //You can use a SpannableStringBuilder here if you are going to
 // manipulate the displayable text too. However if not this should be fine.
 Spannable spannable = (Spannable) mTextView.getText();
 // Now we go through all the urls that were setup and recreate them with
 // with the custom data on the url.
 URLSpan[] spans = spannable.getSpans(0, spannable.length, URLSpan.class);
 for (URLSpan span : spans) {
   // If you do manipulate the displayable text, like by removing the id
   // from it or what not, be sure to keep track of the start and ends
   // because they will obviously change.
   // In which case you may have to update the ForegroundColorSpan's as well
   // depending on the flags used
   int start = spannable.getSpanStart(span);
   int end = spannable.getSpanEnd(span);
   int flags = spannable.getSpanFlags(span);
   spannable.removeSpan(span);
   // Create your new real url with the parameter you want on it.
   URLSpan myUrlSpan = new URLSpan(Uri.parse(span.getUrl).addQueryParam("foo", "bar");
   spannable.setSpan(myUrlSpan, start, end, flags);
 }
 mTextView.setText(spannable);
}

希望这是有道理的。 Linkify仅仅是一个很好的工具,以建立正确的跨度。跨度刚刚获得跨preTED渲染文本时。

Hopefully that makes sense. Linkify is just a nice tool to setup the correct Spans. Spans just get interpreted when rendering text.

这篇关于我可以用Linkify后更改TextView的链接文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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