的TextView与多个超链接
我具有例如以下字符串的TextView与多个超链接
\ N 3门下来是一个http://www.last.fm/tag/post-grunge \ “类= \” bbcode_tag \ (吉他),Matt Roberts(吉他),Todd Harrell(贝司),Chris Henderson(吉他)组成的1996年成立的美国密西西比州Escatawpa的后摇滚乐队, ,以及Greg Upchurch(鼓乐队)。乐队于2000年与Universal Records签约,为他们的第一张专辑,http://www.last.fm/music/3+Doors+Down/The+Better+Life \“class = \” bbcode_album \“>更美好的生活。他们获得了国际上的关注,发行了单曲" http://www.last.fm/music/3+Doors+Down/_/Kryptonite \“class = \”bbcode_track \“> Kryptonite "。专辑接着发行出售超过600万份。\ n \ n http://www.last.fm/music/3+Doors+Down\">在Last.fm上阅读关于3门的更多内容\ n \ n \ n用户提供的文本是根据Creative Commons by-SA许可证提供的,也可以在GNU FDL下提供。\ n
我想在超文本链接中点击显示整个字符串。我也不想看到实际的网址,只是显示网址的文字。关于这个问题的阅读其他职位,他们都建议定义与此类似
<TextView
android:id="@+id/tvArtistOverview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:autoLink="web"
android:linksClickable="true" />
一个TextView和TextView中的SetMovementMethod设置为
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
当我按照这些步骤,我的链接是可以点击的,但他们没有按照我的意愿显示。我错过了什么?
下面是它目前的样子。
使用下面的代码。
TextView tv = ....
tv.setMovementMethod(LinkMovementMethod.getInstance());
String content = tv.getText().toString();
List<String> links = new ArrayList<String>();
Pattern p = Patterns.WEB_URL;
Matcher m = p.matcher(content);
while (m.find()) {
String urlStr = m.group();
links.add(urlStr);
}
SpannableString f = new SpannableString(content);
for (int i = 0; i < links.size(); i++) {
final String url = links.get(i);
f.setSpan(new InternalURLSpan(new OnClickListener() {
public void onClick(View v) {
Context ctx = v.getContext();
String urlToOpen = url;
if (!urlToOpen.startsWith("http://") || !urlToOpen.startsWith("https://"))
urlToOpen = "http://" + urlToOpen;
openURLInBrowser(urlToOpen, ctx);
}
}), content.indexOf(url), content.indexOf(url) + url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.setText(f);
不幸的是,似乎并没有从超链接中清除网址。它们仍然以内联方式显示。 InternalURLSpan是ClickableSpan的一个扩展类,除了事件处理程序覆盖以外没有其他任何内容? – Redshirt 2013-03-11 05:47:19