这让我发疯:
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/