如何在Android中单击按钮时发送emojicons图标?

时间:2014-05-02 06:30:39

标签: android xmpp

1.我正在制作一个聊天应用程序,其中在聊天期间,我想在从对话框中选择时发送表情图标,然后点击发送按钮,它应该显示该特定图像。

2.当我点击发送按钮时,它会发送给用户并显示,但问题是我想在名称的地方显示图像,但它显示了我像这样

enter image description here

喜欢" 4.png"或" 2.png"。 我想显示我选择的图像。我该怎么办?我很困惑,请帮助我。我的代码是

 smilee.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View arg0) 
        {

            if (!popupWindow.isShowing()) 
            {
                popupWindow.setHeight((int) (keyboardHeight));

                if (isKeyBoardVisible) {
                    emoticonsCover.setVisibility(LinearLayout.GONE);
                } else {
                    emoticonsCover.setVisibility(LinearLayout.VISIBLE);
                }
                popupWindow.showAtLocation(parentLayout, Gravity.BOTTOM, 0, 0);

            } else {
                popupWindow.dismiss();
            }

        }});
    send.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View view) 
        {

            String text = mSendText.getText().toString().trim();

            Spanned sp = mSendText.getText();                   
            //chats.add(sp);
            //content.setText("");

            mSendText.setCompoundDrawables(null, null, getResources().getDrawable(R.drawable.s1), null);
            if(commanInstance.checkInternetConn(getApplicationContext()))
            {
                if(commanInstance.getConnection().isConnected())
                {
                    if(text.length()!=0)
                    {
                        Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");

                        Chat chat = null;
                        chat = commanInstance.getConnection().getChatManager().createChat(to, XMPPClient.this);
                        chat_imageview.setEnabled(true);

                        /*
                         * send msg
                         */
                        Message             message = new Message(chat.getParticipant(), Message.Type.chat);
                        message.setThread(chat.getThreadID());
                        String messagePacketID=message.getPacketID();
                        //message.setProperty("Time", commanInstance.getCurrentTime());
                        message.setThread(commanInstance.getCurrentTime());
                        message.setBody(text);

                        MessageEventManager.addNotificationsRequests(message, true, true, true, true);
                        try {
                            chat.sendMessage(message);
                            sendCancelledNotification(to);

                        } catch (XMPPException e1) {
                            e1.printStackTrace();
                        }
                        /*
                         * add msg in list item
                         */
                        /*
                         * insert into database
                         *
                         */
                            DatabaseHandler db = new DatabaseHandler(getApplicationContext());

                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
                        Date date = new Date();

                        String[] toYou=to.split("@");




                        ItemTwoLine act=new ItemTwoLine(getResources().getDrawable(R.drawable.ic_launcher),changeNameToCapital(getUserName())+"#:#"+text+"#:#"+"Sent","Me"+"#:#"+commanInstance.getCurrentTime()+"#:#"+messagePacketID,true,getResources().getDrawable(R.drawable.check));
                        commanInstance.getCustomeList(listKeyValue).add(act);
                        int positionOfItem=commanInstance.getCustomeList(listKeyValue).indexOf(act);
                        db.addContact(new Contact(getUserName(),toYou[0],changeNameToCapital(getUserName())+"#:#"+text+"#:#"+"Sent","Me"+"#:#"+commanInstance.getCurrentTime()+"#:#"+messagePacketID,"Sent","Out","garvage","garvage",String.valueOf(positionOfItem),dateFormat.format(date)));



                        db.close();
                        /*
                         * Mantain open conversation screen list
                         */

                        //  String valForOpen=openCon[0]+"#:#"+text;

                        /*
                         * End open conversation screen list and start refersh list
                         */

                        if(t)
                        {
                            list.setAdapter(adapter);

                            adapter.notifyDataSetChanged();
                            t=false;
                        }
                        else
                        {
                            adapter.notifyDataSetChanged();
                        }
                        mSendText.setText("");
                        scrollMyListViewToBottom();
                        /*
                         * set alarm
                         */
                        //setAlarm(openCon[0]);
                        /*
                         * set alarm end
                         */


                    }
                }
            }
        }
    });

}

这是我的适配器类

Msg = (TextView) convertView.findViewById(R.id.Msg);
            //Msg.setText(Html.fromHtml(nameAndText[1] ));

            //               TextView textView2 = (TextView)findViewById( R.id.TextView2 );
            SpannableStringBuilder ssb = new SpannableStringBuilder(nameAndText[1]);
            //Bitmap smiley = BitmapFactory.decodeResource( getResources(), R.drawable.emoticon );

            //ssb.setSpan(smiley, 16, 17, Spannable.SPAN_INCLUSIVE_INCLUSIVE );    
            Msg.setText( ssb, BufferType.SPANNABLE );

2 个答案:

答案 0 :(得分:1)

您可以使用此库。

https://github.com/rockerhieu/emojicon

它具有自定义文本视图和控件,能够显示表情符号并处理将其转换为unicode。

我在聊天应用程序中使用它,它就像一个魅力,即使将它们保存在数据库中也很容易。

答案 1 :(得分:1)

使用此代码可能有助于发送带有文本的emojisicon

textview.setText(getSmiledText(text.toString()));

     public Spannable getSmiledText(String text) {
    SpannableStringBuilder builder = new SpannableStringBuilder(text);
    if (emoticons.size() > 0) {
        int index;
        for (index = 0; index < builder.length(); index++) {
            if (Character.toString(builder.charAt(index)).equals(":")) {
                for (Map.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(getContext(), entry.getValue()), index, index + length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        index += length - 1;
                        break;
                    }
                }
            }
        }
    }
    return builder;
}

首先为表情符号创建堆,试试这个。

private HashMap<String, Integer> emoticons = new HashMap<String, Integer>();

emoticons.put(":-)", R.drawable.f01);
emoticons.put(":P", R.drawable.f02);
emoticons.put(":D", R.drawable.f03);