欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

安卓(Android)聊天機(jī)器人實(shí)現(xiàn)代碼分享

 更新時(shí)間:2015年11月04日 10:45:22   投稿:mrr  
這是一個(gè)安卓智能聊天機(jī)器人的源碼,采用了仿微信的風(fēng)格設(shè)計(jì),調(diào)用的是圖靈機(jī)器人的API,能夠?qū)崿F(xiàn)智能聊天、講故事、講笑話、查天氣、查公交等豐富的功能

今天看到一個(gè)ios寫的圖靈機(jī)器人,直接去官網(wǎng)(http://www.tuling123.com/openapi/)看了下API接入,太簡(jiǎn)單了,就一個(gè)get請(qǐng)求~于是乎,寫了一個(gè)Android版本的機(jī)器人,沒什么技術(shù)含量,但是挺好玩的~剛好昨晚看了自己喜歡的秦時(shí)明月,嘿嘿,小貔貅,就是我的機(jī)器人寵物啦~

這是一個(gè)安卓智能聊天機(jī)器人的源碼,采用了仿微信的風(fēng)格設(shè)計(jì),調(diào)用的是圖靈機(jī)器人的API,能夠?qū)崿F(xiàn)智能聊天、講故事、講笑話、查天氣、查公交等豐富的功能。

先給大家展示效果圖:

下面是代碼片段,想要源碼的小伙伴可在下面留言留下你的郵箱地址

package com.example.android_robot_;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.example.android_robot_.bean.ChatMessage;
import com.example.android_robot_.bean.ChatMessage.Type;
import com.zhy.utils.HttpUtils;
public class MainActivity extends Activity
{
    /**
     * 展示消息的listview
     */
    private ListView mChatView;
    /**
     * 文本域
     */
    private EditText mMsg;
    /**
     * 存儲(chǔ)聊天消息
     */
    private List mDatas = new ArrayList();
    /**
     * 適配器
     */
    private ChatMessageAdapter mAdapter;
    private Handler mHandler = new Handler()
    {
        public void handleMessage(android.os.Message msg)
        {
            ChatMessage from = (ChatMessage) msg.obj;
            mDatas.add(from);
            mAdapter.notifyDataSetChanged();
            mChatView.setSelection(mDatas.size() - );
        };
    };
    <a  target="_blank">@Override</a>
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main_chatting);
        initView();
        mAdapter = new ChatMessageAdapter(this, mDatas);
        mChatView.setAdapter(mAdapter);
    }
    private void initView()
    {
        mChatView = (ListView) findViewById(R.id.id_chat_listView);
        mMsg = (EditText) findViewById(R.id.id_chat_msg);
        mDatas.add(new ChatMessage(Type.INPUT, "我是小貅貅,很高興為您服務(wù)"));
    }
    public void sendMessage(View view)
    {
        final String msg = mMsg.getText().toString();
        if (TextUtils.isEmpty(msg))
        {
            Toast.makeText(this, "您還沒有填寫信息呢...", Toast.LENGTH_SHORT).show();
            return;
        }
        ChatMessage to = new ChatMessage(Type.OUTPUT, msg);
        to.setDate(new Date());
        mDatas.add(to);
        mAdapter.notifyDataSetChanged();
        mChatView.setSelection(mDatas.size() - );
        mMsg.setText("");
        // 關(guān)閉軟鍵盤
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        // 得到InputMethodManager的實(shí)例
        if (imm.isActive())
        {
            // 如果開啟
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT,
                    InputMethodManager.HIDE_NOT_ALWAYS);
            // 關(guān)閉軟鍵盤,開啟方法相同,這個(gè)方法是切換開啟與關(guān)閉狀態(tài)的
        }
        new Thread()
        {
            public void run()
            {
                ChatMessage from = null;
                try
                {
                    from = HttpUtils.sendMsg(msg);
                } catch (Exception e)
                {
                    from = new ChatMessage(Type.INPUT, "服務(wù)器掛了呢...");
                }
                Message message = Message.obtain();
                message.obj = from;
                mHandler.sendMessage(message);
            };
        }.start();
    }
}

以上代碼就是實(shí)現(xiàn)安卓聊天機(jī)器人的全部代碼,喜歡的朋友直接拿去用,在使用過程中發(fā)現(xiàn)有問題請(qǐng)隨時(shí)和我聯(lián)系。

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論