通过聊天发送Emoctions

时间:2013-02-05 04:42:52

标签: android chat

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

emoticons in message on Android

How to display emoticons in chat in android?

Android Chat application smiley

但两者之间没有找到任何好的解决方案。

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

场景:一个人可以从当前的发布中选择任何一个发布,并且也应该在接收方端接收。

3 个答案:

答案 0 :(得分:11)

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

这样移动:

private HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
private ArrayList<String> arrayListSmileys = new ArrayList<String>();
ImageView imgbtn_show_smileys;
你的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();

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

填充数组列表

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

现在最重要的部分,每次将图像设置为列表视图或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;
    }

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

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

        }
    });

如果您对此有任何疑问,请告诉我。

由于

答案 1 :(得分:2)

以下是我用来解决问题的Smileys适配器:

public class SmileysAdapter extends BaseAdapter {

    private ArrayList<String> arrayListSmileys = new ArrayList<String>();
    private Context context;
    private HashMap<String, Integer> emoticons = new HashMap<String, Integer>();
    private ArrayList<String> showListSmileys = new ArrayList<String>();


    public SmileysAdapter(ArrayList<String> arraylistSmileys,Context context,HashMap<String, Integer> emoticons) {
        // TODO Auto-generated constructor stub

        this.arrayListSmileys = arraylistSmileys;
        this.context = context;
        this.emoticons = emoticons;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arrayListSmileys.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arrayListSmileys.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.row, null);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
        imageView.setBackgroundResource(emoticons.get(arrayListSmileys.get(position)));
        return convertView;
        }
}

答案 2 :(得分:-1)

好吧,基本上表情符号都是这样处理的。 每个表情符号都基于一个字符组合。您可以使用相应的字符组合制作各种图像。 让我们用这个做一个场景。

  1. 设备A发送了微笑图像表情符号,表示为:)
  2. 表情符号通过服务器(或通过任何方法)发送到设备B
  3. 设备B收到一个字符串:)然后将其替换为笑脸图像表情符号