Android 顯示輸入法中的emoji表情以及String字符串轉(zhuǎn)碼

實現(xiàn)用原生鍵盤輸入表情并顯示,有兩種辦法:

  • 發(fā)送時將String字符串轉(zhuǎn)換為Unicode編碼字符串,顯示接收的列表時用Unicode編碼字符串轉(zhuǎn)化為String字符串
  • 發(fā)送時將String字符串轉(zhuǎn)換為UTF-8編碼字符串,顯示接收的列表時用UTF-8編碼字符串轉(zhuǎn)化為String字符串

將String字符串轉(zhuǎn)換為Unicode編碼字符串

只是將中文字符串和emoji表情轉(zhuǎn)換為十六進制Unicode編碼字符串:

    /**
     * 把中文字符串轉(zhuǎn)換為十六進制Unicode編碼字符串
     */
    public static String stringToUnicode(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            if (ch > 255)
                str += "\\u" + Integer.toHexString(ch);
            else
                str += "\\" + Integer.toHexString(ch);

        }
        return str;
    }

將中文、英文、數(shù)字字符串和emoji表情轉(zhuǎn)換為十六進制Unicode編碼字符串:

    /**
     * 把中文字符串轉(zhuǎn)換為十六進制Unicode編碼字符串
     */
    public static String stringToUnicode(String s) {
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            int ch = (int) s.charAt(i);
            if (ch > 255)
                str += "\\u" + Integer.toHexString(ch);
            else
            str += String.valueOf(s.charAt(i));
        }
        return str;
    }

將輸入的每個字符轉(zhuǎn)換為unicode編碼字符串

    /**
     * 字符串轉(zhuǎn)換unicode
     */
    public static String stringToUnicode(String string) {

        StringBuffer unicode = new StringBuffer();

        for (int i = 0; i < string.length(); i++) {

            // 取出每一個字符
            char c = string.charAt(i);

            // 轉(zhuǎn)換為unicode
            unicode.append("\\u" + Integer.toHexString(c));
        }

        return unicode.toString();
    }

將Unicode編碼字符串轉(zhuǎn)換為String字符串

使用正則表達式將Unicode編碼字符串轉(zhuǎn)換為string字符串(包括中文、英文、數(shù)字、emoji表情)

    /**
     * 把十六進制Unicode編碼字符串轉(zhuǎn)換為中文字符串
     */
    public static String unicodeToString(String str) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{2,4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch + "");
        }
        return str;
    }

將每個Unicode編碼字符串取出轉(zhuǎn)換為string字符串(包括中文、英文、數(shù)字、emoji表情)

    /**
     * unicode 轉(zhuǎn)字符串
     */
    public static String unicode2String(String unicode) {

        StringBuffer string = new StringBuffer();

        String[] hex = unicode.split("\\\\u");

        for (int i = 1; i < hex.length; i++) {

            // 轉(zhuǎn)換出每一個代碼點
            int data = Integer.parseInt(hex[i], 16);

            // 追加成string
            string.append((char) data);
        }

        return string.toString();
    }
    

將String字符串轉(zhuǎn)換為UTF-8編碼字符串

編碼:

    /**
     * 字符串換成UTF-8
     *
     * @param str
     * @return
     */
    public static String stringToUtf8(String str) {
        String result = null;
        try {
            result = URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

將UTF-8編碼字符串轉(zhuǎn)換為String字符串

解碼:

    /**
     * utf-8換成字符串
     *
     * @param str
     * @return
     */
    public static String utf8ToString(String str) {
        String result = null;
        try {
            result = URLDecoder.decode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容