【发布时间】:2016-02-25 11:25:22
【问题描述】:
我需要动态突出显示文本中的 url 并使其可点击。
为此,我使用以下方法
private SpannableString addClickablePart(String string) {
string = string.replaceAll("\\n"," \n ");
string += " ";
SpannableString ss = new SpannableString(string);
String[] words = string.split(" ");
for (final String word : words) {
if (CommonUtilities.isValidURL(word)) {
int lastIndex = 0;
while(lastIndex != -1){
lastIndex = string.indexOf(word+" ",lastIndex);
if(lastIndex != -1){
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
//use word here to make a decision
isRefreshingNecessary = false;
Intent mIntent = new Intent(ctx, UserWebsiteActivity.class);
mIntent.putExtra("website", word);
startActivity(mIntent);
}
};
ss.setSpan(clickableSpan, lastIndex, lastIndex + word.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
lastIndex += word.length();
}
}
}
}
return ss;
}
它适用于大多数情况。但是,不适用于以下示例的所有情况。
在您的计划条款中提供给您的定价信息和 关于这些数字范围的条件将不再适用,并将 取而代之的是这种充电结构。见 www.ee.co.uk/ukcalling 更多信息。
对于上述情况,当我使用
分割整个字符串时 String[] words = string.split(" ");
或
String[] words = string.split("\\s+");
我得到了See www.ee.co.uk/ukcalling for 一个字。相反,我需要将这 3 个 - See、www.ee.co.uk/ukcalling 和 for 作为 3 个不同的词,而不是作为单个词分组。
我无法理解用空间分割的方式有什么问题。 请帮我知道。
【问题讨论】:
-
为什么要投反对票?我提到了我尝试过的。请让我知道出了什么问题,这样我就可以改进,而不仅仅是投反对票。
-
为简单起见,您还可以将它们分配给 3 个不同的文本视图
-
我有动态字符串,所以我不能以静态方式处理它们。我将只有一个文本视图,其中需要突出显示 url 部分,只要存在@VivekMishra
-
投反对票的最有可能的原因是因为它不可重现...您能在 ideone.com 中重现该问题并分享链接吗?
-
请让我检查一下那个链接。