Android Studio智能聊天

1,布局activit_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/recycle">


    </androidx.recyclerview.widget.RecyclerView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/input"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/send"
            android:text="发送"/>
    </LinearLayout>



</LinearLayout>

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:padding="10dp"
    android:layout_height="wrap_content">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/left_layout"
        android:layout_gravity="left"
        android:background="@drawable/message_left">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:layout_marginTop="10dp"
            android:id="@+id/left_msg"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/right_layout"
        android:layout_gravity="right"
        android:layout_marginLeft="10dp"
        android:background="@drawable/message_right">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:layout_marginTop="10dp"
            android:id="@+id/right_msg"/>
    </LinearLayout>

3,创建类Msg获取数据


public class Msg {

    public static final int MSG_RECEIVED = 0;
    public static final int MSG_SEND =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;
    }
}

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


public class MsgAdapter extends RecyclerView.Adapter<MsgAdapter.ViewHolder> {
    private List<Msg> mMsgList;

    public class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout leftLayout;
        TextView leftMsg;
        LinearLayout rightLayout;
        TextView rightMsg;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            leftLayout=itemView.findViewById(R.id.left_layout);
            rightLayout=itemView.findViewById(R.id.right_layout);
            leftMsg=itemView.findViewById(R.id.left_msg);
            rightMsg=itemView.findViewById(R.id.right_msg);

        }
    }

    public MsgAdapter(List<Msg> msgList){
        mMsgList=msgList;
    }
    @NonNull
    @Override
    public MsgAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item,parent,false);
        ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MsgAdapter.ViewHolder holder, int position) {
        Msg msg=mMsgList.get(position);

        if (msg.getType()==Msg.MSG_RECEIVED){
            holder.leftLayout.setVisibility(View.VISIBLE);
            holder.rightLayout.setVisibility(View.GONE);
            holder.leftMsg.setText(msg.getContent());
        }else if (msg.getType()==Msg.MSG_SEND){
            holder.leftLayout.setVisibility(View.GONE);
            holder.rightLayout.setVisibility(View.VISIBLE);
            holder.rightMsg.setText(msg.getContent());

        }

    }

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


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

public class RobotManager {
    private static String Url="http://api.qingyunke.com/api.php?key=free&appid=0&msg=!!";

    public static String getUrl(String question){
        String real_Url=Url.replace("!!",question);
        return real_Url;
    }
}

 5,逻辑


public class MainActivity extends AppCompatActivity {
    private static String TAG="MainActivity";


    private List<Msg> msgList = new ArrayList<>();
    private EditText input;
    private RecyclerView recyclerView;
    private LinearLayoutManager manager;
    private Button button;
    private MsgAdapter adapter;
    private String input_text;
    private StringBuilder response;

    private Handler handler = new Handler() {
    @Override
        public void handleMessage(Message msg) {
            //获取解析数据,显示在Recycle中
            Bundle data = msg.getData();
            String result = data.getString("result");

            Msg msg_get = new Msg(result, Msg.MSG_RECEIVED);
            msgList.add(msg_get);
                
                //数据刷新
            adapter.notifyItemInserted(msgList.size() - 1);
            recyclerView.scrollToPosition(msgList.size() - 1);


        }


    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        initMsg();//初始化数据


        recyclerView = findViewById(R.id.recycle);
        button = findViewById(R.id.send);
        input = findViewById(R.id.input);

        manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        adapter = new MsgAdapter(msgList);
        recyclerView.setAdapter(adapter);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                input_text = input.getText().toString();
                Msg msg = new Msg(input_text, Msg.MSG_SEND);
                msgList.add(msg);

                adapter.notifyItemInserted(msgList.size() - 1);
                recyclerView.scrollToPosition(msgList.size() - 1);
                input.setText("");

                getInter();   //发起网络请求

            }

        });

    }


    private void getInter() {
        //开起线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                BufferedReader reader = null;
                try {
                    URL url = new URL(RobotManager.getUrl(input_text));
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setReadTimeout(8000);
                    connection.setConnectTimeout(8000);

                    InputStream in = connection.getInputStream();

                    reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder response = new StringBuilder();
                    String line = "";
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }

                    // 2,解析获得的数据
                    Gson gson=new Gson();
                    Msg msg=gson.fromJson(response.toString(),Msg.class);
                    Log.d(TAG, "result:" + msg.getType());
                    Log.d(TAG, "content:" + msg.getContent());


                    // 3,将解析的数据保存到 Message中,传递到主线程中显示
                    Bundle data=new Bundle();
                    Message msg1=new Message();
                    if (msg.getType()==0){
                        data.putString("result",msg.getContent());
                    }else {
                        data.putString("result","我不知道你在说什么!");
                    }
                    msg1.setData(data);
                    msg1.what=1;
                    handler.sendMessage(msg1);



                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (ProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (reader != null) {
                        try {
                            reader.close();

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (connection != null) {
                        connection.disconnect();
                    }
                }
            }


        }).start();
    }


    private void initMsg() {
        Msg msg = new Msg("我是菲菲,快来和我聊天吧!", Msg.MSG_RECEIVED);
        msgList.add(msg);
    }
}

 

 

 

 

 

 

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
### 回答1: Android Studio智能交通是一种基于Android Studio平台的智能交通管理系统。它可以通过使用各种传感器和设备来监测交通流量、车辆位置和速度等信息,从而提高交通效率和安全性。此外,它还可以通过使用数据分析和机器学习技术来预测交通拥堵和事故,以便及时采取措施来减少交通事故和拥堵。 ### 回答2: 智能交通系统是基于计算机技术和智能化算法来管理和优化城市交通系统的一种创新应用。Android Studio是一款集成开发环境,可用于开发Android操作系统上的应用程序。下面将详细介绍Android Studio智能交通领域的应用。 首先,Android Studio可以用于开发智能交通的移动应用程序。通过这些应用程序,用户可以实时获取交通拥堵情况、道路状况和实时导航等信息。这对规划出行路线、避开拥堵区域非常有帮助。 其次,Android Studio也可以用于开发智能交通监控系统。这些系统可以利用摄像头和传感器等设备监测道路上的交通状况,并通过图像处理和数据分析等技术判断交通流量和拥堵情况。借助Android Studio的开发,可以定制实时的交通数据分析应用程序。 此外,Android Studio还可以与智能灯光控制系统集成,实现智能交通信号控制。通过智能交通信号控制系统,交通信号灯可以根据实时交通流量和路况进行调控,优化交通流动,减少拥堵情况。 最后,Android Studio还可以用于智能公交站台和公交车辆管理系统的开发。借助移动应用程序,乘客可以轻松获取实时的公交车到达时间和公交线路信息。而在公交车辆管理系统方面,可以实现对公交车辆的实时监测和定位,更好地优化公交线路和服务。 综上所述,Android Studio智能交通领域的应用非常广泛。通过开发移动应用程序、监控系统、信号控制系统以及公交车辆管理系统和公交站台等,可以实现更智能、高效的城市交通管理和优化。 ### 回答3: 智能交通是利用现代信息技术对交通运输系统进行智能化管理和优化的一种方法。Android Studio 是一款集成开发环境,主要用于开发基于Android操作系统的应用程序。 Android Studio 可以为智能交通系统的开发提供一些便利。首先,它包含了丰富的工具和资源,提供了开发移动应用程序所需的各种组件和库。开发者可以利用这些工具和资源来开发移动端应用程序,用于智能交通系统中的信息显示、交通指引等功能。其次,Android Studio 提供了可视化界面设计工具,开发者可以通过拖拽、布局等简单操作来构建用户友好的应用界面,提高用户体验。此外,Android Studio 还支持调试和测试功能,以确保应用程序的稳定性和安全性。 针对智能交通系统,Android Studio 可以与其他传感器、设备连接,以获取实时的交通数据。开发者可以利用Android Studio 提供的网络编程技术,与智能设备或其他系统进行数据交换,实现车辆、交通信号灯等的实时控制和管理。同时,Android Studio 提供了丰富的开发文档和社区支持,开发者可以通过学习和交流来解决开发过程中的问题和挑战。 总之,Android Studio 可以为智能交通系统的开发提供方便和支持。它的丰富工具和资源、可视化界面设计、调试测试功能以及与其他设备的连接能力,都可以帮助开发者开发出高效、可靠的智能交通应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值