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

时间:2017-07-21 15:10:34

标签: android android-edittext character emoji

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

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

https://docs.microsoft.com/en-us/azure/cosmos-db/partition-data

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

5 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;
}

答案 2 :(得分:0)

我的处理方法是导入该库:

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

然后,我创建了一个实用程序方法来获取将表情符号计数为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;

这说明了所有最新的表情符号(取决于您的图书馆的最新信息)。检查其他人在库中创建的一些fork,因为在某些情况下它们添加了缺少的表情符号模式。

答案 3 :(得分: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) {

    }
};

`

答案 4 :(得分:0)

对我来说最好的方法是codePointCount method

例如,如果文本值为“?”,则此方法返回1:

    fun getLengthWithEmoji(name: String): Int {
        return name.codePointCount(0, name.length)
    }