Android聊天软件界面开发
<pre style="background-color:#ffffff;color:#000000;font-family:'Courier New';font-size:15.0pt;"><pre class="html" name="code"><span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<!--主布局(纵向排列)-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:background="@drawable/chat_bg"><!--聊天对话框部分android:divider="#0000"透明分隔线--><ListViewandroid:id="@+id/msg_list_view"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:divider="#0000"/><!--输入框部分(横排排列)--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><!--输入框--><EditTextandroid:id="@+id/input_text"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:hint="Type something here"android:maxLines="2"/><!--发送按钮--><Buttonandroid:id="@+id/send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAllCaps="false"android:text="Send"/></LinearLayout></LinearLayout>
</span>
<pre style="background-color:#ffffff;color:#000000;font-family:'Courier New';font-size:15.0pt;"><span><span><span><span style="font-size:18px;"><span style="font-size:24px;"><strong><span style="color:#cc33cc;">2.msg</span></strong><span style="font-size:24px;"><strong><span style="color:#cc33cc;">_layout.xml</span></strong></span></span></span></span></span></span>
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<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"><!--左chat--><LinearLayoutandroid:id="@+id/left_layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="left"android:background="@drawable/chat_ed_left"><TextViewandroid: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><!--右chat--><LinearLayoutandroid:id="@+id/right_layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right"android:background="@drawable/chat_ed_right"><TextViewandroid:id="@+id/right_msg"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="10dp"android:textColor="#a40d88"/></LinearLayout></LinearLayout>
</span>
3.Msg.java
<span style="font-size:14px;">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;}
}</span>
4.MsgAdapter.java
<span style="font-size:14px;">package com.example.cpj.UICustomerViews;import android.content.Context;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;import java.util.List;
import com.example.cpj.uiwidgettest.R;/*** Created by cpj on 2016/3/15.*/
public class MsgAdapter extends ArrayAdapter<Msg>{private int resourceId;public MsgAdapter(Context context, int textViewResourceId,List<Msg> objects){super(context, textViewResourceId, objects);resourceId = textViewResourceId;}/*** 功能描述:这个方法在每个子项滚动到屏幕内的时候被调用* 1.首先通过 getItem()获取当前项的ToolBar实例* 2.使用LayoutInflater将这个子布局项加载并传入我们的主布局* 3.调用View的findViewById()分别获取到左右Layout、左右Msg的实例* 4.调用它们的setText()来显示文字* 5.最后返回布局* */public View getView(int position, View convertView, ViewGroup parent){Msg msg = getItem(position);/** 新增内部类ViewHolder,用于对控件的实例进行缓存。* 1.当convertView为空时,创建一个ViewHolder对象,并将控件的实例对象存放在ViewHolder里。* 2.然后调用View的setTag()方法,将ViewHolder对象存储在View中。* 3.当convertView不为空时,调用View的getTag()方法把ViewHolder重新取出来。* */View view;ViewHolder viewHolder;/*加载自定义布局与控件实例*/if(convertView == null){view = LayoutInflater.from(getContext()).inflate(resourceId, null);//创建控件实例并进行缓存viewHolder = new ViewHolder();viewHolder.leftLayout = (LinearLayout) view.findViewById(R.id.left_layout);viewHolder.rightLayout = (LinearLayout) view.findViewById(R.id.right_layout);viewHolder.leftMsg = (TextView) view.findViewById(R.id.left_msg);viewHolder.rightMsg = (TextView) view.findViewById(R.id.right_msg);view.setTag(viewHolder);} else {//convertView参数用于将之前加载好的布局进行缓存,以便之后可以进行复用(提高效率)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){viewHolder.rightLayout.setVisibility(View.VISIBLE);viewHolder.leftLayout.setVisibility(View.GONE);viewHolder.rightMsg.setText(msg.getContent());}return view;}//新增内部类ViewHolder,用于对控件的实例进行缓存。class ViewHolder{LinearLayout leftLayout;LinearLayout rightLayout;TextView leftMsg;TextView rightMsg;}
}
</span>
5.ChatActivity.java
<span style="font-size:14px;">package com.example.cpj.UICustomerViews;import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;import com.example.cpj.uiwidgettest.BaseActivity;
import com.example.cpj.uiwidgettest.R;import java.util.ArrayList;
import java.util.List;/*** Created by cpj on 2016/3/15.*/
public class ChatActivity extends BaseActivity{private ListView msgListView;private EditText inputText;private Button send;private MsgAdapter adapter;private List<Msg> msgList = new ArrayList<Msg>();protected void onCreate(Bundle saveInstanceState){super.onCreate(saveInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.chatmain_layout);initMsgs();//初始化消息数据</span><span style="font-size:14px;">adapter = new MsgAdapter(ChatActivity.this, R.layout.msg_layout, msgList);inputText = (EditText)findViewById(R.id.input_text);send = (Button)findViewById(R.id.send);msgListView = (ListView)findViewById(R.id.msg_list_view);msgListView.setAdapter(adapter);//发送按钮的点击事件send.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String content = inputText.getText().toString();if(!"".equals(content)){Msg msg = new Msg(content, Msg.TYPE_SENT);msgList.add(msg);adapter.notifyDataSetChanged();//当有消息时刷新msgListView.setSelection(msgList.size());//将ListView定位到最后一行inputText.setText("");//清空输入框的内容}}});}/*** 初始化消息数据* */private void initMsgs(){Msg msg1 = new Msg("Hello cpj.", 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 pengpeng,Nice talking to you.", Msg.TYPE_RECEIVED);msgList.add(msg3);}
}
</span>
三、思路回顾:
Android聊天软件界面开发相关推荐
- Android聊天软件的开发(七)--聊天通信
聊天通信通过Socket实现,大概的框架如下图: 通信流程: 1.服务器在启动时开启聊天服务线程 可以通过ServletContextListener监听Servlet的初始化和销毁,来开启和关闭聊天 ...
- Android聊天软件的开发--聊天通信
Android聊天软件的开发(七)--聊天通信 2014-06-20 23:17:49CSDN-vaintwyt-点击数:338 聊天通信通过Socket实现,大概的框架如下图: 通信流程: 1.服 ...
- Android聊天软件的开发(四)--通讯录
一,好友排序 好友排序是按照昵称拼音进行A-Z排序.效果如下图: 对好友昵称进行排序,需要先将首字转换为ASCII码,然后根据ASCII码得到对应的拼音,最后根据拼音进行A-Z排序.点 ...
- Android聊天软件的开发(六)--表情
表情用于聊天对话的输入,实现的原理主要是:在EditText或TextView中,使用SpannableString,将特定字符串替换为图片. 首先,我们可以规定,表情的字符串为[**],图片名称为s ...
- Android聊天软件的开发(二)--数据库
一,服务器数据库 服务器端的数据库是MySQL,使用Hibernate实现数据的增删改查.主要存储的数据有:用户信息,好友列表. 其中,好友列表中的friend_list ...
- Android聊天软件的开发(三)--网络连接
一,服务器网络接口 服务器网络接口通过Servlet实现,可以获得客户端提交的数据,对数据进行查询存储操作,以及返回结果数据给客户端.客户端可以通过HTTP协议直接访问网络接口. HTTP ...
- 开发简单Android聊天软件(6)
构建完整消息接收加载流程 在 "开发简单Android聊天软件(5)" 中,完成了会话窗口的绘制,以及消息发送.现在我们来完成消息接收和加载,构建一个完整的聊天流程. 消息加载,那 ...
- 【基于Android聊天软件开发-哔哩哔哩】 https://b23.tv/109B82i
[基于Android聊天软件开发-哔哩哔哩] https://b23.tv/109B82i https://b23.tv/109B82i Android聊天软件开发(仿QQ) 要求: Android ...
- 开发简单Android聊天软件(1)
总体介绍 开篇 大概思路 一. 客户端主要依赖 二.包引用完成后,创建wsClient类. 三.连接成功后,就可以在对于业务逻辑调用以下方法开始发送消息 开篇 本人是一位开发新人,将自己的开发学习过程 ...
最新文章
- 【怎样写代码】偷窥高手 -- 反射技术(四):深入窥视属性
- 正则表达式匹配分组(|、(ab)、\num、(?P<name>)(?P=name))
- Python 学习笔记 - 字典
- c++入门代码_Golang Gin 实战(一)| 快速安装入门
- insmod 和modprobe的区别
- JavaScript基础:(加号,数值转换,布尔转换)
- php协程实现mysql异步_swoole与php协程实现异步非阻塞IO开发
- Enterprise Library 5.0 开发向导- 简介(1)
- 中石油训练赛 - Edit Distance(思维+构造)
- PHP简单实现单点登录功能示例
- android launcher 字体大小,Android6.0 Launcher3 修改app字体大小
- EtherCAT是什么?
- python抓取贴吧_python抓取百度贴吧-校花吧,网页图片
- android网格布局间距,android – 删除回收站视图网格布局中的默认间距
- WEB自动化(Python+selenium)的API
- 信号与系统(六)z变换
- head first系列pdf下载
- 前台获取服务器端的值
- 用Unity进行网络游戏开发(一)
- 二分图最大匹配与其应用
热门文章
- javase 双色球选7球 红球在【1-36】之间,选出6个,且不能重复 篮球在【1-16】之间,选出1个
- python打印数组部分元素_Python打印输出数组中全部元素
- 阿里巴巴融资破17亿美金创纪录
- 基于Nodejs的知识信息分享平台的设计和实现
- 最新好看的HYBBS程序极品论坛模板带插件
- 某电子计算机主机,2018年南开大学数学科学学院432统计学[专业硕士]之概率论与数理统计教程考研仿真模拟五套题...
- 成功处理挖矿病毒劫持,crontab注入顽固脚本,cpu、内存飙升
- Codeforces Round #683 (Div. 2) C. Knapsack(贪心)
- java false 0_Java中false指的是1还是0
- Unity和安卓(二):接入 google 广告