Android泡泡聊天界面的实现及源码


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

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

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

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

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

整个工程分布如下:

主activity: ChatActivity如下:

  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. }  

 

 

 

 

  • 1
  • 2
  • 下一页

相关内容