用Android写的客户端,java写的服务端,这里简单的介绍一下我的客户端
效果展示
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200803082611418.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hhYXp6eg==,size_16,color_FFFFFF,t_70)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200803082128979.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hhYXp6eg==,size_2,color_FFFFFF,t_70)
代码展示分析
1.添加权限,依赖库
权限 uses-permission android:name=“android.permission.INTERNET”
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.uichat">
<uses-permission android:name="android.permission.INTERNET" />
<application
...
</application>
</manifest>
依赖库
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
...
implementation 'androidx.recyclerview:recyclerview:1.2.0-alpha05'
implementation 'org.litepal.guolindev:core:3.1.1'
}
1.主页面
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="@drawable/ddd"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.45" />
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="24sp"
app:layout_constraintEnd_toStartOf="@id/guideline"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/login"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintVertical_bias="0.4"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp" />
<EditText
android:id="@+id/usernameEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:hint="输入用户名"
app:layout_constraintBaseline_toBaselineOf="@id/username"
app:layout_constraintStart_toEndOf="@id/guideline"/>
<Button
android:id="@+id/login"
android:text="登录"
android:textSize="18sp"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintTop_toBottomOf="@id/username"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
package com.example.uichat;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button login;
EditText loginedittext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button)findViewById(R.id.login);
loginedittext = (EditText)findViewById(R.id.usernameEdit);
login.setOnClickListener(this);
}
@Override
public void onClick(View view) {
String name = loginedittext.getText().toString();
if("".equals(name))
Toast.makeText(MainActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
else {
Intent intent = new Intent(MainActivity.this, ChatUI.class);
intent.putExtra("username", name);
startActivity(intent);
}
}
}
2.聊天页面
activity_chatui.xml
<?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"
android:background="@drawable/eee"
>
<TextView
android:id="@+id/top_TextView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:text="聊天室"
android:gravity="center"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FF909090"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_ChatUi"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#FF909090"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/ip_editText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"/>
<Button
android:id="@+id/record"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="消息记录"/>
<Button
android:id="@+id/cls"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="清屏"/>
</LinearLayout>
</LinearLayout>
<Vi