java - 如何在聊天应用程序中匹配 Simly 的精确单词

标签 java android

嘿,我在为那个笑脸代码找到确切的 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;
}

我已经调用这个函数:

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

===>> 我的问题是,当我输入 :putnam: 时,会得到像

这样的结果

嗨,我很高兴 enter image description here enter image description here enter image description here enter image description here enter 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);
}

但是它给出了一些错误::

当输入:putnam: 其结果是 enter image description here乌特南:

最佳答案

  1. 您应该按照从“最大微笑代码”到“最小微笑代码”的顺序对微笑进行排序。
  2. 当您找到下一个模式时,只需检查这段文本是否已被 span 覆盖:
Object[] spans = builder.getSpans(matcher.start(), matcher.end(), Object.class);
if (spans == null || spans.length == 0) {
    //add new image span here
}

关于java - 如何在聊天应用程序中匹配 Simly 的精确单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11538522/

相关文章:

java - Spring 在 Filter 中使用 @Value 注解

java - 如何获取发送到 JMETER 中 ISO8583 采样器服务器的字节数组?

java - 数据库连接应该始终保持打开状态还是仅在需要时才打开?

android - Firebase 推送通知更新数据库

java - 如何将用户重定向到 Cloud Firestore 中的特定 Activity ?

java - 将中缀表达式转换为后缀表达式时处理括号

java - 在Java中通过键名解析JSON

Android如何获取以编程方式使用whats app应用程序的联系人列表

java - Android L 5.0 和 libusb 1.0.9 - 没有权限?

java - Fragment 中的 RecyclerView 错误