Android泡泡聊天界面的实现

来源:互联网 发布:高级编程语言 自然语言 编辑:程序博客网 时间:2024/05/01 14:59

Android泡泡聊天界面的实现

分类: Android学习 5523人阅读 评论(11) 收藏 举报
androidstringdatelistviewlayoutcalendar


昨天写了个界面,实现了Android泡泡聊天界面。运行结果如下,点击发送按钮,屏幕就显示Text的内容。


我也是在网上的一份源码的基础上更改的,整个泡泡界面的实现要点:

(1)主界面其实就是一个List View 

       (2)文字显示界面其实就使用了android:background="@drawable/incoming"这个东西。背景图片的格式是xxx.9.png,专门用来缩放的,不然显示效果非常差。

        (3)自定义了一个adapter,当然是继承android.widget.BaseAdapter,重写了getView的方法。

整个工程分布如下:


主activity: ChatActivity如下:

[java] view plaincopy
  1. package com.tencent;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.util.Log;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. import android.widget.ListView;  
  11.   
  12. import java.util.ArrayList;  
  13. import java.util.Calendar;  
  14.   
  15. public class ChatActivity extends Activity {  
  16.     private static final String TAG = ChatActivity.class.getSimpleName();;  
  17.   
  18.     private ListView talkView;  
  19.   
  20.     private Button messageButton;  
  21.   
  22.     private EditText messageText;  
  23.   
  24.     // private ChatMsgViewAdapter myAdapter;  
  25.   
  26.     private ArrayList<ChatMsgEntity> list = new ArrayList<ChatMsgEntity>();  
  27.   
  28.     public void onCreate(Bundle savedInstanceState) {  
  29.         Log.v(TAG, "onCreate >>>>>>");  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.main);  
  32.   
  33.         talkView = (ListView) findViewById(R.id.list);  
  34.         messageButton = (Button) findViewById(R.id.MessageButton);  
  35.         messageText = (EditText) findViewById(R.id.MessageText);  
  36.         OnClickListener messageButtonListener = new OnClickListener() {  
  37.   
  38.             @Override  
  39.             public void onClick(View arg0) {  
  40.                 // TODO Auto-generated method stub  
  41.                 Log.v(TAG, "onclick >>>>>>>>");  
  42.                 String name = getName();  
  43.                 String date = getDate();  
  44.                 String msgText = getText();  
  45.                 int RId = R.layout.list_say_he_item;  
  46.   
  47.                 ChatMsgEntity newMessage = new ChatMsgEntity(name, date, msgText, RId);  
  48.                 list.add(newMessage);  
  49.                 // list.add(d0);  
  50.                 talkView.setAdapter(new ChatMsgViewAdapter(ChatActivity.this, list));  
  51.                 messageText.setText("");  
  52.                 // myAdapter.notifyDataSetChanged();  
  53.             }  
  54.   
  55.         };  
  56.         messageButton.setOnClickListener(messageButtonListener);  
  57.     }  
  58.   
  59.     // shuold be redefine in the future  
  60.     private String getName() {  
  61.         return getResources().getString(R.string.myDisplayName);  
  62.     }  
  63.   
  64.     // shuold be redefine in the future  
  65.     private String getDate() {  
  66.         Calendar c = Calendar.getInstance();  
  67.         String date = String.valueOf(c.get(Calendar.YEAR)) + "-"  
  68.                 + String.valueOf(c.get(Calendar.MONTH)) + "-" + c.get(c.get(Calendar.DAY_OF_MONTH));  
  69.         return date;  
  70.     }  
  71.   
  72.     // shuold be redefine in the future  
  73.     private String getText() {  
  74.         return messageText.getText().toString();  
  75.     }  
  76.   
  77.     public void onDestroy() {  
  78.         Log.v(TAG, "onDestroy>>>>>>");  
  79.         // list = null;  
  80.         super.onDestroy();  
  81.     }  
  82. }  

显示消息体的定义

[java] view plaincopy
  1. package com.tencent;  
  2.   
  3. public class ChatMsgEntity {  
  4.     private static final String TAG = ChatMsgEntity.class.getSimpleName();  
  5.   
  6.     private String name;  
  7.   
  8.     private String date;  
  9.   
  10.     private String text;  
  11.   
  12.     private int layoutID;  
  13.   
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.   
  18.     public void setName(String name) {  
  19.         this.name = name;  
  20.     }  
  21.   
  22.     public String getDate() {  
  23.         return date;  
  24.     }  
  25.   
  26.     public void setDate(String date) {  
  27.         this.date = date;  
  28.     }  
  29.   
  30.     public String getText() {  
  31.         return text;  
  32.     }  
  33.   
  34.     public void setText(String text) {  
  35.         this.text = text;  
  36.     }  
  37.   
  38.     public int getLayoutID() {  
  39.         return layoutID;  
  40.     }  
  41.   
  42.     public void setLayoutID(int layoutID) {  
  43.         this.layoutID = layoutID;  
  44.     }  
  45.   
  46.     public ChatMsgEntity() {  
  47.     }  
  48.   
  49.     public ChatMsgEntity(String name, String date, String text, int layoutID) {  
  50.         super();  
  51.         this.name = name;  
  52.         this.date = date;  
  53.         this.text = text;  
  54.         this.layoutID = layoutID;  
  55.     }  
  56.   
  57. }  

ChatMsgViewAdapter定义如下:

[java] view plaincopy
  1. package com.tencent;  
  2.   
  3. import android.content.Context;  
  4. import android.database.DataSetObserver;  
  5.   
  6. import android.util.Log;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10.   
  11. import android.widget.BaseAdapter;  
  12. import android.widget.LinearLayout;  
  13. import android.widget.TextView;  
  14.   
  15. import java.util.ArrayList;  
  16.   
  17. public class ChatMsgViewAdapter extends BaseAdapter {  
  18.     private static final String TAG = ChatMsgViewAdapter.class.getSimpleName();  
  19.   
  20.     private ArrayList<ChatMsgEntity> coll;  
  21.   
  22.     private Context ctx;  
  23.   
  24.     public ChatMsgViewAdapter(Context context, ArrayList<ChatMsgEntity> coll) {  
  25.         ctx = context;  
  26.         this.coll = coll;  
  27.     }  
  28.   
  29.     public boolean areAllItemsEnabled() {  
  30.         return false;  
  31.     }  
  32.   
  33.     public boolean isEnabled(int arg0) {  
  34.         return false;  
  35.     }  
  36.   
  37.     public int getCount() {  
  38.         return coll.size();  
  39.     }  
  40.   
  41.     public Object getItem(int position) {  
  42.         return coll.get(position);  
  43.     }  
  44.   
  45.     public long getItemId(int position) {  
  46.         return position;  
  47.     }  
  48.   
  49.     public int getItemViewType(int position) {  
  50.         return position;  
  51.     }  
  52.   
  53.     public View getView(int position, View convertView, ViewGroup parent) {  
  54.         Log.v(TAG, "getView>>>>>>>");  
  55.         ChatMsgEntity entity = coll.get(position);  
  56.         int itemLayout = entity.getLayoutID();  
  57.   
  58.         LinearLayout layout = new LinearLayout(ctx);  
  59.         LayoutInflater vi = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  60.         vi.inflate(itemLayout, layout, true);  
  61.   
  62.         TextView tvName = (TextView) layout.findViewById(R.id.messagedetail_row_name);  
  63.         tvName.setText(entity.getName());  
  64.   
  65.         TextView tvDate = (TextView) layout.findViewById(R.id.messagedetail_row_date);  
  66.         tvDate.setText(entity.getDate());  
  67.   
  68.         TextView tvText = (TextView) layout.findViewById(R.id.messagedetail_row_text);  
  69.         tvText.setText(entity.getText());  
  70.         return layout;  
  71.     }  
  72.   
  73.     public int getViewTypeCount() {  
  74.         return coll.size();  
  75.     }  
  76.   
  77.     public boolean hasStableIds() {  
  78.         return false;  
  79.     }  
  80.   
  81.     public boolean isEmpty() {  
  82.         return false;  
  83.     }  
  84.   
  85.     public void registerDataSetObserver(DataSetObserver observer) {  
  86.     }  
  87.   
  88.     public void unregisterDataSetObserver(DataSetObserver observer) {  
  89.     }  
  90. }  

布局文件看得我比较痛苦,这个布局文件不好搞啊,呵呵

整个工程资源文件,我已经上传到资源共享里面了。

请大家这里下载:

http://download.csdn.net/user/randyjiawenjie