安卓TextView无法显示某些表情符号?

3

我有这样的一段文本。

"Hello world \u{1F603}"

"\u{1F603}" 这段文本应该像表情符号一样显示,但在 Textview 或 AppCompatTextview 中无法显示。有些表情符号可以像心形那样显示,但笑脸或生气的表情无法显示。

我的项目中有很多 Textview。我如何以最简单的方式解决这个问题。

感谢任何建议。


https://dev59.com/I2kw5IYBdhLWcg3wp8Sc - Ayush Khare
你可以使用emojicon库或创建CustomTextView。 - Pallavi Tapkir
3个回答

3

支持库中的Emoji兼容性

build.gradle

ext {
  version = '27.1.1'
}
dependencies {
    ...
    implementation  "com.android.support:support-emoji-bundled:$version"
} 

我的应用程序

public class MyApplication  extends Application{

    @Override
    public void onCreate() {
        super.onCreate();
        EmojiCompat.init(new BundledEmojiCompatConfig(getApplicationContext()));
    }
}

Manifest.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:name=".MyApplication" // reference to my application for init needed.
    android:theme="@style/AppTheme">

  // other activities definition.. 
</application>

如何将 Emoji 设置到我的 TextViewAppCompatTextview

    // Regular TextView without EmojiCompat support; you have to manually process the text
    final TextView regularTextView = findViewById(R.id.textview);
    EmojiCompat.get().registerInitCallback(new InitCallback(regularTextView));

InitCallback

private static class InitCallback extends EmojiCompat.InitCallback {

    private final WeakReference<TextView> mRegularTextViewRef;

    InitCallback(TextView regularTextView) {
        mRegularTextViewRef = new WeakReference<>(regularTextView);
    }

    @Override
    public void onInitialized() {
        final TextView regularTextView = mRegularTextViewRef.get();
        if (regularTextView != null) {
            final EmojiCompat compat = EmojiCompat.get();
            regularTextView.setText(compat.process("Neutral face \uD83D\uDE10"));
        }
    }

}

1

-1

试试这个

StringEscapeUtils.unescapeJava(//your string with emoji//)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接