Android實現(xiàn)精美的聊天界面
本文實例為大家分享了Android實現(xiàn)精美的聊天界面的具體代碼,供大家參考,具體內(nèi)容如下
1、activity_chat.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:background="#d8e0e8" ? ? android:orientation="vertical"> ? ? <ListView ? ? ? ? android:id="@+id/msg_list_view" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="0dp" ? ? ? ? android:layout_weight="1" ? ? ? ? android:divider="#0000"></ListView> ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content"> ? ? ? ? <EditText ? ? ? ? ? ? android:id="@+id/input_text" ? ? ? ? ? ? android:layout_width="0dp" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:hint="Type somthing here" ? ? ? ? ? ? android:maxLines="2" /> ? ? ? ? <Button ? ? ? ? ? ? android:id="@+id/send" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:text="Send" /> ? ? </LinearLayout> </LinearLayout>
這里在主界面中放置了一個 ListView用于顯示聊天的消息內(nèi)容,又放置了一個 EditText 用于輸入消息,還放置了一個 Button 用于發(fā)送消息。ListView 中用到了一個 android:divider 屬性,它可以指定 ListView分隔線的顏色,這里#0000表示將分隔線設(shè)為透明色
2、Msg.java
package com.example.guan.chat; /** ?* @author Guan ?* @file com.example.guan.chat ?* @date 2015/8/21 ?* @Version 1.0 ?*/ public class Msg { ? ? public static final int TYPE_RECEIVED = 0; ? ? public static final int TYPE_SENT = 1; ? ? private String content; ? ? private int type; ? ? public Msg(String content, int type) { ? ? ? ? this.content = content; ? ? ? ? this.type = type; ? ? } ? ? public String getContent() { ? ? ? ? return content; ? ? } ? ? public int getType() { ? ? ? ? return type; ? ? } }
Msg類中只有兩個字段,content表示消息的內(nèi)容,type表示消息的類型。其中消息類型 有兩個值可選,TYPE_RECEIVED表示這是一條收到的消息,TYPE_SENT 表示這是一條發(fā) 出的消息。
3、 msg_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:orientation="vertical" ? ? android:padding="10dp"> ? ? <LinearLayout ? ? ? ? android:id="@+id/left_layout" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="left" ? ? ? ? android:background="@drawable/chatto_bg_normal"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/left_msg" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? android:layout_margin="10dp" ? ? ? ? ? ? android:textColor="#fff" /> ? ? </LinearLayout> ? ? <LinearLayout ? ? ? ? android:id="@+id/right_layout" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_gravity="right" ? ? ? ? android:background="@drawable/chatfrom_bg_normal"> ? ? ? ? <TextView ? ? ? ? ? ? android:id="@+id/right_msg" ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? android:layout_margin="10dp" /> ? ? </LinearLayout> </LinearLayout>
這里我們讓收到的消息居左對齊,發(fā)出的消息居右對齊,并且分別使用 message_left.9.png 和 message_right.9.png作為背景圖。你可能會有些疑慮,怎么能讓收到的消息和發(fā)出的消息 都放在同一個布局里呢?不用擔(dān)心,還記得我們前面學(xué)過的可見屬性嗎,只要稍后在代碼中 根據(jù)消息的類型來決定隱藏和顯示哪種消息就可以了。
4、MsgAdapte.java
/** ?* @author Guan ?* @file com.example.guan.chat ?* @date 2015/8/21 ?* @Version 1.0 ?*/ public class MsgAdapter extends ArrayAdapter<Msg> { ? ? private int resourceId; ? ? public MsgAdapter(Context context, int textViewResourceId, List<Msg> objects) { ? ? ? ? super(context, textViewResourceId, objects); ? ? } ? ? @Override ? ? public View getView(int position, View convertView, ViewGroup parent) { ? ? ? ? Msg msg = getItem(position); ? ? ? ? View view; ? ? ? ? ViewHolder viewHolder; ? ? ? ? if (convertView == null) { ? ? ? ? ? ? view = LayoutInflater.from(getContext()).inflate(R.layout.msg_item, null); ? ? ? ? ? ? viewHolder = new ViewHolder(view); ? ? ? ? ? ? view.setTag(viewHolder); ? ? ? ? } else { ? ? ? ? ? ? view = convertView; ? ? ? ? ? ? viewHolder = (ViewHolder) view.getTag(); ? ? ? ? } ? ? ? ? if (msg.getType() == Msg.TYPE_RECEIVED) { ? ? ? ? ? ? // 如果是收到的消息,則顯示左邊的消息布局,將右邊的消息布局隱藏 ? ? ? ? ? ? viewHolder.leftLayout.setVisibility(View.VISIBLE); ? ? ? ? ? ? viewHolder.rightLayout.setVisibility(View.GONE); ? ? ? ? ? ? viewHolder.leftMsg.setText(msg.getContent()); ? ? ? ? } else if (msg.getType() == Msg.TYPE_SENT) { ? ? ? ? ? ? // 如果是發(fā)出的消息,則顯示右邊的消息布局,將左邊的消息布局隱藏 ? ? ? ? ? ? viewHolder.rightLayout.setVisibility(View.VISIBLE); ? ? ? ? ? ? viewHolder.leftLayout.setVisibility(View.GONE); ? ? ? ? ? ? viewHolder.rightMsg.setText(msg.getContent()); ? ? ? ? } ? ? ? ? return view; ? ? } ? ? static class ViewHolder { ? ? ? ? @InjectView(R.id.left_msg) ? ? ? ? TextView leftMsg; ? ? ? ? @InjectView(R.id.left_layout) ? ? ? ? LinearLayout leftLayout; ? ? ? ? @InjectView(R.id.right_msg) ? ? ? ? TextView rightMsg; ? ? ? ? @InjectView(R.id.right_layout) ? ? ? ? LinearLayout rightLayout; ? ? ? ? ViewHolder(View view) { ? ? ? ? ? ? ButterKnife.inject(this, view); ? ? ? ? } ? ? } }
在 getView()方法中增加了對消息類型的判斷。如果這條消息是收到的,則顯示左邊的消 息布局,如果這條消息是發(fā)出的,則顯示右邊的消息布局。
5、ChatActivity.java
/** ?* @author Guan ?* @file com.example.guan.ChatActivity ?* @date 2015/8/21 ?* @Version 1.0 ?*/ public class ChatActivity extends Activity { ? ? @InjectView(R.id.msg_list_view) ? ? ListView msgListView; ? ? @InjectView(R.id.input_text) ? ? EditText inputText; ? ? @InjectView(R.id.send) ? ? Button send; ? ? private MsgAdapter adapter; ? ? private List<Msg> msgList = new ArrayList<Msg>(); ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_chat); ? ? ? ? ButterKnife.inject(this); ? ? ? ? // 初始化消息數(shù)據(jù) ? ? ? ? initMsgs(); ? ? ? ? adapter = new MsgAdapter(ChatActivity.this,R.layout.msg_item, msgList); ? ? ? ? msgListView.setAdapter(adapter); ? ? ? ? send.setOnClickListener(new View.OnClickListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onClick(View v) { ? ? ? ? ? ? ? ? String content = inputText.getText().toString(); ? ? ? ? ? ? ? ? if (!"".equals(content)) { ? ? ? ? ? ? ? ? ? ? Msg msg = new Msg(content, Msg.TYPE_SENT); ? ? ? ? ? ? ? ? ? ? msgList.add(msg); ? ? ? ? ? ? ? ? ? ? // 當(dāng)有新消息時,刷新ListView中的顯示 ? ? ? ? ? ? ? ? ? ? adapter.notifyDataSetChanged(); ? ? ? ? ? ? ? ? ? ? // 將ListView定位到最后一行 ? ? ? ? ? ? ? ? ? ? msgListView.setSelection(msgList.size()); ? ? ? ? ? ? ? ? ? ? // 清空輸入框中的內(nèi)容 ? ? ? ? ? ? ? ? ? ? inputText.setText(""); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? private void initMsgs() { ? ? ? ? Msg msg1 = new Msg("Hello guy.", Msg.TYPE_RECEIVED); ? ? ? ? msgList.add(msg1); ? ? ? ? Msg msg2 = new Msg("Hello. Who is that?", Msg.TYPE_SENT); ? ? ? ? msgList.add(msg2); ? ? ? ? Msg msg3 = new Msg("This is Tom. Nice talking to you. ", Msg.TYPE_RECEIVED); ? ? ? ? msgList.add(msg3); ? ? } }
在 initMsgs()方法中我們先初始化了幾條數(shù)據(jù)用于在 ListView 中顯示。然后在發(fā)送按鈕 的點擊事件里獲取了 EditText中的內(nèi)容,如果內(nèi)容不為空則創(chuàng)建出一個新的 Msg對象,并把 它添加到 msgList列表中去。之后又調(diào)用了適配器的 notifyDataSetChanged()方法,用于通知 列表的數(shù)據(jù)發(fā)生了變化,這樣新增的一條消息才能夠在 ListView中顯示。接著調(diào)用 ListView 的 setSelection()方法將顯示的數(shù)據(jù)定位到最后一行,以保證一定可以看得到最后發(fā)出的一條 消息。最后調(diào)用 EditText的 setText()方法將輸入的內(nèi)容清空。
6、效果圖
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS實現(xiàn)點擊參數(shù)面板按鈕顯示或隱藏數(shù)據(jù)
本文主要介紹JS實現(xiàn)點擊參數(shù)面板按鈕顯示或隱藏數(shù)據(jù)的方法,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03Android中自定義PopupWindow實現(xiàn)彈出框并帶有動畫效果
這篇文章主要介紹了Android中自定義PopupWindow實現(xiàn)彈出框并帶有動畫效果的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09解析在Android中為TextView增加自定義HTML標(biāo)簽的實現(xiàn)方法
本篇文章是對在Android中為TextView增加自定義HTML標(biāo)簽的方法進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05Android實現(xiàn)發(fā)送短信驗證碼倒計時功能示例
本篇文章主要介紹了Android實現(xiàn)發(fā)送短信驗證碼倒計時功能示例,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2017-03-03Android編程實現(xiàn)的EditText彈出打開和關(guān)閉工具類
這篇文章主要介紹了Android編程實現(xiàn)的EditText彈出打開和關(guān)閉工具類,涉及Android輸入框EditText彈出打開和關(guān)閉功能簡單實現(xiàn)技巧,需要的朋友可以參考下2018-02-02實例探究Android開發(fā)中Fragment狀態(tài)的保存與恢復(fù)方法
這篇文章主要介紹了實例探究Android開發(fā)中Fragment狀態(tài)的保存與恢復(fù)方法,或許開發(fā)者們對Fragment的操作都比較熟悉,但onSaveInstanceState()方法并不能夠很好地保存Fragment狀態(tài),需要的朋友可以參考下2016-04-04舉例講解Android應(yīng)用開發(fā)中OTTO框架的基本使用
這篇文章主要介紹了Android應(yīng)用開發(fā)中OTTO框架的基本使用講解,文中舉了創(chuàng)建一個單例模式的應(yīng)用例子,需要的朋友可以參考下2016-02-02