使用Android编写的聊天界面代码
时间: 2023-07-14 14:13:47 浏览: 126
Android利用RecyclerView编写聊天界面
以下是一个简单的 Android 聊天界面的代码示例,其中使用了 RecyclerView 和自定义的适配器:
1. 在布局文件中添加 RecyclerView、EditText 和 Button:
```
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/chat_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:clipToPadding="false"
android:paddingTop="8dp"
android:paddingBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/message_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/message_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<EditText
android:id="@+id/message_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Type a message" />
<Button
android:id="@+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
```
2. 创建一个 ChatMessage 类,用于表示聊天消息:
```
public class ChatMessage {
private String text;
private boolean isMine;
public ChatMessage(String text, boolean isMine) {
this.text = text;
this.isMine = isMine;
}
public String getText() {
return text;
}
public boolean isMine() {
return isMine;
}
}
```
3. 创建一个自定义的适配器 ChatAdapter,用于将聊天消息展示在 RecyclerView 中:
```
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
private List<ChatMessage> messages;
public ChatAdapter(List<ChatMessage> messages) {
this.messages = messages;
}
@Override
public int getItemCount() {
return messages.size();
}
@Override
public int getItemViewType(int position) {
return messages.get(position).isMine() ? 1 : 0;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(viewType == 0 ? R.layout.item_chat_received : R.layout.item_chat_sent, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
ChatMessage message = messages.get(position);
holder.messageTextView.setText(message.getText());
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView messageTextView;
public ViewHolder(View itemView) {
super(itemView);
messageTextView = itemView.findViewById(R.id.message_text_view);
}
}
}
```
4. 在 Activity 中初始化 RecyclerView、EditText 和 Button,并设置 Button 的点击事件:
```
public class ChatActivity extends AppCompatActivity {
private RecyclerView chatRecyclerView;
private EditText messageEditText;
private Button sendButton;
private List<ChatMessage> messages = new ArrayList<>();
private ChatAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
chatRecyclerView = findViewById(R.id.chat_recycler_view);
messageEditText = findViewById(R.id.message_edit_text);
sendButton = findViewById(R.id.send_button);
adapter = new ChatAdapter(messages);
chatRecyclerView.setAdapter(adapter);
chatRecyclerView.setLayoutManager(new LinearLayoutManager(this));
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String messageText = messageEditText.getText().toString().trim();
if (!TextUtils.isEmpty(messageText)) {
messages.add(new ChatMessage(messageText, true));
adapter.notifyItemInserted(messages.size() - 1);
messageEditText.setText("");
}
}
});
}
}
```
5. 创建两个布局文件 item_chat_sent.xml 和 item_chat_received.xml,用于分别展示发送和接收到的聊天消息:
item_chat_sent.xml:
```
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/message_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_gravity="end"
android:background="@drawable/bg_chat_sent"
android:padding="8dp"
android:textColor="@android:color/white" />
```
item_chat_received.xml:
```
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/message_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginStart="16dp"
android:layout_gravity="start"
android:background="@drawable/bg_chat_received"
android:padding="8dp"
android:textColor="@android:color/black" />
```
6. 创建两个 drawable 资源文件 bg_chat_sent.xml 和 bg_chat_received.xml,用于设置发送和接收到的聊天消息的背景颜色:
bg_chat_sent.xml:
```
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#3F51B5" />
<corners android:radius="16dp" />
</shape>
```
bg_chat_received.xml:
```
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#EEEEEE" />
<corners android:radius="16dp" />
</shape>
```
以上是一个简单的 Android 聊天界面的代码示例,你可以根据自己的需求进行修改和扩展。
阅读全文