android - 将表情符号(表情符号图像)嵌入自定义软键盘android

标签 android android-softkeyboard android-input-method

我创建了一个自定义软键盘 (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/

相关文章:

java - 如何在 Activity 重新启动时保留 Intent 对象的值?

android - 我怎样才能使状态栏白色与黑色图标 : Android 11?

android - Android软键盘怎么没有响应EditText?

android - 通过 Android API 发送组合键(元键和键码)

android - 以编程方式显示软键盘 - 不工作

java - Parcelable 类不会创建可分割对象

android - 键盘隐藏输入(位置:fixed; bottom:0;) with phonegap on android

android - 聚焦edittext时如何显示软键盘

Android:如何从 IME 设置首选键盘 View 模式

android - 函数中的错误