微信聊天现在非常火,是因其界面漂亮吗,哈哈,也许吧。微信每条消息都带有一个气泡,非常迷人,看起来感觉实现起来非常难,其实并不难。下面小编给大家分享实现代码。
先给大家展示下实现效果图:
ok,下面我们来看一下整个小项目的主体结构:
下面是activity的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
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; // 发送btn private button mbtnback; // 返回btn private edittext medittextcontent; private listview mlistview; private chatmsgviewadapter madapter; // 消息视图的adapter private list<chatmsgentity> mdataarrays = new arraylist<chatmsgentity>(); // 消息对象数组 public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); initview(); // 初始化view initdata(); // 初始化数据 mlistview.setselection(madapter.getcount() - 1 ); } /** * 初始化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[] { "有大吗" , "有!你呢?" , "我也有" , "那上吧" , "打啊!你放大啊!" , "你tm咋不放大呢?留大抢人头啊?cao!你个菜b" , "2b不解释" , "尼滚..." , "今晚去网吧包夜吧?" , "有毛片吗?" , "种子一大堆啊~还怕没片?" , "ok,搞起!!" }; private string[] dataarray = new string[] { "2012-09-22 18:00:02" , "2012-09-22 18:10:22" , "2012-09-22 18:11:24" , "2012-09-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 ; // 初始化数组总数 /** * 模拟加载消息历史,实际开发可以从数据库中读出 */ public void initdata() { for ( int i = 0 ; i < count; i++) { chatmsgentity entity = new chatmsgentity(); entity.setdate(dataarray[i]); if (i % 2 == 0 ) { entity.setname( "肖b" ); entity.setmsgtype( true ); // 收到的消息 } else { entity.setname( "必败" ); entity.setmsgtype( false ); // 自己发送的消息 } 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(); break ; case r.id.btn_back: // 返回按钮点击事件 finish(); // 结束,实际开发中,可以返回主界面 break ; } } /** * 发送消息 */ private void send() { string contstring = medittextcontent.gettext().tostring(); if (contstring.length() > 0 ) { chatmsgentity entity = new chatmsgentity(); entity.setname( "必败" ); entity.setdate(getdate()); entity.setmessage(contstring); entity.setmsgtype( false ); mdataarrays.add(entity); madapter.notifydatasetchanged(); // 通知listview,数据已发生改变 medittextcontent.settext( "" ); // 清空编辑框数据 mlistview.setselection(mlistview.getcount() - 1 ); // 发送一条消息时,listview显示选择最后一项 } } /** * 发送消息时,获取当前事件 * * @return 当前时间 */ private string getdate() { simpledateformat format = new simpledateformat( "yyyy-mm-dd hh:mm:ss" ); return format.format( new date()); } } |
listview的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
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; /** * 消息listview的adapter * * @author way */ public class chatmsgviewadapter extends baseadapter { public static interface imsgviewtype { int imvt_com_msg = 0 ; // 收到对方的消息 int imvt_to_msg = 1 ; // 自己发送出去的消息 } private static final int itemcount = 2 ; // 消息类型的总数 private list<chatmsgentity> coll; // 消息对象数组 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; } /** * 得到item的类型,是对方发过来的消息,还是自己发送出去的 */ public int getitemviewtype( int position) { chatmsgentity entity = coll.get(position); if (entity.getmsgtype()) { //收到的消息 return imsgviewtype.imvt_com_msg; } else { //自己发送的消息 return imsgviewtype.imvt_to_msg; } } /** * item类型的总数 */ 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 ; } } |
消息对象的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
package com.way.demo; /** * 一个消息的javabean * * @author way * */ public class chatmsgentity { private string name; //消息来自 private string date; //消息日期 private string message; //消息内容 private boolean iscommeg = true ; // 是否为收到的消息 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; } } |
以上所述是小编给大家分享的android高仿微信聊天界面代码分享,希望对大家有所帮助。