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

时间: 2023-12-19 18:07:36 浏览: 43
以下是实现一个简易微信程序的代码示例: MainActivity.java ``` public class MainActivity extends AppCompatActivity { private RecyclerView chatRecyclerView; private EditText messageEditText; private Button sendButton; private List<Message> messageList; private MessageAdapter messageAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); chatRecyclerView = findViewById(R.id.chatRecyclerView); messageEditText = findViewById(R.id.messageEditText); sendButton = findViewById(R.id.sendButton); messageList = new ArrayList<>(); messageAdapter = new MessageAdapter(messageList); LinearLayoutManager layoutManager = new LinearLayoutManager(this); chatRecyclerView.setLayoutManager(layoutManager); chatRecyclerView.setAdapter(messageAdapter); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String content = messageEditText.getText().toString(); if (!content.equals("")) { Message message = new Message(Message.TYPE_SENT, content); messageList.add(message); messageAdapter.notifyItemInserted(messageList.size() - 1); chatRecyclerView.scrollToPosition(messageList.size() - 1); messageEditText.setText(""); } } }); } } ``` Message.java ``` public class Message { public static final int TYPE_RECEIVED = 0; public static final int TYPE_SENT = 1; private int type; private String content; public Message(int type, String content) { this.type = type; this.content = content; } public int getType() { return type; } public String getContent() { return content; } } ``` MessageAdapter.java ``` public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHolder> { private List<Message> messageList; static class ViewHolder extends RecyclerView.ViewHolder { TextView leftMessage; TextView rightMessage; public ViewHolder(View view) { super(view); leftMessage = view.findViewById(R.id.leftMessage); rightMessage = view.findViewById(R.id.rightMessage); } } public MessageAdapter(List<Message> messageList) { this.messageList = messageList; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { Message message = messageList.get(position); if (message.getType() == Message.TYPE_RECEIVED) { holder.leftMessage.setVisibility(View.VISIBLE); holder.rightMessage.setVisibility(View.GONE); holder.leftMessage.setText(message.getContent()); } else if (message.getType() == Message.TYPE_SENT) { holder.rightMessage.setVisibility(View.VISIBLE); holder.leftMessage.setVisibility(View.GONE); holder.rightMessage.setText(message.getContent()); } } @Override public int getItemCount() { return messageList.size(); } } ``` activity_main.xml ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/chatRecyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/messageLayout" android:layout_alignParentTop="true" /> <LinearLayout android:id="@+id/messageLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#FFFFFF" android:orientation="horizontal" android:padding="10dp"> <EditText android:id="@+id/messageEditText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@android:color/transparent" android:hint="Type a message..." /> <Button android:id="@+id/sendButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" /> </LinearLayout> </RelativeLayout> ``` message_item.xml ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp"> <TextView android:id="@+id/leftMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/left_message_background" android:padding="10dp" android:textColor="#FFFFFF" android:textSize="16sp" /> <TextView android:id="@+id/rightMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/right_message_background" android:padding="10dp" android:textColor="#FFFFFF" android:textSize="16sp" /> </LinearLayout> ``` 其中,left_message_background.xml和right_message_background.xml是自定义的背景文件,用于区分发送方和接收方。

相关推荐

最新推荐

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

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

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