我有这样的一段文本。
"Hello world \u{1F603}"
。
"\u{1F603}" 这段文本应该像表情符号一样显示,但在 Textview 或 AppCompatTextview 中无法显示。有些表情符号可以像心形那样显示,但笑脸或生气的表情无法显示。
我的项目中有很多 Textview。我如何以最简单的方式解决这个问题。
感谢任何建议。
我有这样的一段文本。
"Hello world \u{1F603}"
。
"\u{1F603}" 这段文本应该像表情符号一样显示,但在 Textview 或 AppCompatTextview 中无法显示。有些表情符号可以像心形那样显示,但笑脸或生气的表情无法显示。
我的项目中有很多 Textview。我如何以最简单的方式解决这个问题。
感谢任何建议。
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 设置到我的 TextView
或 AppCompatTextview
中
// 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"));
}
}
}
您可以使用这些库中的任何一个来处理表情符号。我们也在这样做。
https://android-arsenal.com/details/1/4366
试试这个
StringEscapeUtils.unescapeJava(//your string with emoji//)