Android Socket IQ chat software-instructions on how to encode chat bubbles

Source: Internet
Author: User

Android Socket IQ chat software-instructions on how to encode chat bubbles

Previously we talked about the LAN chat test of IQ chat software. Today we will talk about the generation and encoding of bubbles. This is not a third-party thing, it is our own.

After development, you need to integrate the layout and container to implement it. In fact, when you see the source code, you will feel very simple. Do not believe you followed me to look down

First of all, let's talk about my programming philosophy. If we want to dynamically generate a message list in an Activity, ListView is an essential container. This is one of them.

Second, we can clearly know that the background colors of "I" and friends are different. The background colors of QQ are: "I" is white, and friends are "green ", however, this is an old version. The new version has custom bubbles and many types. We will not introduce them one by one. Therefore, we need to define two ListView items to fill the ListView.

Third, after we write the layout file, the rest is to load the message list. Today we will focus on the development of bubbles, and then let's talk about the loading of the bubble message list.

 

First, create an Activity and add a ListView. Because the source code is uploaded, no code is pasted here.

Define the layout file of message bubbles for "me" and friends respectively:

This is a friend's message bubble layout. For Layout source code, go

Source code, with the database: http://download.csdn.net/detail/jspping/8067509

The main layout is a linear layout. A Portrait Imageview and a sending time Textview are added under the main layout. A linear layout is nested and a background image is added to the linear layout, that is, the green background bubble adds a friend message TextView in the linear layout to display the messages sent by friends.

 

My bubbles:

 

Bubbles dynamically load core code in Real Time:

 

Et_input = (EditText) findViewById (R. id. et_input); findViewById (R. id. ib_send ). setOnClickListener (new OnClickListener () {public void onClick (View v) {myAccount = MoreActivity. me. getAccount (); ObjectOutputStream oos; try {oos = new ObjectOutputStream // find this thread through the account to get OutputStream (ServerManager. getClientConServerThread (myAccount ). getSocket (). getOutputStream (); // get the input data and clear EditTextchatContent = et_input.getText (). toString (); et_input.setText (); // send the message IQMSG m = new IQMSG (); m. setType (IQMSGResult. COM_MES); m. setSender (myAccount); m. setSenderNick (MoreActivity. me. getNick (); m. setSenderAvatar (MoreActivity. me. getAvatar (); m. setReceiver (chatAccount); m. setContent (chatContent); m. setSendTime (IQTime. geTimeNoS (); oos. writeObject (m); // update the chat content updateChatView (new MessageEntity (MoreActivity. me. getAvatar (), chatContent, IQTime. geTime (), false);} catch (IOException e) {e. printStackTrace () ;}}); // register broadcast IntentFilter myIntentFilter = new IntentFilter (); myIntentFilter. addAction (engineer. jsp. iq. client. mes); br = new MyBroadcastReceiver (); registerReceiver (br, myIntentFilter); ManageActivity. addActiviy (MessageActivity, this) ;}@ Overridepublic void finish () {unregisterReceiver (br); super. finish ();} // broadcast receiver public class MyBroadcastReceiver extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {String [] mes = intent. getStringArrayExtra (message); Log. I (MessageActivity, data: + mes); // update the chat content updateChatView (new MessageEntity (Integer. parseInt (mes [2]), mes [3], mes [4], true) ;}} public void updateChatView (MessageEntity chatEntity) {chatEntityList. add (chatEntity); chatListView = (ListView) findViewById (R. id. lv_chat); chatListView. setAdapter (new MessageAdapter (this, chatEntityList ));}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.