欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

此篇文章緊做關于語音機器人聊天開發(fā),后續(xù)功能實現(xià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)寫清楚了,就不多贅述?。?!

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

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

相關文章

  • Android原生側滑控件DrawerLayout使用方法詳解

    Android原生側滑控件DrawerLayout使用方法詳解

    這篇文章主要為大家詳細介紹了Android原生側滑控件DrawerLayout的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android仿QQ聊天撒花特效 很真實

    Android仿QQ聊天撒花特效 很真實

    本文寫的這個特效,是關于聊天的,你肯定遇到過,就是你跟人家聊天的時候,比如發(fā)送應(么么噠),然后屏幕上全部就是表情了,今天我們就是做這個,撒花的特效,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Andriod arcgis保存Mapview為圖片的實例代碼

    Andriod arcgis保存Mapview為圖片的實例代碼

    這篇文章主要介紹了Andriod arcgis保存Mapview為圖片的實例代碼 的相關資料,需要的朋友可以參考下
    2016-03-03
  • Android?獲取手機已安裝的應用列表實現(xiàn)詳解

    Android?獲取手機已安裝的應用列表實現(xiàn)詳解

    這篇文章主要介紹了Android?獲取手機已安裝的應用列表的實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • Android中使用ViewFlipper進行手勢切換實例

    Android中使用ViewFlipper進行手勢切換實例

    這篇文章主要介紹了Android中使用ViewFlipper進行手勢切換的方法,以實例形式詳細講述了XML文件的定義及功能函數(shù)的實現(xiàn)過程,需要的朋友可以參考下
    2014-10-10
  • android實現(xiàn)可上下回彈的scrollview

    android實現(xiàn)可上下回彈的scrollview

    這篇文章主要為大家詳細介紹了android實現(xiàn)可上下回彈的scrollview,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Retrofit2日志攔截器的使用

    Retrofit2日志攔截器的使用

    這篇文章主要介紹了Retrofit2日志攔截器的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 怎樣刪除android的gallery中的圖片實例說明

    怎樣刪除android的gallery中的圖片實例說明

    長按gallery中的圖片進行刪除該圖片的操作,具體實現(xiàn)如下,感興趣的朋友可以參考下哈
    2013-06-06
  • Android調(diào)用第三方QQ登錄代碼分享

    Android調(diào)用第三方QQ登錄代碼分享

    現(xiàn)在的項目開發(fā),調(diào)用第三方登錄,幾乎是必須的,這篇文章主要介紹了Android調(diào)用第三方QQ登錄代碼分享
    2016-05-05
  • Android仿網(wǎng)易云音樂播放界面

    Android仿網(wǎng)易云音樂播放界面

    這篇文章主要為大家詳細介紹了Android仿網(wǎng)易云音樂播放界面,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評論