最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

kotlin實現(xiàn)語音聊天機器人案例詳解

 更新時間:2023年02月16日 10:19:52   作者:weixin_43912367  
Android智能問答機器人是時下非常流行的一種服務,微軟“小冰”的出現(xiàn)更是讓其實實在在的風靡了一把。那么,本文章就將帶領(lǐng)大家完整的實現(xiàn)整個問答機器人的制作

此篇文章緊做關(guān)于語音機器人聊天開發(fā),后續(xù)功能實現(xiàn)請關(guān)注后續(xù)文章?。?!

此篇文章完成后效果展示:

一.機器人聊天—對話adapter的實現(xiàn)

1.準備兩張左右兩邊動畫背景圖片,做left,和right兩邊布局,為Recyclerview的實現(xià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對話列表的實體類,代碼如下:

package com.zrc.smartbutler.entity
/**
 *項目名:  SmartButler
 *包名:    com.zrc.smartbutler.entity
 *文件名:  ChatListData
 *描述:    對話列表的實體類
 */
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對話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
/**
 *項目名:  SmartButler
 *包名:    com.zrc.smartbutler.adapter
 *文件名:  ChatListAdapter
 *描述:    對話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()
        }
    }
}

這段代碼,仿寫自郭霖大神的第一行代碼(第三版),詳情解析,請查看第三行代碼!

至此,對話adapter完成?。?!

二.機器人聊天—機器人實時對話實現(xiàn)

1.導入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
/**
 *項目名:  SmartButler
 *包名:    com.zrc.smartbutler.fragment
 *文件名:  ButlerFragment
 *描述:    服務管家
 */
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.判斷長度不能大于30
             * 4.添加你輸入的內(nèi)容到right item
             * 5.清空當前的輸入框
             * 6.發(fā)送給機器人請求返回內(nèi)容
             * 7.拿到機器人的返回值之后添加在left item
             */
            //1.獲取輸入框的內(nèi)容
            val content = view.inputText.text.toString()
            //2.判斷是否為空
            if(content.isNotEmpty()){
                //3.判斷長度不能大于30
                if(content.length>30){
                    Toast.makeText(activity,"輸入長度超出限制",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)//當有新消息時,刷新RecycleView中的顯示
                    view.recyclerView.scrollToPosition(msgList.size-1)//講RecycleView定位到最后一行
                    //5.清空當前的輸入框
                    view.inputText.setText("")
                    //6.發(fā)送給機器人請求返回內(nèi)容
                    //6.發(fā)送給機器人請求返回內(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.拿到機器人的返回值之后添加在left item
            val msg = ChatListData(ChatListData.TyPE_RECEIVED,text)
            msgList.add(msg)
            adapter?.notifyItemInserted(msgList.size-1)//當有新消息時,刷新RecycleView中的顯示
            view.recyclerView.scrollToPosition(msgList.size-1)//講RecycleView定位到最后一行
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }
}

代碼邏輯上面注釋已經(jīng)寫清楚了,就不多贅述?。。?/p>

至此,機器人的對話實現(xiàn)?。。?/p>

到此這篇關(guān)于kotlin實現(xiàn)語音聊天機器人案例詳解的文章就介紹到這了,更多相關(guān)kotlin聊天機器人內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

清河县| 佛坪县| 凤城市| 手游| 眉山市| 静安区| 奈曼旗| 荔浦县| 建昌县| 叶城县| 洞头县| 元阳县| 邯郸市| 平湖市| 安阳县| 图片| 磐安县| 祁阳县| 通州区| 梅河口市| 桑植县| 澄迈县| 芦山县| 天柱县| 宝兴县| 雷山县| 丹凤县| 贵定县| 潼南县| 玉树县| 保定市| 英超| 措美县| 宣汉县| 若羌县| 云南省| 类乌齐县| 东乡族自治县| 文水县| 胶南市| 宁陕县|