本文实例讲述了android编程实现泡泡聊天界面的方法。分享给大家供大家参考,具体如下:
昨天写了个界面,实现了android泡泡聊天界面。运行结果如下,点击发送按钮,屏幕就显示text的内容。
我也是在网上的一份源码的基础上更改的,整个泡泡界面的实现要点:
(1)主界面其实就是一个list view
(2)文字显示界面其实就使用了android:background="@drawable/incoming"这个东西。背景图片的格式是xxx.9.png,专门用来缩放的,不然显示效果非常差。
(3)自定义了一个adapter,当然是继承android.widget.baseadapter,重写了getview的方法。
整个工程分布如下:
主activity: chatactivity如下:
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
|
package com.tencent;
import android.app.activity;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
import android.widget.edittext;
import android.widget.listview;
import java.util.arraylist;
import java.util.calendar;
public class chatactivity extends activity {
private static final string tag = chatactivity. class .getsimplename();;
private listview talkview;
private button messagebutton;
private edittext messagetext;
// private chatmsgviewadapter myadapter;
private arraylist<chatmsgentity> list = new arraylist<chatmsgentity>();
public void oncreate(bundle savedinstancestate) {
log.v(tag, "oncreate >>>>>>" );
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
talkview = (listview) findviewbyid(r.id.list);
messagebutton = (button) findviewbyid(r.id.messagebutton);
messagetext = (edittext) findviewbyid(r.id.messagetext);
onclicklistener messagebuttonlistener = new onclicklistener() {
@override
public void onclick(view arg0) {
// todo auto-generated method stub
log.v(tag, "onclick >>>>>>>>" );
string name = getname();
string date = getdate();
string msgtext = gettext();
int rid = r.layout.list_say_he_item;
chatmsgentity newmessage = new chatmsgentity(name, date, msgtext, rid);
list.add(newmessage);
// list.add(d0);
talkview.setadapter( new chatmsgviewadapter(chatactivity. this , list));
messagetext.settext( "" );
// myadapter.notifydatasetchanged();
}
};
messagebutton.setonclicklistener(messagebuttonlistener);
}
// shuold be redefine in the future
private string getname() {
return getresources().getstring(r.string.mydisplayname);
}
// shuold be redefine in the future
private string getdate() {
calendar c = calendar.getinstance();
string date = string.valueof(c.get(calendar.year)) + "-"
+ string.valueof(c.get(calendar.month)) + "-" + c.get(c.get(calendar.day_of_month));
return date;
}
// shuold be redefine in the future
private string gettext() {
return messagetext.gettext().tostring();
}
public void ondestroy() {
log.v(tag, "ondestroy>>>>>>" );
// list = null;
super .ondestroy();
}
}
|
显示消息体的定义
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
|
package com.tencent;
public class chatmsgentity {
private static final string tag = chatmsgentity. class .getsimplename();
private string name;
private string date;
private string text;
private int layoutid;
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 gettext() {
return text;
}
public void settext(string text) {
this .text = text;
}
public int getlayoutid() {
return layoutid;
}
public void setlayoutid( int layoutid) {
this .layoutid = layoutid;
}
public chatmsgentity() {
}
public chatmsgentity(string name, string date, string text, int layoutid) {
super ();
this .name = name;
this .date = date;
this .text = text;
this .layoutid = layoutid;
}
}
|
chatmsgviewadapter定义如下:
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
|
package com.tencent;
import android.content.context;
import android.database.datasetobserver;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.linearlayout;
import android.widget.textview;
import java.util.arraylist;
public class chatmsgviewadapter extends baseadapter {
private static final string tag = chatmsgviewadapter. class .getsimplename();
private arraylist<chatmsgentity> coll;
private context ctx;
public chatmsgviewadapter(context context, arraylist<chatmsgentity> coll) {
ctx = context;
this .coll = coll;
}
public boolean areallitemsenabled() {
return false ;
}
public boolean isenabled( int arg0) {
return false ;
}
public int getcount() {
return coll.size();
}
public object getitem( int position) {
return coll.get(position);
}
public long getitemid( int position) {
return position;
}
public int getitemviewtype( int position) {
return position;
}
public view getview( int position, view convertview, viewgroup parent) {
log.v(tag, "getview>>>>>>>" );
chatmsgentity entity = coll.get(position);
int itemlayout = entity.getlayoutid();
linearlayout layout = new linearlayout(ctx);
layoutinflater vi = (layoutinflater) ctx.getsystemservice(context.layout_inflater_service);
vi.inflate(itemlayout, layout, true );
textview tvname = (textview) layout.findviewbyid(r.id.messagedetail_row_name);
tvname.settext(entity.getname());
textview tvdate = (textview) layout.findviewbyid(r.id.messagedetail_row_date);
tvdate.settext(entity.getdate());
textview tvtext = (textview) layout.findviewbyid(r.id.messagedetail_row_text);
tvtext.settext(entity.gettext());
return layout;
}
public int getviewtypecount() {
return coll.size();
}
public boolean hasstableids() {
return false ;
}
public boolean isempty() {
return false ;
}
public void registerdatasetobserver(datasetobserver observer) {
}
public void unregisterdatasetobserver(datasetobserver observer) {
}
}
|
布局文件看得我比较痛苦,这个布局文件不好搞啊,呵呵
完整实例代码代码点击此处本站下载。
希望本文所述对大家android程序设计有所帮助。