Get Emoji Count In String

て烟熏妆下的殇ゞ 提交于 2019-12-02 13:59:44

问题


I would like to find how many emojis the user has input into an EditText. If the user only enters emojis, and uses 3 or less, I want to be able to display that string within the app with a larger font.

Right now I did come across this post which does help detect if emojis are present in the string, but I have not been able to figure out how to count the number of emojis.

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

Does anyone know how I can get the emoji count from a String?


回答1:


    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:


Another approach would be to take advantage of EmojiCompat. This code presumes you initialized EmojiCompat when your app was starting up. The basic idea here is to have EmojiCompat process your CharSequence, inserting instances of EmojiSpan wherever any emoji appear, and then examine the results, returning a count of the EmojiSpan instances in the processed 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;
}

Don't forget to add dependency in app gradle:

implementation 'androidx.emoji:emoji:28.0.0'



回答3:


My approach to this was to import this library:

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

Then I created a utility method to get the length of a string counting emojis as 1:

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

Generally to 'Get emoji count in string' I would use this line:

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

This accounts for all the latest emojis (depending on how up to date your library it). Check for some of the forks that others have made on the library as they in some cases have added missing emoji patterns.




回答4:


try this

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) {

    }
};

`



来源:https://stackoverflow.com/questions/45241081/get-emoji-count-in-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!