編輯:關於 Android 程式設計
本文透過一個完整範例,帶大家動手打造類似即時通訊軟體的「泡泡」對話介面。點擊發送按鈕後,輸入的文字會立刻以對話框的形式呈現在畫面上。

這個實作是在網路上既有範例的基礎上修改而成,整個泡泡介面的核心要點可以歸納為三項:
(1)主介面本質上就是一個 ListView;
(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 程式設計有所幫助。
Android 分頁加載(上拉加載更多)實作
分頁加載在如今的 apk 中應用廣泛,那麼它是如何實現的呢?下面透過一個小例子來看看。首先是自訂的 ListView,在布局中引用,先看一下布局文件: 下面是自訂
Android Otto 事件總線框架入門:用於解耦元件溝通
這次要介紹的是 Otto——Android 上被廣泛使用的事件總線(EventBus)框架,由 Square 出品。官網:https://square.github.io
Android 學習路線(十五):Activity 生命週期之重新創建
有幾種情況會導致 Activity 在正常流程下被摧毀:使用者按下返回鍵,或者 Activity 自行呼叫 finish() 宣告結束。此外,若 Activity 已停止
Android 仿淘寶預訂日曆實作(完整範例)
本文分享一個仿淘寶「預訂日期選擇」的實作:使用者可以設定可選天數,日曆會自動標出今天、明天、後天,並把上次下單的日期標記起來。以下依檔案說明重點程式碼。 MainActi