Android studio 实现简易微信程序代码
时间: 2023-12-19 20:07:46 浏览: 133
以下是一个简单的微信程序的代码示例,包含了上述提到的步骤。请注意,这只是一个示例,可能还需要根据具体需求进行修改和完善。
Message.java
```java
public class Message {
private String content;
private String sender;
private String receiver;
private Date time;
public Message(String content, String sender, String receiver, Date time) {
this.content = content;
this.sender = sender;
this.receiver = receiver;
this.time = time;
}
public String getContent() {
return content;
}
public String getSender() {
return sender;
}
public String getReceiver() {
return receiver;
}
public Date getTime() {
return time;
}
}
```
MessageAdapter.java
```java
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
private List<Message> messageList;
public MessageAdapter(List<Message> messageList) {
this.messageList = messageList;
}
@NonNull
@Override
public MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message, parent, false);
return new MessageViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MessageViewHolder holder, int position) {
Message message = messageList.get(position);
holder.contentTextView.setText(message.getContent());
holder.senderTextView.setText(message.getSender());
holder.timeTextView.setText(message.getTime().toString());
}
@Override
public int getItemCount() {
return messageList.size();
}
public static class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView contentTextView;
public TextView senderTextView;
public TextView timeTextView;
public MessageViewHolder(View itemView) {
super(itemView);
contentTextView = itemView.findViewById(R.id.contentTextView);
senderTextView = itemView.findViewById(R.id.senderTextView);
timeTextView = itemView.findViewById(R.id.timeTextView);
}
}
}
```
ChatManager.java
```java
public class ChatManager {
private List<Message> messageList;
private MessageAdapter messageAdapter;
public ChatManager() {
messageList = new ArrayList<>();
}
public void setAdapter(MessageAdapter adapter) {
messageAdapter = adapter;
}
public void sendMessage(String content, String sender, String receiver) {
Message message = new Message(content, sender, receiver, new Date());
messageList.add(message);
messageAdapter.notifyDataSetChanged();
// TODO: 发送消息到服务器
}
public List<Message> getMessageList() {
return messageList;
}
public void setMessageList(List<Message> messageList) {
this.messageList = messageList;
messageAdapter.notifyDataSetChanged();
}
}
```
MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
private ChatManager chatManager;
private EditText inputEditText;
private Button sendButton;
private RecyclerView messageRecyclerView;
private MessageAdapter messageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputEditText = findViewById(R.id.inputEditText);
sendButton = findViewById(R.id.sendButton);
messageRecyclerView = findViewById(R.id.messageRecyclerView);
// 初始化 ChatManager,并且设置 RecyclerView 的 Adapter
chatManager = new ChatManager();
messageAdapter = new MessageAdapter(chatManager.getMessageList());
messageRecyclerView.setLayoutManager(new LinearLayoutManager(this));
messageRecyclerView.setAdapter(messageAdapter);
chatManager.setAdapter(messageAdapter);
// 发送消息的按钮点击事件
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content = inputEditText.getText().toString();
chatManager.sendMessage(content, "me", "you");
inputEditText.setText("");
}
});
}
}
```
布局文件 activity_main.xml
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/messageRecyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/inputEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送"/>
</LinearLayout>
</LinearLayout>
```
布局文件 item_message.xml
```xml
<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/contentTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/senderTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="12sp"/>
<TextView
android:id="@+id/timeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"/>
</LinearLayout>
</LinearLayout>
```
阅读全文