整个界面最外层采用线性布局,在最大的linearLayout中先设置了一个TextVIEw用来显示聊天窗口的文本为机器人。
接着在TextVIEw下面放置了一个relativeLayout,在它里面先放置了一个ListVIEw,用于显示聊天消息列表。
然后放置了一个小的relativeLayout,里面放置了一个button和一个EditText,button在EditText右侧,文本为“发送”,作为发送按钮,EditText则是聊天输入框,在里面输入聊天内容。
这样整个聊天界面的布局文件就搭建好了。如图所示:
activity_main的代码如下:
<?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>
3、搭建聊天条目布局 chatting_left_item文件为机器人聊天头像和聊天框显示文件,用于显示机器人的聊天内容。
主要是在relativeLayout中放置了一个ImageVIEw和一个TextVIEw,ImageVIEw为机器人的头像图片robot_head,TextVIEw 中的 style 设置为 style 文件夹中写好的格式文件 chat_content_style,background选择drawable中的chat_left_selector【鼠标选中消息,背景显示为深绿色,默认显示为绿色】。效果如图:
chatting_left_item代码如下:
<?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_item文件为用户聊天头像和聊天框显示文件,用于显示用户的聊天内容。
和机器人的聊天条目相同。主要是在relativeLayout中放置了一个ImageVIEw和一个TextVIEw,ImageVIEw 为用户的头像图片 myhead ,TextVIEw中的 style 为 style 中 chat_content_style,
background选择drawable中的chat_right_selector【鼠标选中消息,背景显示为灰色,默认显示为白色】。效果如图:
chatting_right_item代码如下:
<?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>
4、封装聊天信息实体类 在该类中创建机器人与用户聊天信息的属性。重写了get和set方法,消息的状态设为state,发送消息值设为1,接受消息值设为2。
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; }}
5、编写聊天列表适配器 由于聊天界面用了ListVIEw控件显示聊天信息,因此需要创建一个数据适配器ChatAdapter对ListVIEw控件进行数据适配。所以要创建一个ChatAdapter类。
getVIEw方法内用if和else语句判断当前的消息是发送的消息还是接收到的消息,不同消息加载不同的vIEw。如果是接收消息,说明就是机器人发的消息,那就加载左边布局;如果是发送消息,说明就是用户发的消息,则加载右边布局。
@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; }
6、实现智能机器人通信 在RobotActivity中,创建了5个方法:
(1)initVIEw( ) 用于获取界面控件并初始化界面数据;
(2)showData( ) 用于显示欢迎信息到界面上;
(3)sendData ( ) 用于用户发送信息;
(4)getDataFromServer( ) 从服务器获取机器人的回复信息;
(5)updateVIEw( ) 更新界面信息;
最后重写onKeyDown( )方法,在该方法中实现点击后退键退出智能聊天程序的功能。
四、项目效果
1、进来会有随机的欢迎消息,可以在回复设置里面自定义。
2、没有装中文输入法,所以就发个数字先,它会自动回复。
3、发英文字母a,b,c,它会自动往下接,发e它会回句“额”。
五、项目总结
在本项目的实现过程中,熟悉了网络请求、JsON解析、Handler处理等知识点,这些知识点会在后来AndroID项目中经常使用,因此希望读者可以按照步骤完成此项目,通过熟练掌握本项目中的这些知识点,方便以后开发其他项目。
六、源码下载需要源码学习的同学可以关注我的微信公众号,回复:聊天机器人,即可获取源码,还有很多AndroID项目等你来学习。
总结你遇见了什么人,什么时候找到一份工作,在哪里做了什么事,是努力还是堕落,事到如今,你回头想想,这些其实都是你对自己的安排。所有的昨日,才有了你所有的今日。
以上是内存溢出为你收集整理的Android实现智能聊天机器人全部内容,希望文章能够帮你解决Android实现智能聊天机器人所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)