Android聊天室代码分析

本文介绍了使用Android客户端和Java服务端构建的聊天室应用。详细分析了客户端的代码实现,包括添加INTERNET权限,依赖库的引入,以及主页面、聊天页面和消息记录页面的布局与逻辑。涉及到的关键组件有MainActivity、ChatUI、ChatAdapt,同时利用LitePal进行数据库操作。
摘要由CSDN通过智能技术生成
用Android写的客户端,java写的服务端,这里简单的介绍一下我的客户端
服务端链接
源码链接
效果展示

在这里插入图片描述

在这里插入图片描述

代码展示分析
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'	//recyclerview
    implementation 'org.litepal.guolindev:core:3.1.1'		//LitePal
}
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 {
   
        //只要输入了名字就进入ChatUI界面
            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
  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值