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ā)送按鈕 的點(diǎn)擊事件里獲取了 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)文章
Android內(nèi)存優(yōu)化操作方法梳理總結(jié)
這篇文章主要介紹了Android 內(nèi)存優(yōu)化知識點(diǎn)梳理總結(jié),Android 操作系統(tǒng)給每個進(jìn)程都會分配指定額度的內(nèi)存空間,App 使用內(nèi)存來進(jìn)行快速的文件訪問交互,長時間如此便需要優(yōu)化策略,文章分享優(yōu)化知識點(diǎn)總結(jié),需要的朋友可以參考一下2022-11-11
Android Studio 配置:自定義頭部代碼注釋及添加模版方式
這篇文章主要介紹了Android Studio 配置:自定義頭部代碼注釋及添加模版方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android ScrollView的頂部下拉和底部上拉回彈效果
本篇文章主要介紹了Android ScrollView的頂部下拉和底部上拉回彈效果,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Android App中ViewPager所帶來的滑動沖突問題解決方法
Android中我們經(jīng)常使用ViewPager配合Fragment實現(xiàn)視圖滑動,但在實際操作中又會經(jīng)常發(fā)生方向上的沖突問題,這里我們就來總結(jié)一下Android App中ViewPager所帶來的滑動沖突問題解決方法:2016-06-06
Android基于service實現(xiàn)音樂的后臺播放功能示例
這篇文章主要介紹了Android基于service實現(xiàn)音樂的后臺播放功能,結(jié)合實例形式分析了Android基于Service組件實現(xiàn)多媒體音頻播放功能的步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
Android編程實現(xiàn)自定義Dialog的大小自動控制方法示例
這篇文章主要介紹了Android編程實現(xiàn)自定義Dialog的大小自動控制方法,結(jié)合實例形式分析了Android自定義Dialog對話框的屬性操作技巧與大小動態(tài)控制實現(xiàn)方法,需要的朋友可以參考下2017-09-09

