我创建了一个自定义软键盘 (IME),我们可以在其中添加自定义表情符号。
每当我尝试向其中添加我的表情符号时。它覆盖最后输入的文本。我的意思是它不会附加 表情符号,除非它覆盖它。如何将表情符号添加到 currentInputConnection
例如
i image write hello image abcimage ///where image represents emoji
变成了
i image write hello image image
//我可以轻松地在空格后添加图像,或者我可以轻松地重复添加图像。
当我添加文本时,它会附加到表情符号,但是当我在输入一些文本后添加表情符号时,它会删除文本,然后自行添加(表情符号图像)。
只是为了测试目的,我把表情符号代码放在shift键上
问题代码
else if (primaryCode == Keyboard.KEYCODE_SHIFT)
{
// this.handleShift();
//this.mComposing.append(getSmiledText(getApplicationContext(), ":)"));
ImageGetter imageGetter = new ImageGetter()
{
public Drawable getDrawable(String source) {
Drawable d = getResources().getDrawable(R.drawable.e041);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
Spanned cs = Html.fromHtml("<img src='" + getResources().getDrawable(R.drawable.e041) + "'/>", imageGetter, null);
// getCurrentInputConnection().commitText(cs, 1);
// this.mComposing.append(cs);
//getCurrentInputConnection().commitText(getSmiledText(getApplicationContext(), ":)"), 1);
getCurrentInputConnection().beginBatchEdit();
getCurrentInputConnection().commitText(cs, 1);
getCurrentInputConnection().endBatchEdit();
//getCurrentInputConnection().setComposingText(cs, 1); // it is giving wrong
}
并尝试了多种方法解决,部分尝试过的代码在评论中展示
简单的话:我想将表情符号图像附加到文本中,但是当我将图像添加到文本中时,它会删除书面文本,我该如何解决。 无法显示完整的源代码,因为它太长了。问我是否需要我类的任何方法
相关链接:
add custom image as Emoji in android
https://stackoverflow.com/questions/24100615/cannot-add-an-image-to-my-keyboard-service
Implementations of Emoji (Emoticon) View/Keyboard Layouts
提前致谢。
最佳答案
我也尝试过同样的方法,但总是得到“obj”。最后我找到了一个解决方案试试这个。
不是添加可绘制对象,而是像这样通过它们的 uniCodes 添加表情符号..
getCurrentInputConnection().commitText((CharSequence)"\ud83c\udfb5", 1);
它非常适合我。并且还从您的代码中删除了 beginBatchEdit 和 endBatchEdit。
谢谢
关于android - 将表情符号(表情符号图像)嵌入自定义软键盘android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27604043/