Android泡泡聊天界面的实现
来源:互联网 发布:高级编程语言 自然语言 编辑:程序博客网 时间:2024/05/01 14:59
Android泡泡聊天界面的实现
分类: Android学习2011-08-11 14:04 5523人阅读 评论(11) 收藏 举报
androidstringdatelistviewlayoutcalendar
昨天写了个界面,实现了Android泡泡聊天界面。运行结果如下,点击发送按钮,屏幕就显示Text的内容。
我也是在网上的一份源码的基础上更改的,整个泡泡界面的实现要点:
(1)主界面其实就是一个List View
(2)文字显示界面其实就使用了android:background="@drawable/incoming"这个东西。背景图片的格式是xxx.9.png,专门用来缩放的,不然显示效果非常差。
(3)自定义了一个adapter,当然是继承android.widget.BaseAdapter,重写了getView的方法。
整个工程分布如下:
主activity: ChatActivity如下:
- 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();
- }
- }
显示消息体的定义
- 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定义如下:
- 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) {
- }
- }
布局文件看得我比较痛苦,这个布局文件不好搞啊,呵呵
整个工程资源文件,我已经上传到资源共享里面了。
请大家这里下载:
http://download.csdn.net/user/randyjiawenjie
- Android泡泡聊天界面的实现
- Android泡泡聊天界面的实现
- 关于android 聊天页面 类似泡泡包裹聊天内容的实现
- Android 实现聊天界面
- Android聊天界面-实现微信式聊天的技巧
- Android聊天界面实现方式
- Android开发学习之仿微信聊天界面的实现
- 【android】聊天界面的制作-简易版实现
- 【Android自定义控件】聊天界面录音按钮的实现
- Android学习之仿QQ聊天界面的实现
- 【Android自定义控件】聊天界面录音按钮的实现
- [Android通信]基于socket的聊天app(二):实现聊天界面
- iPhone开发:类似iChat的聊天泡泡
- android 实现微信聊天界面效果
- Android 多媒体之实现语音聊天界面
- android listview实现聊天界面随记
- android模拟聊天界面recyclerview实现
- android模拟聊天界面Listview实现
- 软件工程初步总结
- PHP编程遇到的小问题
- 如何配置环境变量(jdk1.7)
- coreData入门基本知识(1)
- Servlet获取页面数据部分出现乱码
- Android泡泡聊天界面的实现
- 【重点:Lock】把sychronized改为lock的方法
- find out gcc/clang head file search paths
- java级联删除
- hibernate中session的get跟load方法的比较【转载】
- <c:forEach varStatus="status">中 varStatus的属性简介
- declare global data ptr
- Linux环境下段错误的产生原因及调试方法小结
- Android SDK更新以及ADT更新出现问题的解决办法