美文网首页
Android 显示 Emoji

Android 显示 Emoji

作者: DavyJones1 | 来源:发表于2017-06-13 16:13 被阅读28次
    device-2017-06-27-171817.png

    废话不多说、直接上代码。

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.text.method.LinkMovementMethod;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
    
        TextView myTextView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            myTextView = new TextView(this);
            myTextView.setMovementMethod((LinkMovementMethod.getInstance()));
            setEmojiToTextView();
            setContentView(myTextView);
        }
    
        /**
         * 1. Emoticons ( 1F601 - 1F64F )
         * 2. Dingbats ( 2702 - 27B0 )
         * 3. Transport and map symbols ( 1F680 - 1F6C0 )
         * 4. Enclosed characters ( 24C2 - 1F251 )
         * 5. Uncategorized
         * 6a. Additional emoticons ( 1F600 - 1F636 )
         * 6b. Additional transport and map symbols ( 1F681 - 1F6C5 )
         * 6c. Other additional symbols ( 1F30D - 1F567 )
         */
        private void setEmojiToTextView() {
            setEmojiToTextView(0x1F601, 0x1F64F);
            setEmojiToTextView(0x2702, 0x27B0);
            setEmojiToTextView(0x1F680, 0x1F6C0);
            setEmojiToTextView(0x1F681, 0x1F6C5);
            setEmojiToTextView(0x1F30D, 0x1F567);
    
            //setEmojiToTextView(0x24C2, 0x1F251);
        }
    
        private void setEmojiToTextView(int start, int end) {
            int[] unicodeJoy = new int[end - start + 1];
            for (int i = start ; i <= end ; i++) {
                unicodeJoy[i - start] = i;
            }
            String emojiString = getEmojiStringByUnicode(unicodeJoy);
            myTextView.append(emojiString);
            myTextView.append("\n\n\n");
        }
    
        private String getEmojiStringByUnicode(int[] unicode) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int code : unicode) {
                stringBuilder.append(Character.toChars(code));
            }
            return stringBuilder.toString();
        }
    }
    

    相关文章

      网友评论

          本文标题:Android 显示 Emoji

          本文链接:https://www.haomeiwen.com/subject/czndqxtx.html