1

我有一个自动链接设置为的 TextView

<TextView
   android:id="@+id/messageDetail_privateText_txt"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:autoLink="web|phone|email" />

但是,当我使用http://www.test.com?p1=v1&p2=v2之类的 url 设置文本时,TextView 的自动链接无法识别域之后的查询参数。

我可以理解这种 URL 没有太多意义,但是有没有解决这个问题的方法?

iOS 可以很好地识别参数。

4

1 回答 1

0

回答我自己的问题,最终对我有用的是检查字符串的 url 并手动添加斜杠。不是世界上最酷的解决方案,但在这种情况下有效。

代码下方:

protected String normalizeURLs(String html)
{

    String[] pieces = html.split(" ");
    ArrayList<String> textParts = new ArrayList<>();

    for(String piece : pieces) {

        try {
            URL isURL = new URL(piece);
            String protocol = isURL.getProtocol();
            String host = isURL.getHost();
            String query = isURL.getQuery();
            String path = isURL.getPath();
            String questionMark = "?";

            if (path.equals("")) {
                path = "/";
            }

            if (query == null) {
                query = "";
                questionMark = "";
            }

            String url = protocol + "://" + host + path + questionMark + query;
            textParts.add(url);
        } catch (MalformedURLException exception) {
            textParts.add(piece);
        }
    }

    String resultString = "";

    for (String s : textParts)
    {
        resultString += s + " ";
    }

    return resultString;
}
于 2016-03-31T10:16:28.557 回答