问题描述:在上图中的编辑框中,先输入两个表情,然后在两个表情中间输入两个以上汉字(是同时输入的),程序崩溃。
解决办法:
修改项目中的EmoticonsEditText.java文件,将函数onTextChanged替换为下面修改过的函数:
protected void onTextChanged(CharSequence arg0, int start, int lengthBefore, int after) {
super.onTextChanged(arg0, start, lengthBefore, after);
emojiText(arg0.toString());
if (after > 0) {
int end = start + after;
String keyStr = arg0.toString().substring(start, end);
boolean isEmoticonMatcher = false;
for (EmoticonBean bean : emoticonBeanList) {
if (!TextUtils.isEmpty(bean.getContent()) && bean.getContent().equals(keyStr)) {
Drawable drawable = ImageLoader.getInstance(mContext).getDrawable(bean.getIconUri());
if (drawable != null) {
int itemHeight;
if (mItemHeight == WRAP_DRAWABLE) {
itemHeight = drawable.getIntrinsicHeight();
} else if (mItemHeight == WRAP_FONT) {
itemHeight = mFontHeight;
} else {
itemHeight = mItemHeight;
}
int itemWidth;
if (mItemWidth == WRAP_DRAWABLE) {
itemWidth = drawable.getIntrinsicWidth();
} else if (mItemWidth == WRAP_FONT) {
itemWidth = mFontHeight;
} else {
itemWidth = mItemWidth;
}
drawable.setBounds(0, 0, itemHeight, itemWidth);
VerticalImageSpan imageSpan = new VerticalImageSpan(drawable);
getText().setSpan(imageSpan, start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
isEmoticonMatcher = true;
}
}
}
if (!isEmoticonMatcher) {
ImageSpan[] oldSpans = getText().getSpans(start, end, ImageSpan.class);
if(oldSpans != null){
for (int i = 0; i < oldSpans.length; i++) {
int startOld = end;
// 2015.11.03 Modified by zhangrong_lu
// int endOld = after + getText().getSpanEnd(oldSpans[i]) - 1;
int endOld = getText().getSpanEnd(oldSpans[i]);
if (startOld >= 0 && endOld > startOld) {
ImageSpan imageSpan = new ImageSpan(oldSpans[i].getDrawable(), ImageSpan.ALIGN_BASELINE);
getText().removeSpan(oldSpans[i]);
getText().setSpan(imageSpan, startOld, endOld, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
}
}
}
if(onTextChangedInterface != null){
onTextChangedInterface.onTextChanged(arg0);
}
}
程序崩溃的问题就顺利解决了,