通过聊天发送Emoctions

Gau*_*ora 4 android chat

我在android中开发了一个聊天应用程序,我想在我的应用程序中添加Emoctions功能.我已遍历以下链接:

Android上的消息中的表情符号

如何在android中的聊天中显示表情符号?

Android聊天应用程序笑脸

但是之间没有找到任何好的解决方案.

请指导我一个很好的超链接或解决方案.

场景:一个人可以从当前的集合中选择任何一个集合,它也应该在接收端收到.

Gau*_*ora 11

我已经解决了.这是我的代码:

像这样移动:

private HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
private ArrayList<String> arrayListSmileys = new ArrayList<String>();
ImageView imgbtn_show_smileys;
Run Code Online (Sandbox Code Playgroud)

在你的oncreate函数中继续这样做

emoticons.put(":-)", R.drawable.smile);
emoticons.put(":P", R.drawable.tongue);
emoticons.put(":D", R.drawable.cool);
emoticons.put(":-(", R.drawable.sad);
emoticons.put(":0", R.drawable.cool);
fillArrayList();
Run Code Online (Sandbox Code Playgroud)

制作您想要使用的一组图像/表情符号对

填充数组列表

private void fillArrayList() {
    Iterator<Entry<String, Integer>> iterator = emoticons.entrySet().iterator();
    while(iterator.hasNext()){
        Entry<String, Integer> entry = iterator.next();
        arrayListSmileys.add(entry.getKey());
    }
}
Run Code Online (Sandbox Code Playgroud)

现在最重要的部分,每次将图像设置为列表视图或edittext都使用此功能:

edittext.setText(getSmiledText(this,"your text"));

public Spannable getSmiledText(Context context, String text) {
        SpannableStringBuilder builder = new SpannableStringBuilder(text);
        int index;
        for (index = 0; index < builder.length(); index++) {
            for (Entry<String, Integer> entry : emoticons.entrySet()) {
                int length = entry.getKey().length();
                if (index + length > builder.length())
                    continue;
                if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) {
                    builder.setSpan(new ImageSpan(context, entry.getValue()), index, index + length,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    index += length - 1;
                    break;
                }
            }
        }
        return builder;
    }
Run Code Online (Sandbox Code Playgroud)

在对话框按钮上显示表情符号

imgbtn_show_smileys.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            final Dialog groupIconsDialog = new Dialog(UserChatActivity.this);
            groupIconsDialog.setTitle("Choose Group Icon");
            groupIconsDialog.setContentView(R.layout.group_icons_layout);
            //calling and setting the image icons to the grid view adapter
            final GridView groupIconsGrid = (GridView)groupIconsDialog.findViewById(R.id.grid_groupIcons);
            groupIconsGrid.setAdapter(new SmileysAdapter(arrayListSmileys, UserChatActivity.this, emoticons));


            groupIconsGrid.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {
                    // TODO Auto-generated method stub

                    String value = groupIconsGrid.getAdapter().getItem(position).toString();
                    value = editMessage.getText()+value;
                    Spannable spannable = getSmiledText(UserChatActivity.this, value);
                    editMessage.setText(spannable);
                    groupIconsDialog.dismiss();


                }
            });

            groupIconsDialog.show();

        }
    });
Run Code Online (Sandbox Code Playgroud)

如果您对此有任何疑问,请与我们联系.

谢谢