单击Android中的按钮时如何发送表情符号图标? [英] How to send emojicons icons when click on button in Android?

查看:31
本文介绍了单击Android中的按钮时如何发送表情符号图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

1.I am making an chatting application, in which during chatting, I want to send emoticons icons when selects from the dialog and then click on send button, it should show that particular image.

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

2.when I am clicking on send button, it's sending to user and shows, but the problem is that I want to show the image on the place of a name, but it's showing me like this

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

like "4.png" or "2.png". I want to display the image which I have selected. What should I do? I am confused, please help me.here is my code

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

推荐答案

使用此代码可能有助于发送带有文本的表情符号

use this code may help to send the emojisicon with the text

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;
}

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

first create the heap for the emoticons,try this.

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

这篇关于单击Android中的按钮时如何发送表情符号图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆