2024/5/31
所学时间:2小时
代码行数:500
博客园数:1篇
所学知识:
package com.example.kanglaojian;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class Chat extends AppCompatActivity {
private RecyclerView recyclerView;
private MessageAdapter messageAdapter;
private List<String> messageList;
private EditText messageEditText;
private Button sendButton;
private String selectedItem;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
selectedItem = getIntent().getStringExtra("name");
TextView textView = findViewById(R.id.textView);
textView.setText(selectedItem);
recyclerView = findViewById(R.id.recycler_view_messages);
messageEditText = findViewById(R.id.edit_text_message);
sendButton = findViewById(R.id.button_send);
messageList = new ArrayList<>();
messageAdapter = new MessageAdapter(messageList);
recyclerView.setAdapter(messageAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = messageEditText.getText().toString();
sendMessage(message);
messageEditText.setText(""); // 清空输入框
}
});
// 模拟接收消息
simulateIncomingMessage();
}
// 发送消息的方法
private void sendMessage(String message) {
// 发送消息到服务器
// 这里可以使用网络请求或其他技术来发送消息到后端服务器
// 在这里假设直接添加到消息列表
messageList.add("我:" + message);
messageAdapter.notifyDataSetChanged(); // 更新RecyclerView
}
// 模拟接收消息的方法,实际应用中应该从后端服务器接收消息
private void simulateIncomingMessage() {
// 模拟从服务器接收到的消息
String message = "你好,我是"+selectedItem ;
receiveMessage(message);
}
// 接收消息的方法
private void receiveMessage(String message) {
// 更新UI以显示新消息
// 这里可以将收到的消息添加到消息列表中,并更新RecyclerView的数据源
// 在这里假设直接添加到消息列表
messageList.add("对方:" + message);
messageAdapter.notifyDataSetChanged(); // 更新RecyclerView
}
}
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class Chat extends AppCompatActivity {
private RecyclerView recyclerView;
private MessageAdapter messageAdapter;
private List<String> messageList;
private EditText messageEditText;
private Button sendButton;
private String selectedItem;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
selectedItem = getIntent().getStringExtra("name");
TextView textView = findViewById(R.id.textView);
textView.setText(selectedItem);
recyclerView = findViewById(R.id.recycler_view_messages);
messageEditText = findViewById(R.id.edit_text_message);
sendButton = findViewById(R.id.button_send);
messageList = new ArrayList<>();
messageAdapter = new MessageAdapter(messageList);
recyclerView.setAdapter(messageAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = messageEditText.getText().toString();
sendMessage(message);
messageEditText.setText(""); // 清空输入框
}
});
// 模拟接收消息
simulateIncomingMessage();
}
// 发送消息的方法
private void sendMessage(String message) {
// 发送消息到服务器
// 这里可以使用网络请求或其他技术来发送消息到后端服务器
// 在这里假设直接添加到消息列表
messageList.add("我:" + message);
messageAdapter.notifyDataSetChanged(); // 更新RecyclerView
}
// 模拟接收消息的方法,实际应用中应该从后端服务器接收消息
private void simulateIncomingMessage() {
// 模拟从服务器接收到的消息
String message = "你好,我是"+selectedItem ;
receiveMessage(message);
}
// 接收消息的方法
private void receiveMessage(String message) {
// 更新UI以显示新消息
// 这里可以将收到的消息添加到消息列表中,并更新RecyclerView的数据源
// 在这里假设直接添加到消息列表
messageList.add("对方:" + message);
messageAdapter.notifyDataSetChanged(); // 更新RecyclerView
}
}
<?xml version="1.0" encoding="utf-8"?>
<!-- activity_chat.xml -->
<RelativeLayout 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="@color/light_gray"
tools:context=".Chat">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_centerInParent="true" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="35dp"
android:layout_width="match_parent"
android:layout_height="650dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_messages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/layout_message_input"
android:padding="10dp"
android:clipToPadding="false"
android:scrollbars="vertical" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_message_input"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="10dp">
<EditText
android:id="@+id/edit_text_message"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:hint="Type a message..."
android:inputType="text" />
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="发送"
android:textColor="@color/green"/>
</LinearLayout>
</RelativeLayout>
<!-- activity_chat.xml -->
<RelativeLayout 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="@color/light_gray"
tools:context=".Chat">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_centerInParent="true" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="35dp"
android:layout_width="match_parent"
android:layout_height="650dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_messages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/layout_message_input"
android:padding="10dp"
android:clipToPadding="false"
android:scrollbars="vertical" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_message_input"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="10dp">
<EditText
android:id="@+id/edit_text_message"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:hint="Type a message..."
android:inputType="text" />
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="发送"
android:textColor="@color/green"/>
</LinearLayout>
</RelativeLayout>