亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android?Studio實現(xiàn)智能聊天

 更新時間:2022年07月20日 17:09:17   作者:醉流年  
這篇文章主要為大家詳細(xì)介紹了Android?Studio實現(xiàn)智能聊天,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android Studio實現(xiàn)智能聊天的具體代碼,供大家參考,具體內(nèi)容如下

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="發(fā)送"/>
? ? </LinearLayout>

</LinearLayout>

2、創(chuàng)建子布局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、創(chuàng)建類Msg獲取數(shù)據(jù)

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;
? ? }
}

4、創(chuàng)建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();
? ? }

5、創(chuàng)建 RobotManager類封裝網(wǎng)絡(luò),網(wǎng)絡(luò)地址:青云客,智能聊天機器人

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;
? ? }
}

6、邏輯

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) {
? ? ? ? ? ? //獲取解析數(shù)據(jù),顯示在Recycle中
? ? ? ? ? ? Bundle data = msg.getData();
? ? ? ? ? ? String result = data.getString("result");
?
? ? ? ? ? ? Msg msg_get = new Msg(result, Msg.MSG_RECEIVED);
? ? ? ? ? ? msgList.add(msg_get);
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? //數(shù)據(jù)刷新
? ? ? ? ? ? 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();//初始化數(shù)據(jù)
?
?
? ? ? ? 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(); ? //發(fā)起網(wǎng)絡(luò)請求
?
? ? ? ? ? ? }
?
? ? ? ? });
?
? ? }
?
?
? ? 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,解析獲得的數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? 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,將解析的數(shù)據(jù)保存到 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);
? ? }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android中的webview監(jiān)聽每次URL變化實例

    Android中的webview監(jiān)聽每次URL變化實例

    這篇文章主要介紹了Android中的webview監(jiān)聽每次URL變化實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • 微信公眾平臺開發(fā)入門教程(SAE方倍工作室)

    微信公眾平臺開發(fā)入門教程(SAE方倍工作室)

    在這篇微信公眾平臺開發(fā)教程中,我們假定你已經(jīng)有了PHP語言程序、MySQL數(shù)據(jù)庫、計算機網(wǎng)絡(luò)通訊、及HTTP/XML/CSS/JS等基礎(chǔ)
    2014-05-05
  • Android學(xué)習(xí)筆記之應(yīng)用單元測試實例分析

    Android學(xué)習(xí)筆記之應(yīng)用單元測試實例分析

    這篇文章主要介紹了Android學(xué)習(xí)筆記之應(yīng)用單元測試,結(jié)合實例形式較為詳細(xì)的分析了Android單元測試的實現(xiàn)原理與具體步驟,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-11-11
  • 基于Android6.0實現(xiàn)彈出Window提示框

    基于Android6.0實現(xiàn)彈出Window提示框

    這篇文章主要為大家詳細(xì)介紹了基于Android6.0實現(xiàn)彈出Window提示框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Android編程中Handler原理及用法實例分析

    Android編程中Handler原理及用法實例分析

    這篇文章主要介紹了Android編程中Handler用法,結(jié)合實例形式分析了Handler的功能,原理及使用技巧,需要的朋友可以參考下
    2016-01-01
  • android用PopWindow做下拉框?qū)嵗a

    android用PopWindow做下拉框?qū)嵗a

    本篇文章主要介紹了android用PopWindow做下拉框?qū)嵗a,具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01
  • Kotlin類型安全構(gòu)建器的一次運用記錄

    Kotlin類型安全構(gòu)建器的一次運用記錄

    這篇文章主要給大家介紹了關(guān)于Kotlin類型安全構(gòu)建器的一次運用,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Kotlin具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 解決Android webview設(shè)置cookie和cookie丟失的問題

    解決Android webview設(shè)置cookie和cookie丟失的問題

    這篇文章主要介紹了解決Android webview設(shè)置cookie和cookie丟失的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android編程之include文件的使用方法

    Android編程之include文件的使用方法

    這篇文章主要介紹了Android編程之include文件的使用方法,實例分析了Android使用include引入文件的技巧,有助于增加代碼的重用性,提高程序設(shè)計效率,需要的朋友可以參考下
    2016-01-01
  • Android?Scroller實現(xiàn)彈性滑動效果

    Android?Scroller實現(xiàn)彈性滑動效果

    這篇文章主要為大家詳細(xì)介紹了Android?Scroller實現(xiàn)彈性滑動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04

最新評論