4

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

Android上消息中的表情符号

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

Android 聊天应用笑脸

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

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

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

4

3 回答 3

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

        }
    });

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

谢谢

于 2013-02-05T07:21:18.730 回答
2

这是我用来解决问题的笑脸适配器:

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;
        }
}
于 2013-02-25T07:51:12.390 回答
-1

嗯,基本上表情都是这样处理的。每个表情符号都基于字符组合。你可以用它们对应的字符组合来制作各种图像。让我们用这个来做一个场景。

  1. 设备 A 发送了一个微笑图像表情符号,用 :) 表示
  2. 表情符号通过服务器(或通过任何方法)发送到设备 B
  3. 设备 B 收到一个字符串 :) 然后将其替换为微笑图像表情符号
于 2013-02-05T06:00:55.770 回答