android中实现简单的聊天功能
这个例子只是简单的实现了单机版的聊天功能,自己跟自己聊,啦啦~~
主要还是展示RecyclerView控件的使用吧~
参考我之前写的文章:
1、先添加一个关于聊天内容的Chat.java类:
package com.example.chenrui.common;
public class Chat {
private String targetUser;
private String content;
public Chat(String targetUser, String content) {
this.targetUser = targetUser;
this.content = content;
}
public String getTargetUser() {
return targetUser;
}
public void setTargetUser(String targetUser) {
this.targetUser = targetUser;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
Chat.java这个类有两个属性:聊天对象和聊天内容
2、添加一个RecyclerView控件子项chat_item.xml,这个是实现了聊天内容项的展示:
<?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="wrap_content"
android:orientation="vertical"> <TextView
android:id="@+id/chatText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/chat"
android:gravity="center_vertical"
android:text="TextView" />
</LinearLayout>
3、添加RecyclerView控件的适配器ChatAdapter.java类:
package com.example.chenrui.app1; import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView; import com.example.chenrui.common.Chat; import java.util.List; public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> { private List<Chat> chatList; public ChatAdapter(List<Chat> chatList) {
this.chatList = chatList;
} @NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.chat_item,viewGroup,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
} @Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
Chat chat = chatList.get(i);
viewHolder.chatText.setText(chat.getContent());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(viewHolder.chatText.getLayoutParams());
if(chat.getTargetUser().equals("张三")) {
params.gravity = Gravity.LEFT;
} else {
params.gravity = Gravity.RIGHT;
}
viewHolder.chatText.setLayoutParams(params);
} @Override
public int getItemCount() {
return chatList.size();
} static class ViewHolder extends RecyclerView.ViewHolder { TextView chatText; public ViewHolder(@NonNull View itemView) {
super(itemView); chatText = itemView.findViewById(R.id.chatText);
}
}
}
上面的代码,设置的是,如果聊天对象是张三,就把聊天内容显示在列表的左侧,否则就显示在列表的右侧。这个是模拟自己和别人对话的展示效果
4、最后就是主Activity了
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView
android:id="@+id/chatList"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/linearLayout3"
app:layout_constraintTop_toTopOf="parent" /> <LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/darker_gray"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/linearLayout4"> <Spinner
android:id="@+id/targetUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" /> <EditText
android:id="@+id/chatContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:inputType="textPersonName" />
</LinearLayout> <LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"> <Button
android:id="@+id/chatSubmit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="发言" />
</LinearLayout> </android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.chenrui.app1; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner; import com.example.chenrui.common.Chat; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); final RecyclerView chatList = findViewById(R.id.chatList); final List<Chat> chatData = new ArrayList<Chat>();
final ChatAdapter chatAdapter = new ChatAdapter(chatData);
chatList.setAdapter(chatAdapter);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
chatList.setLayoutManager(layoutManager); final Spinner targetUser = findViewById(R.id.targetUser);
String[] data = {"张三","李四"};
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,R.layout.support_simple_spinner_dropdown_item,data);
targetUser.setAdapter(adapter); final EditText chatContent = findViewById(R.id.chatContent); Button chatSubmit = findViewById(R.id.chatSubmit);
chatSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!chatContent.getText().toString().equals("")) {
String user = targetUser.getSelectedItem().toString();
chatData.add(new Chat(user, chatContent.getText().toString())); chatAdapter.notifyItemInserted(chatData.size() - 1);
chatList.scrollToPosition(chatData.size() - 1);
chatContent.setText("");
}
}
});
}
}
显示效果:

android中实现简单的聊天功能的更多相关文章
- Spring 学习——基于Spring WebSocket 和STOMP实现简单的聊天功能
本篇主要讲解如何使用Spring websocket 和STOMP搭建一个简单的聊天功能项目,里面使用到的技术,如websocket和STOMP等会简单介绍,不会太深,如果对相关介绍不是很了解的,请自 ...
- MVP架构在xamarin android中的简单使用
好几个月没写文章了,使用xamarin android也快接近两年,还有一个月职业生涯就到两个年了,从刚出来啥也不会了,到现在回头看这个项目,真jb操蛋(真辛苦了实施的人了,无数次吐槽怎么这么丑),怪 ...
- 采用tcp协议和UDP协议实现简单的聊天功能
Date: 2019-06-19 Author: Sun 一. Python3输出带颜色字体 实现过程: 终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关. 转义 ...
- EventBus在Android中的简单使用
EventBus是一个方便与Android中各组件通信的开源框架,开源地址;https://github.com/greenrobot/EventBus.EventBus功能非常强大 ,今天在做一个功 ...
- Android中禁止SlidingPaneLayout的侧滑功能
Android中使用android.support.v4.widget.SlidingPaneLayout实现侧滑功能的时候,可能出现滑动屏幕时与SlidingPaneLayout的侧滑发生冲突,查看 ...
- Gradle在Android中的简单使用
Gradle在Android中简单的使用 还望支持个人博客站:http://www.enjoytoday.cn Android Studio 使用gradle进行工程构建,为了更好的了解整个andro ...
- Java中使用UDP实现简单的聊天功能
通过DatagramSocket类来实现.此类表示用来发送和接收数据报包的套接字. 发送端代码如下: import java.io.IOException; import java.net.*; im ...
- Android中使用ShareSDK集成分享功能
引言 现在APP开发集成分享功能已经是非常普遍的需求了.其他集成分享技术我没有使用过,今天我就来介绍下使用ShareSDK来进行分享功能开发的一些基本步骤和注意点,帮助朋友们避免一些坑.好了 ...
- android 中Log - 简单使用
例如,我们可以使用'Log.d'进行Debug,在java代码中输入Log.d(String tag, String message),tag为自己命名的tag,message为待输出的信息.然后打开 ...
随机推荐
- 使用JavaScript的数组实现数据结构中的队列与堆栈
今天在项目中要使用JavaScript实现数据结构中的队列和堆栈,这里做一下总结. 一.队列和堆栈的简单介绍 1.1.队列的基本概念 队列:是一种支持先进先出(FIFO)的集合,即先被插入的数据,先被 ...
- CLR如何加载程序集以及程序集版本策略
在项目的配置文件Web.config中,会看到<runtime>节点,以及包含在其中的<assemblyBinding>节点,这显然与程序集有关,这些节点到底何时被用到呢? 在 ...
- Chrome 如何知道网站启用了SPDY 协议?
地址栏输入chrome://net-internals/#spdy 在host后查看协议,google和dropbox用https协议的开启了 3. 也可以通过安装插件来查看(SPDY Indicat ...
- C#编程(十四)----------构造函数
原文链接:http://blog.csdn.net/shanyongxu/article/details/46501683 构造函数 所谓的构造函数就是和类名重名的且没有返回值的方法. class P ...
- Apache Tomcat 9 Installation on Linux (RHEL and clones)
Apache Tomcat 9 is not available from the standard RHEL distributions, so this article provides info ...
- 警告 7 隐藏了继承的成员。如果是有意隐藏,请使用关键字 new
public new bool Print(string 承包方编码, MapPrint.My2Progress pMy2Progress, bool Label2ZJ)
- python测试开发django-43.session机制(登录/注销)
前言 当我们登录访问一个网站时,服务器需要识别到你已经登录了,才有相应的权限访问登录之后的页面.用户退出登录后,将无权限访问再访问登录后的页面. 从登录到退出的一整个流程,可以看成是与服务器的一次会话 ...
- SharePoint Online 设置网站集
前言 本文介绍如何在Office 365中设置SharePoint Online网站集,当我们创建好SharePoint Online站点,开始使用之前,一定会有一些基本的设置,本文就为大家介绍这些基 ...
- putty adb
putty.exe -adb -P 5037 transport-usb 网络调试也是可以的 先connect 再执行上面的命令 http://files.cnblogs.com/files/ahuo ...
- 用开源项目cropper实现对图片中任意部分进行裁剪
红色区域为截图控件的区域. 开源项目地址:https://github.com/edmodo/cropper croper这个开源项目可以对一个图片进行任意区域的街区,并且可以设置图片的旋转角 ...