聊天室
前言
这周是我学习Android的第四周,上周写了一个聊天室,这周来总结一下。聊天室分为两个部分,一个部分是客户端,就是我们平时看到的部分,还有一个非常重要的部分,那就是服务端。客户端主要是和用户进行交互,接收用户所传来的指令,在服务端才会将客户端传来的数据进行处理。
在刚开始的时候,我还不知道应该如何去写,没有思路,最后是先做的客户端,将客户端的UI写好之后,再来处理服务端和客户端与服务端的交接的部分。这是我写聊天室的大体思路,我来分享一下。
客户端
首先写的是客户端,因为刚开始,不知道如何去从服务端下手写,所以就先写了客户端的UI。首先,最好写的是布局,我写的聊天室也比较简单,没有多少功能,所以布局也是比较简单的首先来简单介绍一下,我有两个布局,一个是登录的布局,一个是进入之后的聊天布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffcc"
>
<LinearLayout
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="账户" />
<EditText
android:id="@+id/Account_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="密码" />
<EditText
android:id="@+id/password_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPassword"
android:maxLines="1"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:background="#FFCC00"
android:text="登录" />
<Button
android:id="@+id/create_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="80dp"
android:background="#FF6600"
android:text="注册" />
</RelativeLayout>
</LinearLayout>
以上是登陆布局,这个布局没什么说的,就是简单的布局和一些简单的控件所组成。
下面是登陆进去的聊天界面。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include layout="@layout/title" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/ip_edit"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="255.255.255.255" />
<Button
android:id="@+id/record_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="聊天记录" />
<Button
android:id="@+id/clearAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清除聊天记录"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="@+id/inputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxLines="1"/>
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"
/>
<Button
android:id="@+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="清屏" />
</LinearLayout>
</LinearLayout>
由于上面使用了RecyclerView,所以就会有每个RecyclerView的小布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"