2014-08-29 38 views
0

我有SpannedText in EditText而我用微笑取代文字例如cool替換爲smile。但是當我按下Backspace上的Android手機(Nexus 5 kitkat 4.4.4)它顯示「(微笑)酷」,然後刪除字母最後微笑本身。但在Genymotion模擬器它的工作需要。我的代碼有什麼問題?Android Spanned Text刪除微笑時顯示文字

此代碼是在

Drawable d = Drawable.createFromStream(ims, null); 
       Bitmap b = ((BitmapDrawable)d).getBitmap(); 
       int spanSmileHeight = (displayHeight>displayWidth) ? ((int) ((double) displayHeight/25)) : ((int) ((double) displayWidth/25)); 
       int spanSmileWidth = ((int) ((double) spanSmileHeight/b.getHeight())*b.getWidth())+15; 
       Bitmap bitmapResized = Bitmap.createScaledBitmap(b, spanSmileWidth, spanSmileHeight, false); 
       Drawable sd = new BitmapDrawable(getResources(), bitmapResized); 
       sd.setBounds(0, 0, spanSmileWidth, spanSmileHeight); 
       addPattern(emoticons, smileyCode, sd); 
... 

ImageView.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         int start = Math.max(message.getSelectionStart(), 0); 
         int end = Math.max(message.getSelectionEnd(), 0); 
         message.getText().replace(Math.min(start, end), Math.max(start, end), 
           smileyCode, 0, smileyCode.length()); 
         message.setText(getSmiledText(view.getContext(), message.getText())); 
         message.setSelection(start + smileyCode.length()); 

這bacspace

public static boolean addSmiles(Context context, Spannable spannable) { 
     boolean hasChanges = false; 
     for (Map.Entry<Pattern, Drawable> entry : emoticons.entrySet()) { 
      Matcher matcher = entry.getKey().matcher(spannable); 
      while (matcher.find()) { 
       boolean set = true; 
       for (ImageSpan span : spannable.getSpans(matcher.start(), 
         matcher.end(), ImageSpan.class)) 
        if (spannable.getSpanStart(span) >= matcher.start() 
          && spannable.getSpanEnd(span) <= matcher.end()) 
         spannable.removeSpan(span); 
        else { 
         set = false; 
         break; 
        } 
       if (set) { 
        hasChanges = true; 
        spannable.setSpan(new ImageSpan(entry.getValue(), ImageSpan.ALIGN_BOTTOM), 
          matcher.start(), matcher.end(), 
          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
       } 
      } 
     } 
     return hasChanges; 
    } 

    public static Spannable getSmiledText(Context context, CharSequence text) { 
     Spannable spannable = spannableFactory.newSpannable(text); 
     addSmiles(context, spannable); 
     return spannable; 
    } 

之前

before backspace

退格後(的虛擬keyobard)按壓

after backspace

日誌輸出:

09-01 06:22:21.280 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**cat* 
09-01 06:23:32.060 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**cat 
09-01 06:23:32.080 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat** 
09-01 06:23:32.084 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**cat 

3次以上

09-01 06:24:07.980 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**ca 
09-01 06:24:08.264 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat**c 
09-01 06:24:08.668 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat** 

這裏笑容刪除

09-01 06:24:09.248 1271-1271/com.nnadir.wapchat.app I/wapChat﹕ *sad**sad**sad**sad**sad**cat* 
+0

你的代碼?什麼代碼? – pskink 2014-08-29 10:30:40

+0

我已將代碼添加到問題中。 – 2014-08-29 10:48:05

+0

我發現它發生在Nexus 5上 – 2014-09-01 06:52:48

回答

0

我已經找到一種方法如何防止這種情況。如果我們將targetSDK更改爲15,則笑容將被成功刪除。