我正在做一個簡單的聊天應用程序,我想在編輯文字時顯示笑臉,同時編寫消息。EditText上的ImageSpan(笑臉)。使用SwiftKey鍵盤不起作用
我有這個識別至極字符將通過圖像作爲替代內容的throught的ImageSpan(這被稱爲只有當一個笑臉字符被插入的EditText):
for (index = start; index < start+num_chars; index++) {
if (index + 1 > editable.length())
continue;
if(emoticons.containsKey(editable.subSequence(index, index + 1).toString())){
int length=1;
Drawable drawable = context.getResources().getDrawable(emoticons.get(editable.subSequence(index, index + 1).toString()));
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
int size=Utils.GetDipsFromPixel(context, (int)(textSize*1.3));
Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, size, size, true));
int dWidth = d.getIntrinsicWidth();
int dHeight = d.getIntrinsicHeight();
d.setBounds(0 , -dHeight, dWidth, 0);
ImageSpan span;
span = new ImageSpan(d,ImageSpan.ALIGN_BASELINE);
editable.setSpan(span, index, index + length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
}
}
我使用SPAN_EXCLUSIVE_EXCLUSIVE標籤設置跨度,但我有swiftkey鍵盤的問題,因爲當我在edittext中插入笑臉時,我在imageSpan後面寫入的所有內容都會保留在圖像下方(如SPAN_EXCLUSIVE_INCLUSIVE)。與Android默認鍵盤我沒有這個問題。
我只想在EditText上使用WhatsApp應用程序相同的行爲。
有什麼建議嗎?我必須對我的代碼做任何更改?
編輯:「可編輯」變量傳遞給該方法。它是txtMessage.getText()的值,其中txtMessage是一個EditText。
謝謝!
編輯:只跨越一部分代碼!這適用於多行!我認爲問題在於使用Drawable-> Bitmap-> ResizedBitmap-> Drawable。
public static final HashMap<String, Integer> emoticons = new HashMap();
static {
emoticons.put("\ue415", R.drawable.e415);
emoticons.put("\ue056", R.drawable.e056);
emoticons.put("\ue057", R.drawable.e057);
...
public static Spannable getSmiledText(Context context, Spannable editable,
int start, int num_chars, float textSize) {
int index;
for (index = start; index < start + num_chars; index++) {
if (index + 1 > editable.length())
continue;
if (EmojiLayout.emoticons.containsKey(editable.subSequence(index,
index + 1).toString())) {
int length = 1;
Bitmap smiley = BitmapFactory.decodeResource(context.getResources(), ((Integer) EmojiLayout.emoticons.get(editable.subSequence(index,
index + 1).toString())));
int size = Utils.GetDipsFromPixel(context,
(int) (textSize * 1.37));
Bitmap scaledbmp=Bitmap.createScaledBitmap(
smiley, size, size, false);
ImageSpan span;
span = new ImageSpan(scaledbmp);
Log.d("EmojiLayout", "Index: " + String.valueOf(index) + "To: "
+ String.valueOf(index + length));
editable.setSpan(span, index, index + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
}
}
return editable;
}
郵寄此缺陷給開發者,看看他們說什麼.. – Ronnie 2012-07-24 17:57:56