Android- getSpans() 总是返回一个长度为 0 的数组

标签 android spannablestring

这让我发疯:

String message = "Here is some example test with URLs from mattheworiordan.com.
    Any of the URLs which are prefixed with www. should become URLs such as www.mattheworiordan.com/path_name.html
    And an explicit URL such as http://www.mattheworiordan.com/ should become a URL.
    Emails such as matt@google.com should become links to.
    Or email mailto links such as mailto:matt@google.com should become links to.
    And of course lets not forget querstryings such as http://mattheworiordan.com/?test-param=true_or_false" 

SpannableString spannable = new SpannableString(message);
URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
Linkify.addLinks(spannable, Linkify.ALL);
Log.v("data_", "length:" + Integer.toString(spans.length));

URLSpan[] 数组的长度始终为 0。我在这里做错了什么以及如何解析 String 中的所有 url?

编辑:Html.fromHtml(message) 代替 message 参数返回相同的结果。

最佳答案

那是因为你没有任何跨度。在Html.fromHtml的情况下得到 URLSpan , 你必须使用标签 <a href .例如。

String message = "Here is some example test with URLs from mattheworiordan.com.
      Any of the URLs which are prefixed with www. should become URLs such as www.mattheworiordan.com/path_name.html
      And an explicit URL such as <a href='http://www.mattheworiordan.com/'>http://www.mattheworiordan.com/</a> should become a URL.
      Emails such as matt@google.com should become links to.
      Or email mailto links such as mailto:matt@google.com should become links to.
      And of course lets not forget querstryings such as http://mattheworiordan.com/?test-param=true_or_false" 

将产生 1 .如果你不想使用 Html , 那么你必须设置 URLSpan以编程方式

关于Android- getSpans() 总是返回一个长度为 0 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41163325/

相关文章:

android - 有什么方法可以用 spannable 替换 EditText 中的文本,但可以节省值(value)?

android - 如何更改 TextView 特定部分的颜色?

java - 如何跳出通过从另一个按钮单击一个按钮而启动的 for 循环

android - 覆盖 getText 的 EditText

Android 编辑文本 : how to apply spans when composing?

Android:避免多次打开对话框,但允许隐藏和显示相同的对话框

android - 可扩展字符串错误

java - 以编程方式添加多种 View 类型的 RecyclerView

java - 单击按钮时不重复 Java 中的单词 - Android 应用程序

android - 使用 Google 登录的 Azure 移动授权错误