获取表情符号中的表情符号

问题描述 投票:1回答:4

我想找到用户输入EditText的emojis数量。如果用户只输入表情符号,并且使用3或更少,我希望能够在应用程序中使用更大的字体显示该字符串。

现在我确实遇到过这篇文章,它确实有助于检测字符串中是否存在表情符号,但我无法弄清楚如何计算表情符号的数量。

Detecting if a character in a String is an emoticon (using Android)

有谁知道如何从字符串中获取表情符号计数?

android android-edittext character emoji
4个回答
4
投票
    int emojiCount = 0;

    for (int i = 0; i < yourString.length(); i++) {
     int type = Character.getType(yourString.charAt(i));
      if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
       emojiCount++;
      }
    }

return emojiCount/2;

2
投票

另一种方法是利用EmojiCompat。此代码假设您在应用启动时初始化了EmojiCompat。这里的基本思想是让EmojiCompat处理你的CharSequence,在任何表情符号出现的任何地方插入EmojiSpan的实例,然后检查结果,返回处理后的EmojiSpan中的Spannable实例的计数。

public static int getEmojiCount(CharSequence charSequence) {
    int count = 0;
    CharSequence processed = EmojiCompat.get().process(charSequence, 0, charSequence.length() -1, Integer.MAX_VALUE, EmojiCompat.REPLACE_STRATEGY_ALL);
    if (processed instanceof Spannable) {
        Spannable spannable = (Spannable) processed;
        count = spannable.getSpans(0, spannable.length() - 1, EmojiSpan.class).length;
    }
    return count;
}

1
投票

我的方法是导入这个库:

implementation 'com.vdurmont:emoji-java:4.0.0'

然后我创建了一个实用工具方法来获取将emojis计为1的字符串的长度:

fun getLengthWithEmoji(s: String): Int{
        var emojiCount = EmojiParser.extractEmojis(s).size;
        var noEmojiString = EmojiParser.removeAllEmojis(s);
        var emojiAndStringCount = emojiCount + noEmojiString.length;
        return emojiAndStringCount;
}

一般来说,'以字符串形式获取表情符号计数'我会使用这一行:

var emojiCount = EmojiParser.extractEmojis(s).size;

这可以解释所有最新的表情符号(取决于你的图书库的最新版本)。检查其他人在库上做的一些分叉,因为它们在某些情况下添加了缺少的表情符号模式。


0
投票

试试这个

private TextWatcher textWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
        editText.post(new Runnable() {
            @Override
            public void run() {
                if (length < 100) {
                    if (count > 0 && after <= 0)/*remove emoij*/ {
                        Log.i("MainActivity", "emoij -> down length");
                        length--;
                    } else if (count > after)/*remove text*/ {
                        Log.i("MainActivity", "text -> down length");
                        length--;
                    } else if (count == 0 && after > 1)/*emoij*/ {
                        Log.i("MainActivity", "emoij -> increase");
                        ++length;
                    } else if (count == 0 && after == 1)/*Text*/ {
                        Log.i("MainActivity", "text -> increase");
                        ++length;
                    } else if (count > 0 && after > 1) {
                        Log.i("MainActivity", "text -> increase");
                        ++length;
                    }
                    if (s.length() <= 0)
                        length = 0;
                    Log.w("MainActivity", " Length: " + length);
                } else {
                    if (count > 0 && after <= 0)/*remove emoij*/ {
                        Log.i("MainActivity", "emoij -> down length");
                        length--;
                    } else if (count > after)/*remove text*/ {
                        Log.i("MainActivity", "text -> down length");
                        length--;
                    }
                     Log.w("MainActivity", " Length: " + length);
                }

                if (length == 100) {
                    editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(s.length())});
                } else {
                    editText.setFilters(new InputFilter[]{});
                }
            }
        });
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

`

© www.soinside.com 2019 - 2024. All rights reserved.