kotlin實(shí)現(xiàn)語音聊天機(jī)器人案例詳解
此篇文章緊做關(guān)于語音機(jī)器人聊天開發(fā),后續(xù)功能實(shí)現(xiàn)請(qǐng)關(guān)注后續(xù)文章?。?!
此篇文章完成后效果展示:
一.機(jī)器人聊天—對(duì)話adapter的實(shí)現(xiàn)
1.準(zhǔn)備兩張左右兩邊動(dòng)畫背景圖片,做left,和right兩邊布局,為Recyclerview的實(shí)現(xiàn)做準(zhǔn)備。
left_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="10dp" android:src="@drawable/assistant"/> <TextView android:id="@+id/tv_left_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@drawable/chat_bg_cloud" android:gravity="center_vertical" android:padding="20dp" android:textColor="@android:color/white"/> </LinearLayout>
right_item.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right|center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/tv_right_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:background="@drawable/chat_bg_user" android:gravity="center_vertical" android:padding="20sp" android:textColor="@android:color/white"/> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_marginRight="10dp" android:src="@drawable/user"/> </LinearLayout>
2.在entity包下ChatListData對(duì)話列表的實(shí)體類,代碼如下:
package com.zrc.smartbutler.entity /** *項(xiàng)目名: SmartButler *包名: com.zrc.smartbutler.entity *文件名: ChatListData *描述: 對(duì)話列表的實(shí)體類 */ class ChatListData(var type:Int,var context:String){ companion object{ const val TyPE_RECEIVED = 0 const val Type_SENT = 1 } }
3.在adapter包下創(chuàng)建ChatListAdapter對(duì)話Adapter,代碼如下:
package com.zrc.smartbutler.adapter import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.recyclerview.widget.RecyclerView import com.zrc.smartbutler.R import com.zrc.smartbutler.entity.ChatListData /** *項(xiàng)目名: SmartButler *包名: com.zrc.smartbutler.adapter *文件名: ChatListAdapter *描述: 對(duì)話Adapter */ class ChatListAdapter (val mList:List<ChatListData>): RecyclerView.Adapter<RecyclerView.ViewHolder>() { inner class LeftViewHolder(view:View):RecyclerView.ViewHolder(view){ val leftMsg:TextView = view.findViewById(R.id.tv_left_text) } inner class RightViewHolder(view:View):RecyclerView.ViewHolder(view){ val rightMsg:TextView = view.findViewById(R.id.tv_right_text) } override fun getItemViewType(position: Int): Int { val msg = mList[position] return msg.type } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = if(viewType==ChatListData.TyPE_RECEIVED){ val view = LayoutInflater.from(parent.context).inflate(R.layout.left_item,parent,false) LeftViewHolder(view) }else{ val view = LayoutInflater.from(parent.context).inflate(R.layout.right_item,parent,false) RightViewHolder(view) } override fun getItemCount() = mList.size override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { val msg = mList[position] when(holder){ is LeftViewHolder -> holder.leftMsg.text = msg.context is RightViewHolder -> holder.rightMsg.text = msg.context else -> throw IllegalArgumentException() } } }
這段代碼,仿寫自郭霖大神的第一行代碼(第三版),詳情解析,請(qǐng)查看第三行代碼!
至此,對(duì)話adapter完成!??!
二.機(jī)器人聊天—機(jī)器人實(shí)時(shí)對(duì)話實(shí)現(xiàn)
1.導(dǎo)入RecyclerView依賴
implementation 'androidx.recyclerview:recyclerview:1.1.0
2.前往fragment_butler.xml,編寫界面交互代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/wechat_bg" android:orientation="vertical"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/inputText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="輸入" android:maxLines="2"/> <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@drawable/button_bg" android:text="發(fā)送"/> </LinearLayout> </LinearLayout>
3.編寫Kotlin交互代碼:
package com.zrc.smartbutler.fragment import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager import com.kymjs.rxvolley.RxVolley import com.kymjs.rxvolley.client.HttpCallback import com.zrc.smartbutler.R import com.zrc.smartbutler.adapter.ChatListAdapter import com.zrc.smartbutler.entity.ChatListData import com.zrc.smartbutler.utils.StaticClass import kotlinx.android.synthetic.main.fragment_butler.view.* import org.json.JSONException import org.json.JSONObject /** *項(xiàng)目名: SmartButler *包名: com.zrc.smartbutler.fragment *文件名: ButlerFragment *描述: 服務(wù)管家 */ class ButlerFragment:Fragment() { private val msgList = ArrayList<ChatListData>() private var adapter:ChatListAdapter?= null override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view:View = inflater.inflate(R.layout.fragment_butler,null) findView() val layoutManager = LinearLayoutManager(this.context) view.recyclerView.layoutManager = layoutManager adapter = ChatListAdapter(msgList) view.recyclerView.adapter = adapter view.send.setOnClickListener { /** * 邏輯 * 1.獲取輸入框的內(nèi)容 * 2.判斷是否為空 * 3.判斷長(zhǎng)度不能大于30 * 4.添加你輸入的內(nèi)容到right item * 5.清空當(dāng)前的輸入框 * 6.發(fā)送給機(jī)器人請(qǐng)求返回內(nèi)容 * 7.拿到機(jī)器人的返回值之后添加在left item */ //1.獲取輸入框的內(nèi)容 val content = view.inputText.text.toString() //2.判斷是否為空 if(content.isNotEmpty()){ //3.判斷長(zhǎng)度不能大于30 if(content.length>30){ Toast.makeText(activity,"輸入長(zhǎng)度超出限制",Toast.LENGTH_SHORT).show() }else{ //4.添加你輸入的內(nèi)容到right item val msg = ChatListData(ChatListData.Type_SENT,content) msgList.add(msg) adapter?.notifyItemInserted(msgList.size-1)//當(dāng)有新消息時(shí),刷新RecycleView中的顯示 view.recyclerView.scrollToPosition(msgList.size-1)//講RecycleView定位到最后一行 //5.清空當(dāng)前的輸入框 view.inputText.setText("") //6.發(fā)送給機(jī)器人請(qǐng)求返回內(nèi)容 //6.發(fā)送給機(jī)器人請(qǐng)求返回內(nèi)容 val url = ("http://op.juhe.cn/robot/index?info=" + content + "&key=" + StaticClass().CHAT_LIST_KEY) RxVolley.get(url,object :HttpCallback(){ override fun onSuccess(t: String?) { //Toast.makeText(activity,t,Toast.LENGTH_SHORT).show() parsingJson(t!!,view) } }) } }else{ Toast.makeText(activity,"輸入框不能為空",Toast.LENGTH_SHORT).show() } } return view } private fun findView() { val msg1 = ChatListData(ChatListData.TyPE_RECEIVED,"你好,我是大雨子?。?!") msgList.add(msg1) } //解析Json private fun parsingJson(t: String,view: View) { try { val jsonObhect = JSONObject(t) val jsonresult = jsonObhect.getJSONObject("result") //拿到返回值 val text = jsonresult.getString("text") //7.拿到機(jī)器人的返回值之后添加在left item val msg = ChatListData(ChatListData.TyPE_RECEIVED,text) msgList.add(msg) adapter?.notifyItemInserted(msgList.size-1)//當(dāng)有新消息時(shí),刷新RecycleView中的顯示 view.recyclerView.scrollToPosition(msgList.size-1)//講RecycleView定位到最后一行 } catch (e: JSONException) { e.printStackTrace() } } }
代碼邏輯上面注釋已經(jīng)寫清楚了,就不多贅述?。?!
至此,機(jī)器人的對(duì)話實(shí)現(xiàn)?。?!
到此這篇關(guān)于kotlin實(shí)現(xiàn)語音聊天機(jī)器人案例詳解的文章就介紹到這了,更多相關(guān)kotlin聊天機(jī)器人內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android原生側(cè)滑控件DrawerLayout使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android原生側(cè)滑控件DrawerLayout的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Andriod arcgis保存Mapview為圖片的實(shí)例代碼
這篇文章主要介紹了Andriod arcgis保存Mapview為圖片的實(shí)例代碼 的相關(guān)資料,需要的朋友可以參考下2016-03-03Android?獲取手機(jī)已安裝的應(yīng)用列表實(shí)現(xiàn)詳解
這篇文章主要介紹了Android?獲取手機(jī)已安裝的應(yīng)用列表的實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Android中使用ViewFlipper進(jìn)行手勢(shì)切換實(shí)例
這篇文章主要介紹了Android中使用ViewFlipper進(jìn)行手勢(shì)切換的方法,以實(shí)例形式詳細(xì)講述了XML文件的定義及功能函數(shù)的實(shí)現(xiàn)過程,需要的朋友可以參考下2014-10-10android實(shí)現(xiàn)可上下回彈的scrollview
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)可上下回彈的scrollview,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04怎樣刪除android的gallery中的圖片實(shí)例說明
長(zhǎng)按gallery中的圖片進(jìn)行刪除該圖片的操作,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06