Home>

Today I saw a Turing robot written by ios,Go directly to the official website () and read the api access, too simple,Just a get request ~ So, I wrote an android version of the robot,No technical content,But it was fun ~ I just watched Qin Shimingyue last night,Hey, Xiaoyu, this is my robot pet ~

First show the effect map:

Here is the code snippet,Friends who want the source code can leave your email address in the message below

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 for displaying messages
     * /
    private listview mchatview;
    /**
     * Text field
     * /
    private edittext mmsg;
    /**
     * Store chat messages
     * /
    private list mdatas=new arraylist ();
    /**
     * Adapter
     * /
    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 href="http://home.cto.com/index.php?s=/space/" 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, "I'm a little girl,very happy to help"));
    }
    public void sendmessage (view view)
    {
        final string msg=mmsg.gettext (). tostring ();
        if (textutils.isempty (msg))
        {
            toast.maketext (this, "You have not filled in the information yet.
.. ", 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 ("");
        //close the soft keyboard
        inputmethodmanager imm=(inputmethodmanager) getsystemservice (context.input_method_service);
        //get an instance of inputmethodmanager
        if (imm.isactive ())
        {
            //if open
            imm.togglesoftinput (inputmethodmanager.show_implicit,                    inputmethodmanager.hide_not_always);
            //close the soft keyboard,The opening method is the same.
This method is to switch on and off
        }
        new thread ()
        {
            public void run ()
            {
                chatmessage from=null;
                try
                {
                    from=httputils.sendmsg (msg);
                } catch (exception e)
                {
                    from=new chatmessage (type.input, "The server is down.
.. ");
                }
                message message=message.obtain ();
                message.obj=from;
                mhandler.sendmessage (message);
            };
        } .start ();
    }
}

The above code is all the code to implement the Android chat robot,Take it directly to your favorite friends,If you find any problems during use, please feel free to contact me.

  • Previous Constructing regular expression validation using exec () method in JS
  • Next Detailed data sharing and communication between AngularJS controllers