2

我想从中提取 URL hi there this is a URL String http://mytest.com

我尝试使用 EditText.getURLs 但它对我不起作用。

EditText.setText("hi there this is a URL String http://stackoverflow.com");
EditText.getURLs.toString();

如何从 EditText 获取 URL?

4

3 回答 3

14

这是功能:

//Pull all links from the body for easy retrieval
private ArrayList pullLinks(String text) {
   ArrayList links = new ArrayList();

   String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";
   Pattern p = Pattern.compile(regex);
   Matcher m = p.matcher(text);
   while(m.find()) {
      String urlStr = m.group();
      if (urlStr.startsWith("(") && urlStr.endsWith(")")) {
         urlStr = urlStr.substring(1, urlStr.length() - 1);
      }
      links.add(urlStr);
   }
   return links;
}
于 2013-09-05T07:43:27.873 回答
1

更改 EditText 的输入类型---- android:inputType="textUri"

并使用 --- String url=edittext.getText().toString(); 获取您的网址

于 2013-09-30T06:55:33.577 回答
0

检查这个库:(https://github.com/robinst/autolink-java

  compile "org.nibor.autolink:autolink:0.7.0"

在 Android 上运行良好。

例子:

  LinkExtractor linkExtractor = LinkExtractor.builder()
                    .linkTypes(EnumSet.of(LinkType.URL))
                    .build();
            Iterable<LinkSpan> links = linkExtractor.extractLinks(String_with_Link");
            LinkSpan link = links.iterator().next();
            link.getType();        // LinkType.URL
            link.getBeginIndex();  // 17
            link.getEndIndex();    // 32
            final_Url = String_with_Link.substring(link.getBeginIndex(), link.getEndIndex()); 
于 2017-09-11T09:08:15.380 回答