Android studio 实现简易微信程序代码
时间: 2023-12-19 12:07:36 浏览: 91
AndroidStudio简单微信界面
4星 · 用户满意度95%
以下是实现一个简易微信程序的代码示例:
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是自定义的背景文件,用于区分发送方和接收方。
阅读全文