Android studio 实现简易微信程序代码

时间: 2023-12-19 08:07:46 浏览: 24
以下是一个简单的微信程序的代码示例,包含了上述提到的步骤。请注意,这只是一个示例,可能还需要根据具体需求进行修改和完善。 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> ```

相关推荐

最新推荐

recommend-type

android studio实现简单考试应用程序实例代码详解

主要介绍了android studio实现简单考试应用程序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

Android Studio实现简易计算器(表格布局TableLayout)

主要为大家详细介绍了Android Studio实现简易计算器,表格布局TableLayout,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

android实现微信朋友圈发布动态功能

主要为大家详细介绍了android实现微信朋友圈发布动态功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

解决Android Studio 代码自动提示突然失效的问题

主要介绍了解决Android Studio 代码自动提示突然失效的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

详解Android Studio实现用户登陆界面demo(xml实现)

主要介绍了详解Android Studio实现用户登陆界面demo,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。