目前还只能自言自语,日后会考虑使用图灵机器人的API写一个完整的AI聊天程序
效果


布局源码
整体布局activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="16dp" android:text="只会说一句话的聊天机器人" android:textAlignment="center" android:textColor="#000000" android:textSize="18sp" />o
<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" android:background="@drawable/bottom_background">
<EditText android:id="@+id/editText" style="@style/Widget.AppCompat.AutoCompleteTextView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_weight="1" android:background="@null" android:layout_gravity="center" android:hint="@string/hint_text" android:maxLines="2" />
<Button android:id="@+id/send_button" style="@style/Widget.AppCompat.Button.Borderless.Colored" android:textColor="#42D363" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/send" /> </LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
|
左聊天气泡布局msg_left_layout.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="100dp" android:padding="10dp">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_margin="5dp" android:background="@drawable/left_chat">
<TextView android:id="@+id/leftText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:textColor="#191919" android:textSize="16dp" /> </LinearLayout> </FrameLayout>
|
右聊天气泡布局msg_right_layout.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="100dp" android:padding="10dp">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_margin="5dp" android:background="@drawable/right_chat">
<TextView android:id="@+id/rightText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:textColor="#fff" android:textSize="16dp" /> </LinearLayout>
</FrameLayout>
|
逻辑实现MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| package com.example.chatui
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.LinearLayout import android.widget.TextView import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import kotlinx.android.synthetic.main.activity_main.* import org.w3c.dom.Text import java.lang.IllegalArgumentException
class Msg(val content:String,val type:Int){ companion object{ const val RIGHT = 0 const val LEFT = 1 } }; class MainActivity : AppCompatActivity() { private val msgList = ArrayList<Msg>()
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)
supportActionBar?.hide() initMsg() recyclerView.layoutManager = LinearLayoutManager(this) val adapter = MsgAdapter(msgList) recyclerView.adapter = adapter send_button.setOnClickListener{ val content:String = editText.text.toString() if(content.isNotEmpty()){ msgList.add(Msg(content,Msg.RIGHT)) adapter.notifyItemInserted(msgList.size-1) recyclerView.scrollToPosition(msgList.size-1) editText.setText("") }
}
}
fun initMsg(){ msgList.add(Msg("Hello",Msg.LEFT)) } } class MsgAdapter(val msgList:List<Msg>):RecyclerView.Adapter<RecyclerView.ViewHolder>(){
inner class RightViewHolder(val view: View):RecyclerView.ViewHolder(view){ val rightMsg: TextView = view.findViewById(R.id.rightText) } inner class LeftViewHolder(val view:View):RecyclerView.ViewHolder(view){ val leftMsg:TextView = view.findViewById(R.id.leftText) } override fun getItemViewType(position: Int): Int { val msg = msgList[position] return msg.type } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { if(viewType==Msg.LEFT){ val leftView = LayoutInflater.from(parent.context).inflate(R.layout.msg_left_layout,parent,false) return LeftViewHolder(leftView) }else{ val rightView = LayoutInflater.from(parent.context).inflate(R.layout.msg_right_layout,parent,false) return RightViewHolder(rightView) }
} override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { val msg = msgList[position] when(holder){ is LeftViewHolder -> holder.leftMsg.text = msg.content is RightViewHolder -> holder.rightMsg.text = msg.content else -> throw IllegalArgumentException() } } override fun getItemCount(): Int { return msgList.size }
}
|
完整代码: https://github.com/cnatom/ChatBot