【问题标题】:Android webview not displaying '%' characterAndroid webview不显示'%'字符
【发布时间】:2011-08-31 19:55:53
【问题描述】:

我有一个WebView,使用以下代码:

WebView webView = new WebView(cont);
webView.loadData("Red 20%", "text/html", "utf-8");

显示字符串有问题。但是,如果我从字符串中删除 '%' 字符,它会正确显示。代码有什么问题?如何在WebView 中显示“%”?

【问题讨论】:

    标签: android html webview loaddata


    【解决方案1】:

    简单:

    WebView webView = new WebView(cont);
    webView.loadData("Red 20%", "text/html", "utf-8");
    

    您可以在此处查看特殊字符: http://www.degraeve.com/reference/specialcharacters.php

    【讨论】:

      【解决方案2】:

      URL 编码%

      20%25 应该可以解决问题

      【讨论】:

        【解决方案3】:

        更简单的替代方法是使用TextUtils.htmlEncode() 来显示您要显示的字符串。

        WebView webView = new WebView(cont);
        String s = TextUtils.htmlEncode("Red 20%");
        webView.loadData(s, "text/html", "utf-8");
        

        【讨论】:

        • htmlEncode 无法对其进行编码,以便在 WebView 中显示。但我需要一种可以转换所有此类字符的自动化方法。编码。
        【解决方案4】:

        而不是%,您必须使用它的等价物才能在网络中显示它。实际上它是&#37,因此您的代码应该更改为

        webView.loadData("Red 20%", "text/html", "utf-8"); 
        

        【讨论】:

          【解决方案5】:

          您可以替换“红色 20%”->“红色 20%”

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-05-19
            • 2012-11-16
            • 2017-02-07
            • 1970-01-01
            • 2016-12-19
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多