Android:实现QQ聊天(超简单)
前言
1. 简单定义Msg类
package com.bytedance.sjtu.msgclass Msg(val msg: String, val msgType: Int) {companion object {const val SEND = 0 //定义消息类型,0为发送, 1为收取const val GET = 1}}
2. ChatActivity
class ChatActivity : AppCompatActivity() {private val tvName : TextView by lazy { findViewById(R.id.tvName) }private val recyclerView : RecyclerView by lazy { findViewById(R.id.recyclerView) }private val editMsg : EditText by lazy { findViewById(R.id.editMsg) }private val sendMsg : TextView by lazy { findViewById(R.id.sendMsg) }override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_chat)window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) //设置隐藏状态栏val msgList = mutableListOf(Msg( "你好", Msg.SEND),Msg( "嗯嗯", Msg.GET),Msg( "How are you?", Msg.SEND),Msg( "I'm fine, thanks.", Msg.GET),)recyclerView.layoutManager = LinearLayoutManager(this)recyclerView.adapter = ChatAdapter(this, msgList) //简单初始化聊天信息editMsg.addTextChangedListener(object : TextWatcher { //监听消息输入框的输入状态override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}override fun afterTextChanged(p0: Editable?) {if (editMsg.text.isNotEmpty()) {sendMsg.setTextColor(resources.getColor(R.color.silver_red)) //输入框有内容,发布字体变红色} else {sendMsg.setTextColor(resources.getColor(R.color.light_gray)) //否则变灰色}}})sendMsg.setOnClickListener {if (editMsg.text.isNotEmpty()) {val msgType = Random.nextInt(0, 2) //随机生成 0 和 1msgList.add(Msg(editMsg.text.toString(), msgType)) //模拟“发送消息”和“接收消息”recyclerView.adapter = ChatAdapter(this, msgList) //更新适配器数据recyclerView.scrollToPosition(msgList.size - 6)recyclerView.smoothScrollBy(0, 1000, DecelerateInterpolator(), 1500) //平滑刷新消息界面,有bugeditMsg.text = null //输入框置空}}}
}
3. activity_chat.XML布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:fitsSystemWindows="true"tools:context=".msg.ChatActivity"><FrameLayoutandroid:layout_width="match_parent"android:layout_height="50dp"><ImageViewandroid:id="@+id/imgBack"android:layout_width="50dp"android:layout_height="50dp"android:layout_marginStart="5dp"android:padding="15dp"android:src="@drawable/ic_back" /><TextViewandroid:id="@+id/tvName"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_gravity="center"android:gravity="center"android:text="name"android:textSize="20sp"android:textColor="@color/black" /><Viewandroid:layout_width="match_parent"android:layout_height="0.1dp"android:layout_gravity="bottom"android:background="@color/light_gray" /></FrameLayout><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#EAEDF6" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><EditTextandroid:id="@+id/editMsg"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:layout_margin="10dp"android:padding="12dp"android:hint="输入..."android:textSize="18sp"android:background="@drawable/bg_round_rectangle_2" /><TextViewandroid:id="@+id/sendMsg"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="bottom"android:layout_marginEnd="10dp"android:layout_marginBottom="10dp"android:text="发送"android:textStyle="bold"android:textSize="20sp"android:textColor="@color/light_gray" /></LinearLayout></LinearLayout>
4. ChatAdapter适配器
class ChatAdapter(private val mContext: Context,private val msgList: MutableList<Msg>
): RecyclerView.Adapter<ChatAdapter.ViewHolder>() {override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {val view = LayoutInflater.from(parent.context).inflate(R.layout.layout_chat_item_view, parent, false)return ViewHolder(view)}override fun onBindViewHolder(holder: ViewHolder, position: Int) { //绑定数据if (msgList[position].msgType == Msg.SEND) { //发出的消息holder.msgSend.text = msgList[position].msgholder.msgSend.visibility = View.VISIBLEholder.msgGet.visibility = View.GONE} else if (msgList[position].msgType == Msg.GET) { //收到的消息holder.msgGet.text = msgList[position].msgholder.msgGet.visibility = View.VISIBLEholder.msgSend.visibility = View.GONE}holder.itemView.setOnClickListener {// Toast.makeText(mContext, "You click msg $position", Toast.LENGTH_SHORT).show()}}override fun getItemCount(): Int {return msgList.size}class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {val msgSend : TextView = itemView.findViewById(R.id.msgSend)val msgGet : TextView = itemView.findViewById(R.id.msgGet)}}
5. layout_chat_item_view.XML布局文件
其中TextView的background应该设置为9-Patch文件,也就是.9.png文件,可以自己制作。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginHorizontal="10dp"><TextViewandroid:id="@+id/msgSend"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingBottom="25dp"android:layout_gravity="end"android:text="这是发送的消息"android:textSize="18sp"android:textColor="@color/white"android:background="@drawable/bg_bubble_1_50" /><TextViewandroid:id="@+id/msgGet"android:gravity="start"android:visibility="visible"android:background="@drawable/bg_bubble_2_50"android:layout_width="wrap_content"android:layout_height="wrap_content"android:paddingBottom="25dp"android:text="这是收到的消息"android:textSize="18sp"android:textColor="@color/black" /></FrameLayout>
Android:实现QQ聊天(超简单)相关推荐
- webqq2协议分析和qq聊天机器人简单实现(转)
webqq2协议分析和qq聊天机器人简单实现 转之http://hfutxf.javaeye.com/blog/800866 通过webqq接口,可以实现发送qq消息接收qq消息等,这样,想实现一个q ...
- Android直播app源代码超简单气泡效果
Android直播app源代码超简单气泡效果实现的相关代码 1.1 定义气泡 气泡效果我们关心的属性并不多,主要有这几种:半径.坐标.上升速度.水平平移速度.由于我们只在 View 内部使用,因此直接 ...
- Android学QQ聊天列表展示ListView
Android学QQ聊天列表展示ListView 稍微修改下实现的,比完全copy感觉爽多了 布局文件:activity_main.xml <RelativeLayout xmlns:andro ...
- Android 仿qq聊天界面之一
一.登录界面 本来是只想仿一个qq的聊天界面的,顺便做了一个登录界面,熟悉下SharedPreferences(解释一下:SharedPreferences由于非常适合记录一些零散的简单的数据,因此登 ...
- android仿qq聊天项目点评,android 实现qq聊天对话界面效果
[实例简介] Android UI[android 仿微信.QQ聊天,带表情,可翻页,带翻页拖动缓冲] 博客介绍http://blog.csdn.net/lnb333666/article/detai ...
- Android 实现QQ聊天底部+号显示底部菜单
今天项目需要加一个类似QQ聊天界面,点击+号弹出底部菜单选项.我以为网上有例子,百度了下,不尽人意.所以我就自己实现了一下,这里作为记录,方便以后查看,也给需要的童鞋一个例子. 初始页面 点击加号 点 ...
- webqq2协议分析和qq聊天机器人简单实现
转之http://hfutxf.javaeye.com/blog/800866 通过webqq接口,可以实现发送qq消息接收qq消息等,这样,想实现一个qq聊天机器人,就不是什么难事情了了,下面开始一 ...
- android调用qq聊天功能
String url="mqqwpa://im/chat?chat_type=wpa&uin=2853700237"; startActivity(new Intent(I ...
- android 仿qq聊天背景,安卓手机qq如何修改聊天背景
如何大多数的用户都是使用安卓手机,如果我们想要修改安卓手机qq的聊天背景,应该如何修改呢?下面就让学习啦小编告诉你安卓手机qq如何修改聊天背景,希望对大家有所帮助. 安卓手机qq修改聊天背景的方法 手 ...
- android 仿qq聊天背景,Android,_安卓实现类似QQ刚换聊天背景的功能,Android - phpStudy...
安卓实现类似QQ刚换聊天背景的功能 自己实现了一下,但对于一些手机一设置背景就出现闪退不知道为什么,大体思路就是获得用户选择的uri,然后如果屏幕分辨率小于图片的分辨率就对图片进行下处理,防止OOM. ...
最新文章
- PCB 使用Nginx让IIS7实现负载均衡
- Windows内核加载器概念学习
- ffmpeg入门及java操作ffmpeg对视频进行处理
- Java后端学习路线6大维度详细总结(编程基础+开发工具+应用框架+运维知识+成神之路+平稳降落)【可作为知识点梳理列表】【点击可查看高清原图】
- python scrapy教程实例_Python之scrapy实例1
- 浅谈HTTPS以及Fiddler抓取HTTPS协议
- c语言存储学生信息并显示,C语言实现学生信息管理程序
- mysql防止预约重号_mysql 防止重复插入唯一限制的数据
- js字符串方法、数组方法整理
- 西安电子科技计算机专业,2020西安电子科技大学计算机专业课改考
- 在线mod计算机,计算机系中有关mod的常识(全).doc
- 【LeetCode】162-寻找峰值
- linux kali局域网远程桌面,kali使用rdesktop连接Windows远程桌面
- 朴素贝叶斯模型、推导、拉普拉斯平滑
- 佳蓝php智能应答系统|在线客服 v1.0,佳蓝PHP智能应答系统|在线客服 v1.0
- 9.2 向量范数的三大不等式
- Pandas学习笔记(二)—— Pandas索引
- Java可以运行javac不能运行
- 软件测试人还知道大名鼎鼎的compuware性能调优工具吗?该公司撤离中国!
- 手机GPS天线设计对GPS定位有影响