Kotlin

val config = FontRequestEmojiCompatConfig(...)

.setReplaceAll(true)

.setEmojiSpanIndicatorEnabled(true)

.setEmojiSpanIndicatorColor(Color.GREEN)

.registerInitCallback(object: EmojiCompat.InitCallback() {

...

})Java

EmojiCompat.Config config = new FontRequestEmojiCompatConfig(...)

.setReplaceAll(true)

.setEmojiSpanIndicatorEnabled(true)

.setEmojiSpanIndicatorColor(Color.GREEN)

.registerInitCallback(new InitCallback() {...})

添加初始化监听器

将 EmojiCompat 与 AppCompat 微件搭配使用

将支持库添加到 dependencies 部分。

dependencies {

implementation "com.android.support:support-emoji-appcompat:$version"

}

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

捆绑式字体配置

注意:字体的大小达几兆字节。

添加支持库依赖项

如需通过捆绑式字体配置使用

如需将支持库添加到应用项目中,请执行以下操作:

打开应用的 build.gradle 文件。

将支持库添加到 dependencies 部分。

dependencies {

...

implementation "com.android.support:support-emoji-bundled:$version"

}

使用捆绑式字体配置 EmojiCompat

如需使用捆绑式字体配置

Kotlin

class MyApplication : Application() {

override fun onCreate() {

super.onCreate()

val config = BundledEmojiCompatConfig(this)

EmojiCompat.init(config)

}

}Java

public class MyApplication extends Application {

@Override

public void onCreate() {

super.onCreate();

EmojiCompat.Config config = new BundledEmojiCompatConfig(this);

EmojiCompat.init(config);

...

}

}

不搭配微件使用 EmojiCompat

Kotlin

val processed = EmojiCompat.get().process("neutral face \uD83D\uDE10")Java

CharSequence processed = EmojiCompat.get().process("neutral face \uD83D\uDE10");

将 EmojiCompat 用于 IME

使用 true。

键盘还可以检查应用支持的

将 EmojiCompat 与自定义微件搭配使用

您始终可以使用

示例 TextView

Kotlin

class MyTextView(context: Context) : AppCompatTextView(context) {

private val emojiTextViewHelper: EmojiTextViewHelper by lazy(LazyThreadSafetyMode.NONE) {

EmojiTextViewHelper(this).apply {

updateTransformationMethod()

}

}

override fun setFilters(filters: Array) {

super.setFilters(emojiTextViewHelper.getFilters(filters))

}

override fun setAllCaps(allCaps: Boolean) {

super.setAllCaps(allCaps)

emojiTextViewHelper.setAllCaps(allCaps)

}

}Java

public class MyTextView extends AppCompatTextView {

...

public MyTextView(Context context) {

super(context);

init();

}

...

private void init() {

getEmojiTextViewHelper().updateTransformationMethod();

}

@Override

public void setFilters(InputFilter[] filters) {

super.setFilters(getEmojiTextViewHelper().getFilters(filters));

}

@Override

public void setAllCaps(boolean allCaps) {

super.setAllCaps(allCaps);

getEmojiTextViewHelper().setAllCaps(allCaps);

}

private EmojiTextViewHelper getEmojiTextViewHelper() {

...

}

}

示例 EditText

Kotlin

class MyEditText(context: Context) : AppCompatEditText(context) {

private val emojiEditTextHelper: EmojiEditTextHelper by lazy(LazyThreadSafetyMode.NONE) {

EmojiEditTextHelper(this).also {

super.setKeyListener(it.getKeyListener(keyListener))

}

}

override fun setKeyListener(input: KeyListener?) {

input?.also {

super.setKeyListener(emojiEditTextHelper.getKeyListener(it))

}

}

override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {

val inputConnection: InputConnection = super.onCreateInputConnection(outAttrs)

return emojiEditTextHelper.onCreateInputConnection(

inputConnection,

outAttrs

) as InputConnection

}

}Java

public class MyEditText extends AppCompatEditText {

...

public MyEditText(Context context) {

super(context);

init();

}

...

private void init() {

super.setKeyListener(getEmojiEditTextHelper().getKeyListener(getKeyListener()));

}

@Override

public void setKeyListener(android.text.method.KeyListener keyListener) {

super.setKeyListener(getEmojiEditTextHelper().getKeyListener(keyListener));

}

@Override

public InputConnection onCreateInputConnection(EditorInfo outAttrs) {

InputConnection inputConnection = super.onCreateInputConnection(outAttrs);

return getEmojiEditTextHelper().onCreateInputConnection(inputConnection, outAttrs);

}

private EmojiEditTextHelper getEmojiEditTextHelper() {

...

}

}

常见问题解答

如何启动字体下载?

如果设备上不存在表情符号字体,会在首次请求时下载这些字体。下载调度对应用是透明的。

初始化需要多长时间?

下载字体后,初始化

EmojiCompat 支持库会使用多少内存?

目前,用于查找表情符号的数据结构加载到应用的内存中,约占 200KB。

我可以将 EmojiCompat 用于自定义 TextView 吗?

可以。EmojiCompat 为自定义微件提供辅助程序类。也可以对给定字符串进行预处理并将其转换为 将 EmojiCompat 与自定义微件搭配使用部分。

如果我在搭载 Android 4.4(API 级别 19)或更低版本的设备上的布局 XML 中添加微件,会出现什么情况?

如果您的应用支持搭载 Android 4.4(API 级别 19)或更低版本的设备,您可以在应用中添加

其他资源

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐