When user input emoji into EditText, it's count like 2 symbols. How to count emoji like 1 symbol?

huangapple go评论69阅读模式
英文:

When user input emoji into EditText, it's count like 2 symbols. How to count emoji like 1 symbol?

问题

我有一个来自Google Material的InputEditText。当我从软键盘输入表情符号时,计数器上显示的是每个表情符号的2个符号,而不是1个符号。如何避免这种情况,并将表情符号计算为1个符号?

我尝试在方法或文档中找到一些信息,但是没有找到任何内容。

以下是要翻译的代码部分:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/optionalInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/padding_default_vertical"
    android:focusable="true"
    android:hint="@string/spend_visit_note"
    app:counterEnabled="false"
    app:counterMaxLength="100"
    app:errorEnabled="true"
    app:hintTextAppearance="@style/TextAppearance.AppCompat.Medium.Inverse">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/optionalEditText"
        style="@style/AppTheme.Widget.EditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionDone"
        android:inputType="textNoSuggestions"
        android:maxLength="101"
        android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
optionalEditText
    .afterTextChangeEvents()
    .skipInitialValue()
    .throttleFirst(100, TimeUnit.MICROSECONDS)
    .map {
        Action.OptionalEdited(
            it.editable?.toString() ?: String.empty
        )
    }
    .bindToUiActions()
英文:

I have InputEditText from Google Material. And when I enter emoji from soft keyboard, on counter instead of 1 symbol shows 2 symbols per emoji. How to avoid it and count emoji like 1 symbol?

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/optionalInputLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/padding_default_vertical"
    android:focusable="true"
    android:hint="@string/spend_visit_note"
    app:counterEnabled="false"
    app:counterMaxLength="100"
    app:errorEnabled="true"
    app:hintTextAppearance="@style/TextAppearance.AppCompat.Medium.Inverse">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/optionalEditText"
        style="@style/AppTheme.Widget.EditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionDone"
        android:inputType="textNoSuggestions"
        android:maxLength="101"
        android:singleLine="true" />
</com.google.android.material.textfield.TextInputLayout>
optionalEditText
    .afterTextChangeEvents()
    .skipInitialValue()
    .throttleFirst(100, TimeUnit.MICROSECONDS)
    .map {
        Action.OptionalEdited(
            it.editable?.toString() ?: String.empty
        )
    }
    .bindToUiActions()

I try to find smth in methods or documentation, but I found nothing

答案1

得分: 0

你遇到的问题与表情符号在输入框中的表示方式有关。表情符号,特别是较新的表情符号,可能由多个Unicode字符组成,这可能导致字符计数不一致。为了将表情符号视为单个字符,你需要考虑Unicode字符长度的变化。

为了处理这个问题,你可以使用java.text包中的breakIterator类,它可以帮助你正确计算表情符号的字符数。

下面是一个代码片段,展示了如何修改你现有的代码以将表情符号计算为单个字符:

import java.text.BreakIterator;

// 在你现有的代码中:
optionalEditText
    .afterTextChangeEvents()
    .skipInitialValue()
    .throttleFirst(100, TimeUnit.MICROSECONDS)
    .map {
        val inputText = it.editable?.toString() ?: String.empty
        val emojiCount = countEmojis(inputText)
        Action.OptionalEdited(inputText, emojiCount)
    }
    .bindToUiActions()

// 计算表情符号为单个字符的函数
private fun countEmojis(inputText: String): Int {
    var count = 0
    val breakIterator = BreakIterator.getCharacterInstance()

    breakIterator.setText(inputText)
    var prevIndex = 0
    var index = breakIterator.first()

    while (index != BreakIterator.DONE) {
        val char = inputText.substring(prevIndex, index)
        if (char.isEmoji()) {
            count++
        }
        prevIndex = index
        index = breakIterator.next()
    }

    return count
}

// 扩展函数,用于检查字符串是否为表情符号
private fun String.isEmoji(): Boolean {
    return this.codePointCount(0, this.length) == 1 && this.matches(Regex("\\p{So}"))
}

这段代码引入了一个countEmojis函数,它使用BreakIterator来正确计算表情符号的字符数。isEmoji扩展函数用于确定一个字符串是否表示一个表情符号。

通过使用这种方法,你应该能够在TextInputLayout中看到正确的字符计数。请注意,如果需要,你可能需要根据你的具体Android/Kotlin环境进行代码调整。

英文:

The issue you're encountering is related to how emojis are represented in the input field. Emojis, especially the newer ones, can be made up of multiple Unicode characters, which can lead to discrepancies in character counting. To count emojis as a single character, you need to take into account the variation in Unicode character lengths.

To handle this, you can use the breakIterator class from java.text package, which can help you get the correct character count even for emojis.

Here's a code snippet that shows how you can modify your existing code to count emojis as single characters:

import java.text.BreakIterator;

// Inside your existing code:
optionalEditText
    .afterTextChangeEvents()
    .skipInitialValue()
    .throttleFirst(100, TimeUnit.MICROSECONDS)
    .map {
        val inputText = it.editable?.toString() ?: String.empty
        val emojiCount = countEmojis(inputText)
        Action.OptionalEdited(inputText, emojiCount)
    }
    .bindToUiActions()

// Function to count emojis as single characters
private fun countEmojis(inputText: String): Int {
    var count = 0
    val breakIterator = BreakIterator.getCharacterInstance()

    breakIterator.setText(inputText)
    var prevIndex = 0
    var index = breakIterator.first()

    while (index != BreakIterator.DONE) {
        val char = inputText.substring(prevIndex, index)
        if (char.isEmoji()) {
            count++
        }
        prevIndex = index
        index = breakIterator.next()
    }

    return count
}

// Extension function to check if a string is an emoji
private fun String.isEmoji(): Boolean {
    return this.codePointCount(0, this.length) == 1 && this.matches(Regex("\\p{So}"))
}

This code introduces a countEmojis function that uses BreakIterator to properly count emojis as single characters. The isEmoji extension function is used to determine whether a string represents an emoji.

By using this approach, you should now see the correct character count in the TextInputLayout. Note that you may need to adapt the code to your specific Android/Kotlin environment if needed.

huangapple
  • 本文由 发表于 2023年7月27日 15:31:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定