经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android Studio实现智能聊天
来源:jb51  时间:2022/7/20 13:09:03  对本文有异议

本文实例为大家分享了Android Studio实现智能聊天的具体代码,供大家参考,具体内容如下

1、布局activit_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. ? ? xmlns:app="http://schemas.android.com/apk/res-auto"
  4. ? ? xmlns:tools="http://schemas.android.com/tools"
  5. ? ? android:layout_width="match_parent"
  6. ? ? android:layout_height="match_parent"
  7. ? ? android:orientation="vertical"
  8. ? ? tools:context=".MainActivity">
  9. ?
  10. ? ? <androidx.recyclerview.widget.RecyclerView
  11. ? ? ? ? android:layout_width="match_parent"
  12. ? ? ? ? android:layout_height="0dp"
  13. ? ? ? ? android:layout_weight="1"
  14. ? ? ? ? android:id="@+id/recycle">
  15. ?
  16. ?
  17. ? ? </androidx.recyclerview.widget.RecyclerView>
  18. ?
  19. ? ? <LinearLayout
  20. ? ? ? ? android:layout_width="match_parent"
  21. ? ? ? ? android:layout_height="wrap_content"
  22. ? ? ? ? android:orientation="horizontal">
  23. ?
  24. ? ? ? ? <EditText
  25. ? ? ? ? ? ? android:layout_width="0dp"
  26. ? ? ? ? ? ? android:layout_height="wrap_content"
  27. ? ? ? ? ? ? android:layout_weight="1"
  28. ? ? ? ? ? ? android:id="@+id/input"/>
  29. ?
  30. ? ? ? ? <Button
  31. ? ? ? ? ? ? android:layout_width="wrap_content"
  32. ? ? ? ? ? ? android:layout_height="wrap_content"
  33. ? ? ? ? ? ? android:id="@+id/send"
  34. ? ? ? ? ? ? android:text="发送"/>
  35. ? ? </LinearLayout>
  36.  
  37. </LinearLayout>

2、创建子布局msg_item,显示聊天对话框

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. ? ? android:orientation="vertical"
  4. ? ? android:layout_width="match_parent"
  5. ? ? android:padding="10dp"
  6. ? ? android:layout_height="wrap_content">
  7. ?
  8. ?
  9. ? ? <LinearLayout
  10. ? ? ? ? android:layout_width="wrap_content"
  11. ? ? ? ? android:layout_height="wrap_content"
  12. ? ? ? ? android:id="@+id/left_layout"
  13. ? ? ? ? android:layout_gravity="left"
  14. ? ? ? ? android:background="@drawable/message_left">
  15. ?
  16. ? ? ? ? <TextView
  17. ? ? ? ? ? ? android:layout_width="wrap_content"
  18. ? ? ? ? ? ? android:layout_height="wrap_content"
  19. ? ? ? ? ? ? android:textSize="20sp"
  20. ? ? ? ? ? ? android:layout_marginTop="10dp"
  21. ? ? ? ? ? ? android:id="@+id/left_msg"/>
  22. ? ? </LinearLayout>
  23. ?
  24. ? ? <LinearLayout
  25. ? ? ? ? android:layout_width="wrap_content"
  26. ? ? ? ? android:layout_height="wrap_content"
  27. ? ? ? ? android:id="@+id/right_layout"
  28. ? ? ? ? android:layout_gravity="right"
  29. ? ? ? ? android:layout_marginLeft="10dp"
  30. ? ? ? ? android:background="@drawable/message_right">
  31. ?
  32. ? ? ? ? <TextView
  33. ? ? ? ? ? ? android:layout_width="wrap_content"
  34. ? ? ? ? ? ? android:layout_height="wrap_content"
  35. ? ? ? ? ? ? android:textSize="20sp"
  36. ? ? ? ? ? ? android:layout_marginTop="10dp"
  37. ? ? ? ? ? ? android:id="@+id/right_msg"/>
  38. </LinearLayout>

3、创建类Msg获取数据

  1. public class Msg {
  2. ?
  3. ? ? public static final int MSG_RECEIVED = 0;
  4. ? ? public static final int MSG_SEND =1 ;
  5. ?
  6. ? ? private String content;
  7. ? ? private int type;
  8. ?
  9. ? ? public Msg(String content,int type){
  10. ? ? ? ? this.content=content;
  11. ? ? ? ? this.type=type;
  12. ? ? }
  13. ?
  14. ? ? public String getContent() {
  15. ? ? ? ? return content;
  16. ? ? }
  17. ?
  18. ? ? public int getType() {
  19. ? ? ? ? return type;
  20. ? ? }
  21. }

4、创建RecyclerView的适配器,MsgAdapter继RecyclerView.Adapter<MsgAdapter.ViewHolder>

  1. ?public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
  2. ? ? private List<Msg> mMsgList;
  3. ?
  4. ? ? public class ViewHolder extends RecyclerView.ViewHolder {
  5. ?
  6. ? ? ? ? LinearLayout leftLayout;
  7. ? ? ? ? TextView leftMsg;
  8. ? ? ? ? LinearLayout rightLayout;
  9. ? ? ? ? TextView rightMsg;
  10. ?
  11. ? ? ? ? public ViewHolder(@NonNull View itemView) {
  12. ? ? ? ? ? ? super(itemView);
  13. ?
  14. ? ? ? ? ? ? leftLayout=itemView.findViewById(R.id.left_layout);
  15. ? ? ? ? ? ? rightLayout=itemView.findViewById(R.id.right_layout);
  16. ? ? ? ? ? ? leftMsg=itemView.findViewById(R.id.left_msg);
  17. ? ? ? ? ? ? rightMsg=itemView.findViewById(R.id.right_msg);
  18. ?
  19. ? ? ? ? }
  20. ? ? }
  21. ?
  22. ? ? public MsgAdapter(List<Msg> msgList){
  23. ? ? ? ? mMsgList=msgList;
  24. ? ? }
  25. ? ? @NonNull
  26. ? ? @Override
  27. ? ? public MsgAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  28. ? ? ? ? View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
  29. ? ? ? ? ViewHolder holder=new ViewHolder(view);
  30. ? ? ? ? return holder;
  31. ? ? }
  32. ?
  33. ? ? @Override
  34. ? ? public void onBindViewHolder(@NonNull MsgAdapter.ViewHolder holder, int position) {
  35. ? ? ? ? Msg msg=mMsgList.get(position);
  36. ?
  37. ? ? ? ? if (msg.getType()==Msg.MSG_RECEIVED){
  38. ? ? ? ? ? ? holder.leftLayout.setVisibility(View.VISIBLE);
  39. ? ? ? ? ? ? holder.rightLayout.setVisibility(View.GONE);
  40. ? ? ? ? ? ? holder.leftMsg.setText(msg.getContent());
  41. ? ? ? ? }else if (msg.getType()==Msg.MSG_SEND){
  42. ? ? ? ? ? ? holder.leftLayout.setVisibility(View.GONE);
  43. ? ? ? ? ? ? holder.rightLayout.setVisibility(View.VISIBLE);
  44. ? ? ? ? ? ? holder.rightMsg.setText(msg.getContent());
  45. ?
  46. ? ? ? ? }
  47. ?
  48. ? ? }
  49. ?
  50. ? ? @Override
  51. ? ? public int getItemCount() {
  52. ? ? ? ? return mMsgList.size();
  53. ? ? }

5、创建 RobotManager类封装网络,网络地址:青云客,智能聊天机器人

  1. public class RobotManager {
  2. ? ? private static String Url="http://api.qingyunke.com/api.php?key=free&appid=0&msg=!!";
  3. ?
  4. ? ? public static String getUrl(String question){
  5. ? ? ? ? String real_Url=Url.replace("!!",question);
  6. ? ? ? ? return real_Url;
  7. ? ? }
  8. }

6、逻辑

  1. public class MainActivity extends AppCompatActivity {
  2. ? ? private static String TAG="MainActivity";
  3. ?
  4. ?
  5. ? ? private List<Msg> msgList = new ArrayList<>();
  6. ? ? private EditText input;
  7. ? ? private RecyclerView recyclerView;
  8. ? ? private LinearLayoutManager manager;
  9. ? ? private Button button;
  10. ? ? private MsgAdapter adapter;
  11. ? ? private String input_text;
  12. ? ? private StringBuilder response;
  13. ?
  14. ? ? private Handler handler = new Handler() {
  15. ? ? @Override
  16. ? ? ? ? public void handleMessage(Message msg) {
  17. ? ? ? ? ? ? //获取解析数据,显示在Recycle中
  18. ? ? ? ? ? ? Bundle data = msg.getData();
  19. ? ? ? ? ? ? String result = data.getString("result");
  20. ?
  21. ? ? ? ? ? ? Msg msg_get = new Msg(result, Msg.MSG_RECEIVED);
  22. ? ? ? ? ? ? msgList.add(msg_get);
  23. ? ? ? ? ? ? ? ??
  24. ? ? ? ? ? ? ? ? //数据刷新
  25. ? ? ? ? ? ? adapter.notifyItemInserted(msgList.size() - 1);
  26. ? ? ? ? ? ? recyclerView.scrollToPosition(msgList.size() - 1);
  27. ?
  28. ?
  29. ? ? ? ? }
  30. ?
  31. ?
  32. ? ? };
  33. ? ? @Override
  34. ? ? protected void onCreate(Bundle savedInstanceState) {
  35. ? ? ? ? super.onCreate(savedInstanceState);
  36. ? ? ? ? setContentView(R.layout.activity_main);
  37. ?
  38. ?
  39. ? ? ? ? initMsg();//初始化数据
  40. ?
  41. ?
  42. ? ? ? ? recyclerView = findViewById(R.id.recycle);
  43. ? ? ? ? button = findViewById(R.id.send);
  44. ? ? ? ? input = findViewById(R.id.input);
  45. ?
  46. ? ? ? ? manager = new LinearLayoutManager(this);
  47. ? ? ? ? recyclerView.setLayoutManager(manager);
  48. ? ? ? ? adapter = new MsgAdapter(msgList);
  49. ? ? ? ? recyclerView.setAdapter(adapter);
  50. ?
  51. ? ? ? ? button.setOnClickListener(new View.OnClickListener() {
  52. ? ? ? ? ? ? @Override
  53. ? ? ? ? ? ? public void onClick(View view) {
  54. ? ? ? ? ? ? ? ? input_text = input.getText().toString();
  55. ? ? ? ? ? ? ? ? Msg msg = new Msg(input_text, Msg.MSG_SEND);
  56. ? ? ? ? ? ? ? ? msgList.add(msg);
  57. ?
  58. ? ? ? ? ? ? ? ? adapter.notifyItemInserted(msgList.size() - 1);
  59. ? ? ? ? ? ? ? ? recyclerView.scrollToPosition(msgList.size() - 1);
  60. ? ? ? ? ? ? ? ? input.setText("");
  61. ?
  62. ? ? ? ? ? ? ? ? getInter(); ? //发起网络请求
  63. ?
  64. ? ? ? ? ? ? }
  65. ?
  66. ? ? ? ? });
  67. ?
  68. ? ? }
  69. ?
  70. ?
  71. ? ? private void getInter() {
  72. ? ? ? ? //开起线程
  73. ? ? ? ? new Thread(new Runnable() {
  74. ? ? ? ? ? ? @Override
  75. ? ? ? ? ? ? public void run() {
  76. ? ? ? ? ? ? ? ? HttpURLConnection connection = null;
  77. ? ? ? ? ? ? ? ? BufferedReader reader = null;
  78. ? ? ? ? ? ? ? ? try {
  79. ? ? ? ? ? ? ? ? ? ? URL url = new URL(RobotManager.getUrl(input_text));
  80. ? ? ? ? ? ? ? ? ? ? connection = (HttpURLConnection) url.openConnection();
  81. ? ? ? ? ? ? ? ? ? ? connection.setRequestMethod("GET");
  82. ? ? ? ? ? ? ? ? ? ? connection.setReadTimeout(8000);
  83. ? ? ? ? ? ? ? ? ? ? connection.setConnectTimeout(8000);
  84. ?
  85. ? ? ? ? ? ? ? ? ? ? InputStream in = connection.getInputStream();
  86. ?
  87. ? ? ? ? ? ? ? ? ? ? reader = new BufferedReader(new InputStreamReader(in));
  88. ? ? ? ? ? ? ? ? ? ? StringBuilder response = new StringBuilder();
  89. ? ? ? ? ? ? ? ? ? ? String line = "";
  90. ? ? ? ? ? ? ? ? ? ? while ((line = reader.readLine()) != null) {
  91. ? ? ? ? ? ? ? ? ? ? ? ? response.append(line);
  92. ? ? ? ? ? ? ? ? ? ? }
  93. ?
  94. ? ? ? ? ? ? ? ? ? ? // 2,解析获得的数据
  95. ? ? ? ? ? ? ? ? ? ? Gson gson=new Gson();
  96. ? ? ? ? ? ? ? ? ? ? Msg msg=gson.fromJson(response.toString(),Msg.class);
  97. ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "result:" + msg.getType());
  98. ? ? ? ? ? ? ? ? ? ? Log.d(TAG, "content:" + msg.getContent());
  99. ?
  100. ?
  101. ? ? ? ? ? ? ? ? ? ? // 3,将解析的数据保存到 Message中,传递到主线程中显示
  102. ? ? ? ? ? ? ? ? ? ? Bundle data=new Bundle();
  103. ? ? ? ? ? ? ? ? ? ? Message msg1=new Message();
  104. ? ? ? ? ? ? ? ? ? ? if (msg.getType()==0){
  105. ? ? ? ? ? ? ? ? ? ? ? ? data.putString("result",msg.getContent());
  106. ? ? ? ? ? ? ? ? ? ? }else {
  107. ? ? ? ? ? ? ? ? ? ? ? ? data.putString("result","我不知道你在说什么!");
  108. ? ? ? ? ? ? ? ? ? ? }
  109. ? ? ? ? ? ? ? ? ? ? msg1.setData(data);
  110. ? ? ? ? ? ? ? ? ? ? msg1.what=1;
  111. ? ? ? ? ? ? ? ? ? ? handler.sendMessage(msg1);
  112.  
  113. ?
  114. ? ? ? ? ? ? ? ? } catch (MalformedURLException e) {
  115. ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
  116. ? ? ? ? ? ? ? ? } catch (ProtocolException e) {
  117. ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
  118. ? ? ? ? ? ? ? ? } catch (IOException e) {
  119. ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
  120. ? ? ? ? ? ? ? ? } finally {
  121. ? ? ? ? ? ? ? ? ? ? if (reader != null) {
  122. ? ? ? ? ? ? ? ? ? ? ? ? try {
  123. ? ? ? ? ? ? ? ? ? ? ? ? ? ? reader.close();
  124. ?
  125. ? ? ? ? ? ? ? ? ? ? ? ? } catch (IOException e) {
  126. ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
  127. ? ? ? ? ? ? ? ? ? ? ? ? }
  128. ? ? ? ? ? ? ? ? ? ? }
  129. ? ? ? ? ? ? ? ? ? ? if (connection != null) {
  130. ? ? ? ? ? ? ? ? ? ? ? ? connection.disconnect();
  131. ? ? ? ? ? ? ? ? ? ? }
  132. ? ? ? ? ? ? ? ? }
  133. ? ? ? ? ? ? }
  134. ?
  135. ?
  136. ? ? ? ? }).start();
  137. ? ? }
  138. ?
  139. ?
  140. ? ? private void initMsg() {
  141. ? ? ? ? Msg msg = new Msg("我是菲菲,快来和我聊天吧!", Msg.MSG_RECEIVED);
  142. ? ? ? ? msgList.add(msg);
  143. ? ? }
  144. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号