How can I display special characters (like –) in the TextView?

后端 未结 4 2045
再見小時候
再見小時候 2020-12-07 01:47

How can I display special characters(like – ") in a TextView?

相关标签:
4条回答
  • 2020-12-07 02:18

    If you know the Unicode value, you can display any UTF-8 character. Example, for " you would have &\#0034;.

    See Unicode Characters (at Code Table) for more information.

    0 讨论(0)
  • 2020-12-07 02:22

    I've written a custom method that will convert all unicode from hexa to integer and replaces from actual string. So that text view can read is as a unicode. have a look ,this will solve your problem...

    public String unecodeStr(String escapedString) {

        try {
            String str;
            int from = 0;
            int index = escapedString.indexOf("\\u", 0);
            while (index > -1) {
                str = escapedString.substring(index, index + 6).replace("\\u", "");
                try {
                    Integer iI = Integer.parseInt(str, 16);
                    char[] chaCha = Character.toChars(iI);
                    escapedString = escapedString.replaceFirst(str, String.valueOf(chaCha));
                } catch (Exception e) {
                    CustomLog.e("error:", e.getMessage());
                }
                from = index + 3;
                index = escapedString.indexOf("\\u", from);
            }
    
            escapedString = escapedString.replace("\\u", "");
        } catch (Exception e) {
            CustomLog.info("warnning", "emoji parsing error at " + escapedString);
        }
    
        return escapedString;
    }
    
    0 讨论(0)
  • 2020-12-07 02:41

    You can use the Html.fromHtml() to handle HTML formatted text into a Spannable that TextView can display.

    0 讨论(0)
  • 2020-12-07 02:41

    I've implemented this solution.

    Activity class:

    textView.setText( getString(R.string.author_quote, "To be or not to be", "Shakespeare") )
    

    strings.xml:

    <string name="author_quote">&#171; %1$s &#187; - %2$s</string>
    

    HTML chars are written directly in strings.xml, no additional Html.fromHtml() is needed. It works fine on all my devices.

    0 讨论(0)
提交回复
热议问题