Embedding emoji(emoticons images) to custom softkeyboard android

匿名 (未验证) 提交于 2019-12-03 08:59:04

问题:

I have created a custom soft Keyboard(IME) where we can add custom emoji.

Whenever i try to add my emoticons to it. it override the last text entered. i mean it dont append the emoticons unless it override it. how can i add emoji to currentInputConnection

for example

i image write  hello image abcimage          ///where image represents emoji 

it becomes

i image write hello image image 

// and i can add image after space easily or i can repeatedly add images easily .

when i add text it appends to emoji but when i add emoji after entering some text it remove the text and then add it self(emoji image).

Just for testing purpose i put emoji code to shift key

Problem code

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            }  

And tried multiple ways to solve it ,some tried codes are shown in comments

Simple Words : I want to append emoji image to text, but when i add image to text, it remove the written text, how can i solve. full source can't be shown it is lengthy. Ask me if you need any method in my class

Related link :

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

Thanks in Advance.

回答1:

I also tried for the same but always got "obj".Atlast I found a solution try this.

Instead of adding drawables just add emoticons by their uniCodes like this..

getCurrentInputConnection().commitText((CharSequence)"\ud83c\udfb5", 1);

Its working for me perfectly.and also remove beginBatchEdit and endBatchEdit from your code.

Thanks



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!