尬聊器(伪聊天机器人)

尬聊器(伪聊天机器人)_第1张图片尬聊器(伪聊天机器人)_第2张图片

哈哈哈就只会回这句,实现如下

首先要在app/build.gradle中加依赖库

compile 'com.android.support:recyclerview-v7:23.4.0'要跟上面一样版本

尬聊器(伪聊天机器人)_第3张图片


接着主xml




    

    

        

        
新建定义消息的实体类Msg。

package com.graduationdesign.pxc.wefuck;

import java.security.PublicKey;


public class Msg {
    public static final int TYPE_RECIVED = 0;
    public static final int TYPE_SENT = 1;
    private String content;
    private int type;

    public Msg(String content, int type) {
        this.content = content;//消息内容
        this.type = type;//消息类型
    }

    public String getContent() {
        return content;
    }

    public int getType() {
        return type;
    }
}
接着新建msg_item.xml来保存消息框的布局,使收到消息居左,发出居右




    

        
    

    

        
    
新建RecyclerView的适配器类,新建类MsgAdapter.java
package com.graduationdesign.pxc.wefuck;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

public class MsgAdapter extends RecyclerView.Adapter{
    private List mMsgList;
    static class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout leftLayout;
        LinearLayout rightLayout;
        TextView leftMsg;
        TextView rightMsg;
        public ViewHolder(View view){
            super(view);
            leftLayout=(LinearLayout)view.findViewById(R.id.left_layout);
            rightLayout=(LinearLayout)view.findViewById(R.id.right_layout);
            leftMsg=(TextView)view.findViewById(R.id.left_msg);
            rightMsg=(TextView)view.findViewById(R.id.right_msg);
        }
    }

    public MsgAdapter(List msgList) {
        mMsgList=msgList;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
        return  new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Msg msg=mMsgList.get(position);
        if (msg.getType()==Msg.TYPE_RECIVED){
            //如果是收到的消息,则显示在左边的消息布局,将右边的消息布局隐藏
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftMsg.setText(msg.getContent());
        }else if(msg.getType()==Msg.TYPE_SENT){
            //如果是发出的消息,则显示在右边的消息布局,将左边的消息布局隐藏
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightMsg.setText(msg.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }


}

最后修改主类
package com.graduationdesign.pxc.wefuck;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List msgList = new ArrayList<>();
    private EditText inputText;
    private Button send;
    private RecyclerView msgRecyclerView;
    private MsgAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initMsgs();//初始化消息数据
        inputText = (EditText) findViewById(R.id.input_text);
        send = (Button) findViewById(R.id.send);
        msgRecyclerView = (RecyclerView) findViewById(R.id.msg_recyclear_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        msgRecyclerView.setLayoutManager(layoutManager);
        adapter = new MsgAdapter(msgList);
        msgRecyclerView.setAdapter(adapter);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = inputText.getText().toString();
                String conre = "嗯,你多喝热水";
                if (!"".equals(content)) {
                    Msg msg = new Msg(content, Msg.TYPE_SENT);
                    msgList.add(msg);
                    adapter.notifyItemInserted(msgList.size() - 1);//当有新消息时,刷新ListView中的显示
                    msgRecyclerView.scrollToPosition(msgList.size() - 1);//将ListView定位到最后一行
                    inputText.setText("");//清空输入框中的内容
                    Msg msg0 = new Msg(conre, Msg.TYPE_RECIVED);
                    msgList.add(msg0);
                    adapter.notifyItemInserted(msgList.size() - 1);//当有新消息时,刷新ListView中的显示
                    msgRecyclerView.scrollToPosition(msgList.size() - 1);//将ListView定位到最后一行
                    inputText.setText("");//清空输入框中的内容
                }
            }
        });
    }

    private void initMsgs() {
        Msg msg1 = new Msg("你好呀,小可爱。", Msg.TYPE_RECIVED);
        msgList.add(msg1);
        Msg msg2 = new Msg("弱弱的问,你谁啊?", Msg.TYPE_SENT);
        msgList.add(msg2);
        Msg msg3 = new Msg("你猜。", Msg.TYPE_RECIVED);
        msgList.add(msg3);
    }
}

就这样,一个只会回复

"嗯,你多喝热水"

的伪机器人聊天框就好了






你可能感兴趣的:(Android,Studio菜鸟日记)