【问题标题】:Add domain to a relative link将域添加到相对链接
【发布时间】:2014-04-30 21:45:47
【问题描述】:

我正在从我的服务器获取一些包含 HTML 的数据以在我的应用程序中使用,并且该 html 有一些链接,如 <a href="/go/here">Go here</a>

我正在将 html 应用到像

这样的 TextView
text.setText(Html.fromHtml(htmlString));
text.setMovementMethod(LinkMovementMethod.getInstance());

链接有效,但导致我的应用程序崩溃

No Activity found to handle Intent { act=android.intent.action.VIEW dat=/go/here...`

我猜崩溃是因为它是一个相对链接并且没有域。有没有任何方法,也许是正则表达式,来搜索所有 <a/> 标签并在它之前添加域?

【问题讨论】:

    标签: java android html


    【解决方案1】:

    我很想采用最简单的方法:

    final String faultyHtml = "Link: <a href=\"/images/nav_logo193.png\">click here</a> etc etc";
    final String domain = "http://www.google.fr";
    
    final String fixedHtml = faultyHtml.replace("href=\"/", "href=\"" + domain + "/");
    
    text.setText(Html.fromHtml(fixedHtml));
    text.setMovementMethod(LinkMovementMethod.getInstance());
    

    (不过,所有出现的事件都将添加到同一个域)

    【讨论】:

    • 我忘了放我的帖子,他们都属于同一个域,所以这是完美的:)
    【解决方案2】:
    int index = htmlString.indexOf("<a>");
    String part1 = htmlString.subString(0,index+2);
    String part2 = htmlString.subString(index+3);
    String newHtmlString = part1+"http://"+part2;
    

    【讨论】:

      【解决方案3】:

      你可以试试这样的代码。

      TextView tv = (TextView) findViewById(R.id.textView1);
          String text = "This is just a test. Click this link here <a href=\"http://www.google.com\">Google</a> to visit google.";
          tv.setMovementMethod(LinkMovementMethod.getInstance());
          tv.setText(Html.fromHtml(text));
      

      参考How to make links in textview clickable in android?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-20
        • 1970-01-01
        • 1970-01-01
        • 2016-01-15
        • 1970-01-01
        • 2013-04-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多