经过许多小时……</p>
这是我的解决方案。我把这段代码放在RecyclerView
adapter's
onBindViewHolder()
.
// replace url links with clickable link that says "Click here" (or "Haz clic aquí" in Spanish).
// link color is set in TextView in the xml.
// SpannableStringBuilder is mutable, so we can replace the link.
SpannableStringBuilder spannableStringBuilder = new
SpannableStringBuilder(newsFeedItem.getBody());
// use Linkify to automatically set all Url's in the string.
Linkify.addLinks(spannableStringBuilder,Linkify.WEB_URLS);
//do this process for each Url
for (URLSpan urlSpan: spannableStringBuilder.getSpans(0,spannableStringBuilder.length(),URLSpan.class)){
int start = spannableStringBuilder.getSpanStart(urlSpan);
int end = spannableStringBuilder.getSpanEnd(urlSpan);
// put whatever you want it to say into the next line where I wrote "Click here".
SpannableString customLinkSpannableString = new SpannableString("Click here");
customLinkSpannableString.setSpan(urlSpan,0, customLinkSpannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableStringBuilder.replace(start, end, customLinkSpannableString);
}
// now set the fixed up string into the TextView and set LinkMovementMethod.
textView.setText(spannableStringBuilder);
textView.setMovementMethod(LinkMovementMethod.getInstance());