赞
踩
(空白处与本篇内容无关)
<?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:orientation="vertical"
android:background="@drawable/background_main"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/msg_recycle_view"
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/input_text"
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:text="发送"
android:background="#1E8F34"
android:textColor="#fff"/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:id="@+id/left_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="10dp"
android:background="@drawable/message_left"
tools:ignore="RtlHardcoded">
<TextView
android:id="@+id/left_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:id="@+id/right_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:background="@drawable/message_right"
tools:ignore="RtlHardcoded">
<TextView
android:id="@+id/right_msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"/>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white"/>
<corners
android:radius="15dip"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white"/>
<corners
android:radius="15dip"/>
</shape>
package com.example;
//新建消息类 用content表示消息的内容,type表示消息的类型,消息类型有两个值,0代表收到的信息,1代表发送的信息
public class Msg {
public static final int TYPE_RECEIVED = 0;
public static final int TYPE_SENT = 1;
private final String content;//改为final
private final int type;//改为final
public Msg(String content,int type){
this.content = content;
this.type = type;
}
public String getContent(){
return content;
}
public int getType(){
return type;
}
}
package com.example.ict2021menu;
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 java.util.List;
public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
private final List<com.example.ict2021menu.Msg> mMsgList;//改为final
static class ViewHolder extends RecyclerView.ViewHolder{
LinearLayout leftLayout;
LinearLayout rightLayout;
TextView leftMsg;
TextView rightMsg;
public ViewHolder(View view){
super(view);
leftLayout = view.findViewById(R.id.left_layout);
rightLayout = view.findViewById(R.id.right_layout);
leftMsg = view.findViewById(R.id.left_msg);
rightMsg = view.findViewById(R.id.right_msg);
}
}
public MsgAdapter(List<com.example.ict2021menu.Msg> msgList){
mMsgList = msgList;
}
@NonNull
@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(@NonNull ViewHolder holder, int position){
com.example.ict2021menu.Msg msg = mMsgList.get(position);
if (msg.getType()== com.example.ict2021menu.Msg.TYPE_RECEIVED){
//如果是收到消息,则显示在左边,将右边布局隐藏
holder.leftLayout.setVisibility(View.VISIBLE);
holder.rightLayout.setVisibility(View.GONE);
holder.leftMsg.setText(msg.getContent());
}else if(msg.getType()== com.example.ict2021menu.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.example.ict2021menu;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.ict2021menu.utils.bafaCloudUtil;
import com.example.ict2021menu.utils.fileUtil;
import com.example.ict2021menu.utils.huaweiCloudUtil;
import java.io.IOException;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class voice_Activity extends AppCompatActivity {
private final List<Msg> msgList = home_Activity.msgList;//改为final
private EditText inputText;
private RecyclerView msgRecyclerView;
private MsgAdapter adapter;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice);
//初始化消息
initMegs();
//找到输入文本和发送按钮控件
inputText = (EditText)findViewById(R.id.input_text);
Button send = (Button) findViewById(R.id.send);
msgRecyclerView = (RecyclerView)findViewById(R.id.msg_recycle_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
msgRecyclerView.setLayoutManager(layoutManager);
adapter = new MsgAdapter(msgList);
msgRecyclerView.setAdapter(adapter);
/*
将语音识别结果内容以String传递到这
Msg msg4 = new Msg("String result",Msg.TYPE_RECEIVED);
msgList.add(msg4);
*/
send.setOnClickListener(v -> {
String content = inputText.getText().toString();
if(!"".equals(content)){
Msg msg = new Msg(content,Msg.TYPE_SENT);
msgList.add(msg);
//当有新消息,刷新RecyclerView的显示
adapter.notifyItemInserted(msgList.size() - 1);
//将RecyclerView定位到最后一行
msgRecyclerView.scrollToPosition(msgList.size() - 1);
//清空输入框内容
inputText.setText("");
};
doRe2Question(content);
// doRe2Bafa();
});
}
@RequiresApi(api = Build.VERSION_CODES.O)
public String QA(String question){
StringBuffer tip1=new StringBuffer("对于您的问题: ");
StringBuffer tip_question=new StringBuffer(question);
tip1.append(tip_question);
Msg msg_no = new Msg(tip1.toString(), Msg.TYPE_RECEIVED);
msgList.add(msg_no);
/*
问答处理
*/
Toast.makeText(getApplicationContext(),"正在处理中",Toast.LENGTH_SHORT).show();
String tip="没有找到您想要的答案,或许您可以换个说法再试试?";
return tip;
}
}
private void initMegs(){
Msg msg1 = new Msg("你好呀,我是你的智慧菜谱小助手",Msg.TYPE_RECEIVED);
msgList.add(msg1);
Msg msg2 = new Msg("Hi",Msg.TYPE_SENT);
msgList.add(msg2);
Msg msg3 = new Msg("有什么需要吗?,home页直接点击语音就可以跟我对话哦", Msg.TYPE_RECEIVED);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。