Android intelligent chat robot

Next, a relativelayout is placed under the textview, and a listview is placed in it to display the chat message list.

Then a small relativelayout is placed, in which a button and an EditText are placed. The button is on the right side of EditText, and the text is "send", which is used as the send button. EditText is the chat input box, in which chat content is entered.

In this way, the layout file of the whole chat interface is built. As shown in the figure:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center"
        android:background="#0cc4e5"
        android:text="机器人"
        android:textColor="@android:color/white"
        android:textSize="20sp"/>
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@+id/rl_bottom"
            android:cacheColorHint="@android:color/black"
            android:divider="@null"
            android:listSelector="@null"
            android:transcriptMode="alwaysScroll"/>
        <RelativeLayout
            android:id="@+id/rl_bottom"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@drawable/bottom_bg">
            <Button
                android:id="@+id/btn_send"
                android:layout_width="60dp"
                android:layout_height="40dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="10dp"
                android:background="@drawable/btn_send_selector"
                android:text="发送"
                android:textColor="@android:color/black"
                android:textSize="14sp"/>
            <EditText
                android:id="@+id/et_send_msg"
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_toLeftOf="@+id/btn_send"
                android:background="@drawable/send_msg_bg"
                android:singleLine="true"
                android:textColor="@android:color/black"
                android:textSize="18sp"/>
        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>

Mainly, an ImageView and a textview are placed in the relativelayout. The ImageView is the head picture of the robot_ Head, set the style in textview to the format file chat written in the style folder_ content_ Style, background select chat in drawable_ left_ Select the message with the mouse, the background is dark green, and the default is green. The effect is shown in the figure:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="6dp">
    <ImageView
        android:id="@+id/iv_head"
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:background="@drawable/robot_head"
        android:focusable="false"/>
    <TextView
        android:id="@+id/tv_chat_content"
        style="@style/chat_content_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/iv_head"
        android:background="@drawable/chat_left_selector" />
</RelativeLayout>

chatting_ right_ The item file displays the user's chat avatar and chat box, which is used to display the user's chat content.

Same as the robot's chat entry. Mainly, an ImageView and a textview are placed in the relativelayout. The ImageView is the user's Avatar myhead, and the style in the textview is the style chat_ content_ Style, background select chat in drawable_ right_ Select the message with the mouse, the background is gray, and the default is white. The effect is shown in the figure:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="6dp">
    <ImageView
        android:id="@+id/iv_head"
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:background="@drawable/myhead"
        android:focusable="false"/>
    <TextView
        android:id="@+id/tv_chat_content"
        style="@style/chat_content_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_toLeftOf="@+id/iv_head"
        android:background="@drawable/chat_right_selector"/>

</RelativeLayout>

public class ChatBean {
    public static final int SEND=1;//发送消息
    public static final int RECEIVE=2;//接收消息
    private int state;//消息的状态(是接收还是发送)
    private String message;//消息的内容

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

Because the chat interface uses the listview control to display chat information, you need to create a data adapter chatadapter to adapt the data of the listview control. So create a chatadapter class.

In the getview method, if and else statements are used to determine whether the current message is a sent message or a received message. Different messages load different views. If the message is received, it means that it is the message sent by the robot, then load the layout on the left; If the message is sent, it means that it is sent by the user, and the layout on the right is loaded.

@Override
    public View getView(int position, View contentView, ViewGroup viewGroup) {
        Holder holder = new Holder();
        //判断当前信息是发送信息还是接收信息,根据不同信息加载不同布局文件
        if (chatBeanList.get(position).getState() == ChatBean.RECEIVE) {
        	//加载机器人对应的布局文件
            contentView = layoutInflater.inflate(R.layout.chatting_left_item, null);
        } else {
        	//加载用户对应的布局文件
           contentView = layoutInflater.inflate(R.layout.chatting_right_item,null);
        }
        //获取聊天内容控件,并设置控件的文本信息
        holder.tv_chat_content = (TextView) contentView.findViewById(R.id. tv_chat_content);
        holder.tv_chat_content.setText(chatBeanList.get(position).getMessage());
        return contentView;
    }

(1) Initview() is used to obtain interface controls and initialize interface data;

1. There will be random welcome messages, which can be customized in the reply settings.

During the implementation of this project, I am familiar with network request, JSON parsing, handler processing and other knowledge points, which will be often used in later Android projects. Therefore, I hope readers can complete this project in accordance with the steps, and master these knowledge points in this project to facilitate the development of other projects in the future.

Students who need source learning can pay attention to my WeChat official account. Reply: chat robot, you can get source code, and there are many Android projects waiting for you to learn.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>