網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Android實(shí)現(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>
這里在主界面中放置了一個(gè) ListView用于顯示聊天的消息內(nèi)容,又放置了一個(gè) EditText 用于輸入消息,還放置了一個(gè) Button 用于發(fā)送消息。ListView 中用到了一個(gè) 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類中只有兩個(gè)字段,content表示消息的內(nèi)容,type表示消息的類型。其中消息類型 有兩個(gè)值可選,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ā)出的消息 都放在同一個(gè)布局里呢?不用擔(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)有新消息時(shí),刷新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)建出一個(gè)新的 Msg對象,并把 它添加到 msgList列表中去。之后又調(diào)用了適配器的 notifyDataSetChanged()方法,用于通知 列表的數(shù)據(jù)發(fā)生了變化,這樣新增的一條消息才能夠在 ListView中顯示。接著調(diào)用 ListView 的 setSelection()方法將顯示的數(shù)據(jù)定位到最后一行,以保證一定可以看得到最后發(fā)出的一條 消息。最后調(diào)用 EditText的 setText()方法將輸入的內(nèi)容清空。
6、效果圖
原文鏈接:https://blog.csdn.net/chenliguan/article/details/48750547
相關(guān)推薦
- 2023-06-17 詳解Flask數(shù)據(jù)庫的連接與使用_python
- 2022-08-25 python數(shù)學(xué)建模之三大模型與十大常用算法詳情_python
- 2022-12-27 python中g(shù)etopt()函數(shù)用法詳解_python
- 2022-04-02 python3?QT5?端口轉(zhuǎn)發(fā)工具兩種場景分析_python
- 2022-07-16 windows安全加固--關(guān)閉非必要端口
- 2022-06-15 oracle多表簡單查詢實(shí)例代碼_oracle
- 2023-10-15 #css# 超出高度,可上下滾動
- 2022-04-11 socket連接關(guān)閉問題分析_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支