Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android編程實現泡泡聊天界面實例詳解(附源碼)

Android編程實現泡泡聊天界面實例詳解(附源碼)

編輯:關於Android編程

本文實例講述了Android編程實現泡泡聊天界面的方法。分享給大家供大家參考,具體如下:

昨天寫了個界面,實現了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) { 
  } 
} 

布局文件看得我比較痛苦,這個布局文件不好搞啊,呵呵

完整實例代碼代碼點擊此處本站下載。

希望本文所述對大家Android程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved