Android High Imitation micro-letter Chat interface code sharing _android

Source: Internet
Author: User
Tags getdate getmessage static class

Micro-letter Chat is now very hot, because its interface beautiful, haha, maybe. Micro-letter Each message has a bubble, very attractive, it seems to realize very difficult, in fact, it is not difficult. The following small series for everyone to share the implementation code.

First show you the implementation of the results of the chart:

OK, let's take a look at the main structure of the entire small project:

The following is the code for the activity:

Package Com.way.demo; 
Import Java.text.SimpleDateFormat; 
Import java.util.ArrayList; 
Import Java.util.Date; 
Import java.util.List; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
Import Android.widget.Button; 
Import Android.widget.EditText; 
Import Android.widget.ListView; /** * @author Way/public class Weixinchatdemoactivity extends activity implements Onclicklistener {private Button 
mbtnsend;//send btn Private Button mbtnback;//return btn private EditText medittextcontent; 
Private ListView Mlistview; Private Chatmsgviewadapter madapter;//Message view adapter private list<chatmsgentity> mdataarrays = new arraylist< 
Chatmsgentity> ()//Message object array public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
Setcontentview (R.layout.main); 
Initview ()//Initialize View InitData ()//Initialize Data mlistview.setselection (Madapter.getcount ()-1); }/** * Initialize view/public void INitview () {Mlistview = (ListView) Findviewbyid (R.id.listview); 
Mbtnsend = (Button) Findviewbyid (r.id.btn_send); 
Mbtnsend.setonclicklistener (this); 
Mbtnback = (Button) Findviewbyid (r.id.btn_back); 
Mbtnback.setonclicklistener (this); 
Medittextcontent = (edittext) Findviewbyid (r.id.et_sendmessage); Private string[] Msgarray = new string[] {"Big", "there!" What about you? "I have, too," said the man, "Fight!" You zoom in! "Why don't you zoom in?" How about a head grab? Cao! You dish B ", 2B does not explain", "the Martini ...", "Go to the Internet café to pack night tonight?" "Is there any pornography?" "," a lot of seeds ah ~ still afraid of no film? "," OK, get up!! 
" }; Private string[] DataArray = new string[] {"2012-09-22 18:00:02", "2012-09-22 18:10:22", "2012-09-22 18:11:24", "2012-0 9-22 18:20:23 "," 2012-09-22 18:30:31 "," 2012-09-22 18:35:37 "," 2012-09-22 18:40:13 "," 2012-09-22 18:50:26 "," 2012-09-22 
18:52:57 "," 2012-09-22 18:55:11 "," 2012-09-22 18:56:45 "," 2012-09-22 18:57:33 ",}; Private final static int COUNT = 12;//initialization array Total/** * Simulate loading message history, the actual development can read from the database/public void InitData () {for (int i = 0; i < COUNT; i++) {chatmsgentity entity = new ChatMsgentity (); 
Entity.setdate (Dataarray[i]); 
if (i% 2 = 0) {entity.setname ("Shaw B"); 
Entity.setmsgtype (TRUE);/the message received} else {Entity.setname ("must fail"); 
Entity.setmsgtype (false);//message sent by itself} entity.setmessage (Msgarray[i]); 
Mdataarrays.add (entity); 
} madapter = new Chatmsgviewadapter (this, mdataarrays); 
Mlistview.setadapter (Madapter); 
@Override public void OnClick (View v) {switch (V.getid ()) {Case r.id.btn_send://send button click event Send (); 
Break 
Case r.id.btn_back://return button click event Finish ()//end, in actual development, you can return to the main interface break; 
}/** * Send message */private void Send () {String contstring = Medittextcontent.gettext (). toString (); 
if (contstring.length () > 0) {chatmsgentity entity = new chatmsgentity (); 
Entity.setname ("must fail"); 
Entity.setdate (GetDate ()); 
Entity.setmessage (contstring); 
Entity.setmsgtype (FALSE); 
Mdataarrays.add (entity); 
Madapter.notifydatasetchanged ();//Notify ListView, Data has changed Medittextcontent.settext (""); empty edit box data Mlistview.setselection (Mlistview.getcount ()-1);//When a message is sent, ListView displays the last item selected}/** * Send a message, get current event * * @return Current time/private String getDate () {SimpleDateFormat F 
Ormat = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); 
Return Format.format (New Date ()); } 
}

ListView Code:

Package Com.way.demo; 
Import java.util.List; 
Import Android.content.Context; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.BaseAdapter; 
Import Android.widget.TextView; /** * Message ListView Adapter * * @author Way/public class Chatmsgviewadapter extends Baseadapter {public static inte Rface imsgviewtype {int imvt_com_msg = 0;//Receive message int imvt_to_msg = 1;//send out message} private static final int ITEMC 
Ount = total number of 2;//message types private list<chatmsgentity> coll;//Message Object array private Layoutinflater minflater; 
Public Chatmsgviewadapter (Context context, list<chatmsgentity> coll) {this.coll = coll; 
Minflater = Layoutinflater.from (context); 
public int GetCount () {return coll.size (); 
Public Object getitem (int position) {return coll.get (position); 
public long getitemid (int position) {return position; /** * Get the type of item, is the message sent over by the other side, or send out their own * * public int getitemviewtype (int position) {chatmsgentity entity = coll.get (position); 
if (Entity.getmsgtype ()) {//Received message return imsgviewtype.imvt_com_msg; 
else {//Send yourself a message return imsgviewtype.imvt_to_msg; 
The total number of/** * Item Types/public int getviewtypecount () {return ITEMCOUNT; 
Public View GetView (int position, View Convertview, ViewGroup parent) {chatmsgentity entity = coll.get (position); 
Boolean iscommsg = Entity.getmsgtype (); 
Viewholder viewholder = null; if (Convertview = = null) {if (iscommsg) {Convertview = minflater.inflate (r.layout.chatting_item_msg_text_left, NULL) 
; 
else {Convertview = minflater.inflate (r.layout.chatting_item_msg_text_right, NULL); 
} Viewholder = new Viewholder (); 
Viewholder.tvsendtime = (TextView) convertview. Findviewbyid (R.id.tv_sendtime); 
Viewholder.tvusername = (TextView) convertview. Findviewbyid (R.id.tv_username); 
Viewholder.tvcontent = (TextView) convertview. Findviewbyid (r.id.tv_chatcontent); 
Viewholder.iscommsg = iscommsg; Convertview.settag (viewhoLder); 
else {Viewholder = (Viewholder) convertview.gettag (); 
} viewHolder.tvSendTime.setText (Entity.getdate ()); 
ViewHolder.tvUserName.setText (Entity.getname ()); 
ViewHolder.tvContent.setText (Entity.getmessage ()); 
return convertview; 
Static class Viewholder {public TextView tvsendtime; 
Public TextView Tvusername; 
Public TextView tvcontent; 
public Boolean iscommsg = true;  } 
}

Code for the Message object:

Package Com.way.demo; 
/** * 
a message javabean 
* * 
* @author Way 
*/public 
class Chatmsgentity { 
private String name ;//Message from 
private string date;//message date 
private string message;//message content 
Private Boolean iscommeg = true;// Is the message that is received public 
String GetName () {return 
name; 
} 
public void SetName (String name) { 
this.name = name; 
} 
Public String GetDate () {return 
date; 
} 
public void setdate (String date) { 
this.date = date; 
} 
Public String GetMessage () {return message 
; 
} 
public void Setmessage (String message) { 
this.message = message; 
} 
public Boolean Getmsgtype () {return 
iscommeg; 
} 
public void Setmsgtype (Boolean iscommsg) { 
iscommeg = iscommsg; 
} 
Public chatmsgentity () { 
} public 
chatmsgentity (string name, string date, String text, Boolean iscommsg) {
   
    super (); 
this.name = name; 
This.date = date; 
This.message = text; 
This.iscommeg = iscommsg; 
} 

   

The above is a small set to share the Android high imitation micro-letter Chat interface code sharing, I hope to help.

Related Article

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.