2

ねえ、その Smily コードの正確な Imotion(Facebook) を見つけるのに問題があります.. Snippat コードを置きます:

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;
}

そして、私は Call This Function を持っています:

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

===>>私の問題は、:putnam:と入力すると、次のような結果が得られることです

こんにちはハッピーここに画像の説明を入力 ここに画像の説明を入力 ここに画像の説明を入力 ここに画像の説明を入力ここに画像の説明を入力

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

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);
}

しかし、そのいくつかのエラーを与える::

:putnam: と入力すると、結果は ここに画像の説明を入力utnam:になります。

4

1 に答える 1

2
  1. 「最大の笑顔コード」から「最小の笑顔コード」の順に並べ替える必要があります。
  2. 次のパターンが見つかったら、このテキストがすでにスパンで覆われているかどうかを確認してください。
Object[] spans = builder.getSpans(matcher.start(), matcher.end(), Object.class);
if (spans == null || spans.length == 0) {
    //add new image span here
}
于 2012-07-18T09:56:21.223 に答える