代码包括四个部分
package com.jiang.jzj_qq.wechat; import java.util.HashMap; public class Answer { public static HashMap points = new HashMap(); public static HashMap createHashMap(){ points.put(Integer.valueOf(0), "你真的特别想要一个全新的开始"); points.put(Integer.valueOf(1), "你还有别的选择吗"); points.put(Integer.valueOf(2), "克制自己"); points.put(Integer.valueOf(3), "这听起来相当不错"); points.put(Integer.valueOf(4), "到目前为止没有什么不好的"); points.put(Integer.valueOf(5), "别担心"); points.put(Integer.valueOf(6), "坚持下去"); points.put(Integer.valueOf(7), "再试试"); points.put(Integer.valueOf(8), "你得尽力而为"); points.put(Integer.valueOf(9), "付出总有回报"); points.put(Integer.valueOf(10), "这没什么区别"); points.put(Integer.valueOf(11), "机不可失失不再来"); points.put(Integer.valueOf(12), "管好你自己的事"); points.put(Integer.valueOf(13), "老天也许在给你准备惊喜"); points.put(Integer.valueOf(14), "成为你想成为的人 而不是别人想看到的"); points.put(Integer.valueOf(15), "自由"); points.put(Integer.valueOf(16), "过去的就让它过去吧"); points.put(Integer.valueOf(17), "不跟随"); points.put(Integer.valueOf(18), "跟随自己内心的同时也带着点脑子"); points.put(Integer.valueOf(19), "保持乐观没错但你好像太乐观了"); points.put(Integer.valueOf(20), "不经历风雨 怎么见彩虹"); points.put(Integer.valueOf(21), "你开心就好"); points.put(Integer.valueOf(22), "有时候转变一下自己的态度就能获得幸福"); points.put(Integer.valueOf(23), "不要回头"); points.put(Integer.valueOf(24), "是你的就是你的"); points.put(Integer.valueOf(25), "你面临着无数机会"); points.put(Integer.valueOf(26), "别惹麻烦"); points.put(Integer.valueOf(27), "没有不长杂草的花园"); points.put(Integer.valueOf(28), "没人能骑在你的背上除非你弯下腰"); points.put(Integer.valueOf(29), "反败为胜"); points.put(Integer.valueOf(30), "不要把生命浪费在怀疑与恐惧中"); points.put(Integer.valueOf(31), "别忘了吸取教训"); points.put(Integer.valueOf(32), "无论好与不好 思考它的意义"); points.put(Integer.valueOf(33), "自信但别骄傲"); points.put(Integer.valueOf(34), "忍耐是一种美德"); points.put(Integer.valueOf(35), "一报还一报"); points.put(Integer.valueOf(36), "想每一个人都讨好 偏偏容易一个都讨不好"); points.put(Integer.valueOf(37), "不会撑船怪河弯"); points.put(Integer.valueOf(38), "无论怎样天塌不下来"); points.put(Integer.valueOf(39), "不要揽事过多"); points.put(Integer.valueOf(40), "风水轮流转"); points.put(Integer.valueOf(41), "眼不见心不烦"); points.put(Integer.valueOf(42), "谨慎消灾"); points.put(Integer.valueOf(43), "最好的总会在最不经意的时候出现"); points.put(Integer.valueOf(44), "好了伤疤忘了疼痛"); points.put(Integer.valueOf(45), "健康就是幸福"); points.put(Integer.valueOf(46), "你运气不错"); points.put(Integer.valueOf(47), "历史往往会重演"); points.put(Integer.valueOf(48), "这就是人生"); points.put(Integer.valueOf(49), "萝卜青菜 各有所爱"); points.put(Integer.valueOf(50), "一个巴掌拍不响"); points.put(Integer.valueOf(51), "梦不足信 眼见为实"); points.put(Integer.valueOf(52), "这很重要"); points.put(Integer.valueOf(53), "外表是靠不住的"); points.put(Integer.valueOf(54), "保持冷静继续前行"); points.put(Integer.valueOf(55), "永远不要忘了说谢谢"); points.put(Integer.valueOf(56), "你宁可信我都不信你自己吗"); points.put(Integer.valueOf(57), "有没有觉得自己很搞笑"); points.put(Integer.valueOf(58), "我也没什么好主意"); points.put(Integer.valueOf(59), "没有什么是不可能的"); points.put(Integer.valueOf(60), "这太难了"); points.put(Integer.valueOf(61), "可能吧"); points.put(Integer.valueOf(62), "别傻了"); points.put(Integer.valueOf(63), "反正没有一成不变的事"); points.put(Integer.valueOf(64), "再想想"); points.put(Integer.valueOf(65), "我想偷会懒 请你勤快点"); points.put(Integer.valueOf(66), "麻烦来了"); points.put(Integer.valueOf(67), "细节 注意细节"); points.put(Integer.valueOf(68), "再打听打听"); points.put(Integer.valueOf(69), "问问朋友的意见"); points.put(Integer.valueOf(70), "别那么小气"); points.put(Integer.valueOf(71), "别乱来"); points.put(Integer.valueOf(72), "这对你是个打击"); points.put(Integer.valueOf(73), "会有个不错的结果的"); points.put(Integer.valueOf(74), "可能得花点钱"); points.put(Integer.valueOf(75), "你一个人搞不定"); points.put(Integer.valueOf(76), "你捡到宝了"); points.put(Integer.valueOf(77), "吃点东西冷静一下"); points.put(Integer.valueOf(78), "态度坚决点"); points.put(Integer.valueOf(79), "金钱不是万能的"); points.put(Integer.valueOf(80), "情况正在好转"); points.put(Integer.valueOf(81), "别再用你的过去欺骗自己"); points.put(Integer.valueOf(82), "坦然接受吧"); points.put(Integer.valueOf(83), "忙碌是一种幸福"); points.put(Integer.valueOf(84), "保持眼界的开阔"); points.put(Integer.valueOf(85), "不要停下你的脚步"); points.put(Integer.valueOf(86), "不要总和别人比较"); points.put(Integer.valueOf(87), "耐心点"); points.put(Integer.valueOf(88), "相信老天是公平的"); points.put(Integer.valueOf(89), "欲速则不达"); points.put(Integer.valueOf(90), "这对你有用吗"); points.put(Integer.valueOf(91), "凭直觉做事"); points.put(Integer.valueOf(92), "慢慢来"); points.put(Integer.valueOf(93), "世事难料"); points.put(Integer.valueOf(94), "做个快乐的人"); points.put(Integer.valueOf(95), "拒绝"); points.put(Integer.valueOf(96), "这风险可不小"); points.put(Integer.valueOf(97), "抱最好的愿望做最坏的打算"); points.put(Integer.valueOf(98), "准了"); points.put(Integer.valueOf(99), "没有可不可以 只有愿不愿意"); points.put(Integer.valueOf(100), "可能是你的问题"); points.put(Integer.valueOf(101), "绝对办不到"); points.put(Integer.valueOf(102), "天晓得"); points.put(Integer.valueOf(103), "这是个伤脑筋的问题"); points.put(Integer.valueOf(104), "先把它搁一边"); points.put(Integer.valueOf(105), "死胡同"); points.put(Integer.valueOf(106), "顶多道个歉就差不多了"); points.put(Integer.valueOf(107), "世界真是小"); points.put(Integer.valueOf(108), "这不会花费太长时间"); points.put(Integer.valueOf(109), "快点去做"); points.put(Integer.valueOf(110), "是的"); points.put(Integer.valueOf(111), "不是"); points.put(Integer.valueOf(112), "这是一个秘密"); points.put(Integer.valueOf(113), "有个积极的心态很重要"); points.put(Integer.valueOf(114), "好运即将到来"); points.put(Integer.valueOf(115), "现在不合适"); points.put(Integer.valueOf(116), "放松一点会拥有更多"); points.put(Integer.valueOf(117), "你最终会得到你想要的"); points.put(Integer.valueOf(118), "这并不受你的控制"); points.put(Integer.valueOf(119), "且行且珍惜"); points.put(Integer.valueOf(120), "说出来"); points.put(Integer.valueOf(121), "不要拖延"); points.put(Integer.valueOf(122), "不要受别人的影响"); points.put(Integer.valueOf(123), "这很奇怪"); points.put(Integer.valueOf(124), "这件事是难以忘记的"); points.put(Integer.valueOf(125), "还有其他选择"); points.put(Integer.valueOf(126), "这件事会给你极大的乐趣"); points.put(Integer.valueOf(127), "就赌这个"); points.put(Integer.valueOf(128), "你可能不会同意"); points.put(Integer.valueOf(129), "先让自己睡个好觉"); points.put(Integer.valueOf(130), "机会就在你你面前"); points.put(Integer.valueOf(131), "这不是很理想"); points.put(Integer.valueOf(132), "你必须放弃一些其它东西"); points.put(Integer.valueOf(133), "承担"); points.put(Integer.valueOf(134), "你会得到很多支持的"); points.put(Integer.valueOf(135), "如果你愿意的话"); points.put(Integer.valueOf(136), "这不值得你努力"); points.put(Integer.valueOf(137), "快点"); points.put(Integer.valueOf(138), "仔细想想你就会知道"); points.put(Integer.valueOf(139), "它与以往不同"); points.put(Integer.valueOf(140), "你知道现在是最好的时机"); points.put(Integer.valueOf(141), "你得改正这个缺点"); points.put(Integer.valueOf(142), "冷静下来做个好决定"); points.put(Integer.valueOf(143), "再问一遍"); points.put(Integer.valueOf(144), "最终不会让你失望"); points.put(Integer.valueOf(145), "保持距离"); points.put(Integer.valueOf(146), "等你再大一点就会知道"); points.put(Integer.valueOf(147), "无需纠结"); points.put(Integer.valueOf(148), "只试一次"); points.put(Integer.valueOf(149), "你知道这并不会让你放松"); points.put(Integer.valueOf(150), "这可能是个意外"); points.put(Integer.valueOf(151), "演好你的角色"); points.put(Integer.valueOf(152), "不要勉强接受你所不满意的"); points.put(Integer.valueOf(153), "你需要更多的方法"); points.put(Integer.valueOf(154), "当心"); points.put(Integer.valueOf(155), "想尽一切办法 只要你能做到"); points.put(Integer.valueOf(156), "做一个表格"); points.put(Integer.valueOf(157), "这可无法保证"); points.put(Integer.valueOf(158), "最好的办法可能不是你最想要的"); points.put(Integer.valueOf(159), "最好换个时间"); points.put(Integer.valueOf(160), "这似乎是毋庸置疑的"); points.put(Integer.valueOf(161), "持怀疑态度"); points.put(Integer.valueOf(162), "它是无法预料的"); points.put(Integer.valueOf(163), "你就是个好奇宝宝"); points.put(Integer.valueOf(164), "生活就是斗争"); points.put(Integer.valueOf(165), "别对自己太苛刻了"); points.put(Integer.valueOf(166), "万事皆有缘由"); points.put(Integer.valueOf(167), "不管黑夜有多长 天总会亮"); points.put(Integer.valueOf(168), "不要紧"); points.put(Integer.valueOf(169), "越在意就会失去越多"); points.put(Integer.valueOf(170), "这不管用"); points.put(Integer.valueOf(171), "保持合作"); points.put(Integer.valueOf(172), "累了就睡觉"); points.put(Integer.valueOf(173), "翻到这一页确实需要勇气"); return points; } } package com.jiang.jzj_qq.wechat; public class Message { public static final int TYPE_RECIVE = 0; public static final int TYPE_SEND = 1; private String content; private int type; public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getType() { return type; } public void setType(int type) { this.type = type; } public Message(String content, int type){ this.content = content; this.type = type; } } package com.jiang.jzj_qq.wechat; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.jiang.jzj_qq.R; import java.util.List; public class MsgAtapter extends RecyclerView.Adapter<MsgAtapter.MyViewHoder> { private Context mcontext; private List<Message> msg; public MsgAtapter(Context context, List<Message> msgList){ this.mcontext = context; this.msg = msgList; } @NonNull @Override public MyViewHoder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(mcontext).inflate(R.layout.item_recyclerview_layout,parent,false); MyViewHoder myViewHoder = new MyViewHoder(view); return myViewHoder; } @Override public void onBindViewHolder(@NonNull MyViewHoder holder, int position) { Message message = msg.get(position); if(message.getType()==Message.TYPE_RECIVE){ holder.leftlayout.setVisibility(View.VISIBLE); holder.rightlayout.setVisibility(View.GONE); holder.leftmsg.setText(message.getContent()); } if(message.getType()==Message.TYPE_SEND){ holder.leftlayout.setVisibility(View.GONE); holder.rightlayout.setVisibility(View.VISIBLE); holder.rightmsg.setText(message.getContent()); } } @Override public int getItemCount() { return msg.size(); } class MyViewHoder extends RecyclerView.ViewHolder{ private LinearLayout rightlayout; private LinearLayout leftlayout; private TextView rightmsg; private TextView leftmsg; public MyViewHoder(@NonNull View itemView) { super(itemView); rightlayout = itemView.findViewById(R.id.right_layout); leftlayout = itemView.findViewById(R.id.left_layout); rightmsg = itemView.findViewById(R.id.right_msg); leftmsg = itemView.findViewById(R.id.left_msg); } } } package com.jiang.jzj_qq.wechat; import android.os.Bundle; import android.view.View; import android.widget.EditText; import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.jiang.jzj_qq.R; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class SmartRoot extends AppCompatActivity { private RecyclerView recyclerView; private List<Message> msgList = new ArrayList<>(); private HashMap ansewer = Answer.createHashMap(); private MsgAtapter msgAtapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_smartroot_layout); initmsg(); recyclerView = findViewById(R.id.myrecyclerview); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); recyclerView.setLayoutManager(linearLayoutManager); msgAtapter = new MsgAtapter(this,msgList); recyclerView.setAdapter(msgAtapter); } private List<Message> initmsg() { Message message = new Message("hello,what do you know?",Message.TYPE_RECIVE); msgList.add(message); return msgList; } public void send(View v){ EditText msget = findViewById(R.id.msgEt); String msg = msget.getText().toString().trim(); Message sendMessage = new Message(msg,Message.TYPE_SEND); msgList.add(sendMessage); String reciveAnswer = (String) ansewer.get((int) (Math.random() * (ansewer.size() - 1))); Message reclveMessage = new Message(reciveAnswer,Message.TYPE_RECIVE); msgList.add(reclveMessage); msgAtapter.notifyDataSetChanged(); } }对应的xml文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" android:orientation="vertical"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:text="Smart Robot" android:textSize="24dp" android:textStyle="bold" android:gravity="center" android:background="#EBEBEB"/> <androidx.recyclerview.widget.RecyclerView android:id="@+id/myrecyclerview" android:layout_below="@id/title" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_margin="8dp" android:layout_alignParentBottom="true"> <EditText android:id="@+id/msgEt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:layout_gravity=""/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="send" android:onClick="send" android:textStyle="bold"/> </LinearLayout> </RelativeLayout>