Android studio 人机聊天界面实现代码
时间: 2023-11-25 08:05:44 浏览: 209
首先,你需要先创建一个布局文件,用来显示聊天记录和输入框。可以参考以下代码:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/chat_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="0dp"
android:listSelector="@android:color/transparent"
android:stackFromBottom="true" />
<LinearLayout
android:id="@+id/input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
android:orientation="horizontal"
android:padding="@dimen/padding_medium">
<EditText
android:id="@+id/input_edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:hint="@string/enter_message"
android:imeOptions="actionSend"
android:inputType="text"
android:maxLines="6"
android:textColor="@android:color/black"
android:textColorHint="@android:color/darker_gray"
android:textSize="@dimen/font_medium" />
<Button
android:id="@+id/send_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_send"
android:text="@string/send"
android:textColor="@android:color/white"
android:textSize="@dimen/font_medium" />
</LinearLayout>
</RelativeLayout>
```
然后,在Activity中,你需要实现一个适配器来展示聊天记录,并且监听发送按钮的点击事件,将输入框中的文本发送给对方。可以参考以下代码:
```
public class ChatActivity extends AppCompatActivity implements View.OnClickListener {
private ListView mChatListView;
private EditText mInputEditText;
private Button mSendButton;
private List<Message> mMessageList;
private ChatAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
mChatListView = findViewById(R.id.chat_listview);
mInputEditText = findViewById(R.id.input_edittext);
mSendButton = findViewById(R.id.send_button);
mSendButton.setOnClickListener(this);
mMessageList = new ArrayList<>();
mAdapter = new ChatAdapter(this, mMessageList);
mChatListView.setAdapter(mAdapter);
}
@Override
public void onClick(View v) {
if (v == mSendButton) {
String messageText = mInputEditText.getText().toString().trim();
if (!TextUtils.isEmpty(messageText)) {
Message message = new Message(messageText, true);
mMessageList.add(message);
mAdapter.notifyDataSetChanged();
mInputEditText.setText("");
}
}
}
}
```
其中,Message是一个简单的数据类,用来保存聊天记录的文本和发送者的信息。ChatAdapter则是一个继承自BaseAdapter的适配器,用来将数据绑定到ListView上。具体实现可以参考以下代码:
```
public class ChatAdapter extends BaseAdapter {
private Context mContext;
private List<Message> mMessageList;
public ChatAdapter(Context context, List<Message> messageList) {
mContext = context;
mMessageList = messageList;
}
@Override
public int getCount() {
return mMessageList.size();
}
@Override
public Message getItem(int position) {
return mMessageList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return getItem(position).isSentByMe() ? 0 : 1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
int layoutId = getItemViewType(position) == 0 ?
R.layout.list_item_chat_sent : R.layout.list_item_chat_received;
convertView = LayoutInflater.from(mContext).inflate(layoutId, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.bindView(getItem(position));
return convertView;
}
private static class ViewHolder {
private TextView mMessageTextView;
ViewHolder(View itemView) {
mMessageTextView = itemView.findViewById(R.id.message_textview);
}
void bindView(Message message) {
mMessageTextView.setText(message.getText());
}
}
}
```
最后,你还需要定义两个布局文件,分别用来显示发送和接收到的消息。可以参考以下代码:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/padding_small">
<TextView
android:id="@+id/message_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/chat_bubble_sent"
android:padding="@dimen/padding_medium"
android:textColor="@android:color/white"
android:textSize="@dimen/font_medium" />
</RelativeLayout>
```
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/padding_small">
<TextView
android:id="@+id/message_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/chat_bubble_received"
android:padding="@dimen/padding_medium"
android:textColor="@android:color/black"
android:textSize="@dimen/font_medium" />
</RelativeLayout>
```
注意,这里使用了不同的背景图片来区分发送和接收到的消息。
至此,你就完成了一个简单的人机聊天界面的实现。
阅读全文