2016-12-15 7 views
1

Это было меня с ума:Android- getSpans() всегда возвращает массив длины 0

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 [email protected] should become links to. 
    Or email mailto links such as mailto:[email protected] 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. Что я делаю неправильно здесь и как можно Я разбираю все URL-адреса в строке?

Редактировать: Html.fromHtml (сообщение) вместо параметра message возвращает тот же результат.

+0

это просто текст, у вас нет какого-либо промежутка в тексте – Blackbelt

ответ

2

Это потому, что у вас нет пролетов. В случае 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 [email protected] should become links to. 
     Or email mailto links such as mailto:[email protected] should become links to. 
     And of course lets not forget querstryings such as http://mattheworiordan.com/?test-param=true_or_false" 

даст 1. Если вы не хотите использовать Html, вам необходимо будет установить URLSpan s программно

0

Простая ошибка. Попробуйте позвонить Linkify.addLinks до, вызвав getSpans.

+0

Пожалуйста, ответьте на какой-нибудь код. –