2012-07-18 60 views
2

嘿,我有一些問題找準確的iMotion(臉譜)對於代碼興高采烈.. 我把我的Snippat代碼:如何匹配精確匹配字Simly在聊天應用程序

private static final HashMap<String, Integer> smilyRegexMap = new HashMap<String, Integer>(); 

static{ 
     smilyRegexMap.put(":-)" , R.drawable.fb_smile); 
     smilyRegexMap.put(":)",R.drawable.fb_smile); 
     smilyRegexMap.put(":-]" , R.drawable.fb_smile); 

     smilyRegexMap.put(":-(", R.drawable.fb_frown); 
     smilyRegexMap.put(":(" , R.drawable.fb_frown); 
     smilyRegexMap.put(":-[" , R.drawable.fb_frown); 

     smilyRegexMap.put(":-P" , R.drawable.fb_tounge); 
     smilyRegexMap.put(":p", R.drawable.fb_tounge); 

     smilyRegexMap.put(":-D" , R.drawable.fb_grin); 
     smilyRegexMap.put(":D" , R.drawable.fb_grin); 

     smilyRegexMap.put(":-O" , R.drawable.fb_gasp); 
     smilyRegexMap.put(":*" , R.drawable.fb_gasp); 

     smilyRegexMap.put(";-)" , R.drawable.fb_wink); 
     smilyRegexMap.put(";)" , R.drawable.fb_wink); 
     smilyRegexMap.put(":putnam:" , R.drawable.fb_putnam); 
    } 

// == ================================================== =====

public static CharSequence addSmileySpans(Context ch, CharSequence your_recieved_message) 
{ 
    //smilyRegexMap = new HashMap<Integer, String>(); 

    System.out.println("==In Spannable Function.........."); 
    SpannableStringBuilder builder = new SpannableStringBuilder(your_recieved_message); 

    System.out.println("==================Size of Smily : "+ smilyRegexMap.size()); 
    @SuppressWarnings("rawtypes") 
    Iterator it = smilyRegexMap.entrySet().iterator(); 
    while (it.hasNext()) { 
     @SuppressWarnings("rawtypes") 
     Map.Entry pairs = (Map.Entry) it.next(); 

     Pattern mPattern = Pattern.compile((String) pairs.getKey(),Pattern.CASE_INSENSITIVE); 
     Matcher matcher = mPattern.matcher(your_recieved_message); 

     while (matcher.find()) { 
      Bitmap smiley = BitmapFactory.decodeResource(ch.getResources(), ((Integer) pairs.getValue())); 
      builder.setSpan(new ImageSpan(smiley), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     } 
} 
    return builder; 
} 

而且我調用這個函數:

addSmileySpans(this,"Hi i m Happy :-) ;-) :p :putnam: "); 

=== >>我的問題是,當我輸入:普特南:那麼這個結果得到像

你好我很高興enter image description hereenter image description hereenter image description hereenter image description hereenter image description here

// =========== ================================

Ookeyyy ...現在我使用此代碼:::

Bitmap smiley = BitmapFactory.decodeResource(ch.getResources(), ((Integer) pairs.getValue())); 
Object[] spans = builder.getSpans(matcher.start(), matcher.end(), ImageSpan.class); 
if (spans == null || spans.length == 0) { 
    builder.setSpan(new ImageSpan(smiley), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
} 

但它給了一些錯誤::

當輸入:普特南: 其結果是enter image description here utnam:

回答

2
  1. 你應該排序你爲了從「最燦爛的笑容代碼」到「最小的笑容代碼」微笑。
  2. 當你找到下一個模式只是檢查,如果這一段文字已經覆蓋跨度:
Object[] spans = builder.getSpans(matcher.start(), matcher.end(), Object.class); 
if (spans == null || spans.length == 0) { 
    //add new image span here 
} 
+0

是的,你的解決方案非常接近我的問題.. u能解釋更加清晰。因爲現在我輸入:putnam:給出兩個相同的圖像 – 2012-07-18 10:47:44

+0

alse解釋爲什麼不使用builder.getSpans(,Object.class);米不明白wht是Object.class ?? 謝謝 – 2012-07-18 10:49:08

+0

'Object.class'參數只是表示返回所有可能的跨度。如果你只有圖像,你可以使用'ImageSpan.class'。 – Jin35 2012-07-18 11:02:26