[英]Text is messed up with ImageSpan in EditText
I am building a simple chat app where the user has the ability to send text and emoticons. 我正在构建一个简单的聊天应用程序,用户可以在其中发送文本和表情符号。 I can send both text and emoticons to another phone. 我可以将文字和表情符号发送到另一部手机。 My problems are: 我的问题是:
1.When I type something and add an emoticon: 1.当我输入内容并添加表情符号时:
Then I cannot type any text right before and right after the image. 然后我无法在图像之前和之后输入任何文本。 I can write before the "o" letter. 我可以在“o”字母前面写。 The system "sees" that I type, so even if I type "Honey" after the smiley, I cannot see it, but the EditText registers it and the message is sent: 系统“看到”我输入的内容,所以即使我在笑脸后输入“Honey”,我也看不到它,但EditText会注册它并发送消息:
2.When I add just an emoticon to the Edittext then I delete it , I cannot type anything because the deleted emoticon appears. 2.当我向Edittext添加一个表情符号然后删除它时 ,我无法输入任何内容,因为删除的表情符号出现了。 It appears only once, so no matter how many characters I type, the EditText looks like just before I deleted the emoticon, BUT the text is sent without the emoticon, just like in all three cases. 它只出现一次,所以无论我输入多少个字符,EditText看起来就像我删除表情符号之前,但文本是在没有表情符号的情况下发送的,就像在所有三种情况下一样。
3.When I type "something" in the EditText then insert an emoticon after "some": 3.当我在EditText中输入“something”时,在“some”之后插入一个表情符号:
Then I put the cursor after the emoticon and delete it, here what's left: 然后我将光标放在表情符号后面并删除它,这里剩下的是:
But the correct message is sent when I press the Send button: 但是当我按下发送按钮时会发送正确的消息:
That's what's inside the button listener of the emoticon (this method is activated when I click the emoticon to add it to the EditText). 这就是表情符号按钮监听器内部的内容(当我单击表情符号将其添加到EditText时,此方法被激活)。
ib_happy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int cursorPosition = mOutEditText.getSelectionStart();
mOutEditText.getText().insert(cursorPosition, smileys[0]);
SpannableStringBuilder ssb = new SpannableStringBuilder(mOutEditText.getText());
ssb.setSpan(new ImageSpan(bitmapArray.get(0), ImageSpan.ALIGN_BASELINE), cursorPosition, cursorPosition+2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mOutEditText.setText(ssb, BufferType.SPANNABLE);
mOutEditText.setSelection(cursorPosition+2);
dialog_emoticon.dismiss();
}
});
I found the solution. 我找到了解决方案。 All I had to do was to change Spannable.SPAN_INCLUSIVE_INCLUSIVE
to Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
我所要做的就是将Spannable.SPAN_INCLUSIVE_INCLUSIVE
更改为Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
I would add a textwatcher to that edittext and watch as the user types, that way I can reposition the images/set the text/make corrections/validate input/etc. 我会将textwatcher添加到该edittext并观察用户类型,这样我就可以重新定位图像/设置文本/进行更正/验证输入/等。
editText.addTextChangedListener(textWatcher);
textWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
//editText.doStuffHere
//reposition your image/etc.
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
public void onTextChanged(CharSequence s, int start, int before, int count) { }
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.