Android机器人对话实现

一、主要内容:
(1)使用图灵机器人对话

http://www.tuling123.com/
注册后,选用自定义,新建机器人。

将第三方jar包引入AndroidStudio

    · 将jar文件复制、粘贴到app的libs目录中

    · 右键点击jar文件,并点击弹出菜单中的“Add As Library”,将jar文件作为类库添加到项目中;

    · 选择指定的类库。

    注:如果不执行23步,jar文件将不起作用,并且不能使用import语句引用。

将第三方so文件引入AndroidStudio


   · 在“src/main”目录中新建名为“jniLibs”的目录;

   · 将so文件复制、粘贴到“jniLibs”目录内。

相关文章链接:
http://jp1017.github.io/2015/12/20/Android-Studio%E4%B8%8B%E6%B7%BB%E5%8A%A0%E5%BC%95%E7%94%A8jar%E6%96%87%E4%BB%B6%E5%92%8Cso%E6%96%87%E4%BB%B6/

(2)ListView中item不同布局的实现

二、左侧布局文件


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/id_form_msg_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#bebebe"
        android:text="2012-12-12 12:12:12"
        android:textColor="#f5f5f5"
        android:textSize="12sp" />

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

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="49dp"
                android:layout_height="49dp"
                android:src="@drawable/icon" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="SKY_L"
                android:textSize="18sp" >
            TextView>
        LinearLayout>

        <TextView
            android:id="@+id/id_from_msg_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/chatfrom_bg_normal"
            android:gravity="center_vertical"
            android:textSize="16sp"
            android:text="你好" />
    LinearLayout>

LinearLayout>

右侧布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/id_to_msg_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#bebebe"
        android:text="2012-12-12 12:12:12"
        android:textColor="#f5f5f5"
        android:textSize="12sp" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:orientation="horizontal" >

         <TextView
             android:id="@+id/id_to_msg_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/chatto_bg_normal"
            android:gravity="center_vertical"
              android:textSize="16sp"
            android:text="你好" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ImageView
                android:layout_width="49dp"
                android:layout_height="49dp"
                android:src="@drawable/me" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="SKY_L"
                android:textSize="18sp" >
            TextView>
        LinearLayout>


    LinearLayout>

LinearLayout>

activity布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/id_ly_top"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_alignParentTop="true"
        android:background="@drawable/title_bar" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="小慕"
            android:textColor="#ffffff"
            android:textSize="22sp" />
    RelativeLayout>

    <RelativeLayout
        android:id="@+id/id_ly_bottom"
        android:layout_width="fill_parent"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true"
        android:background="@drawable/bottom_bar" >

        <Button
            android:id="@+id/id_send_msg"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@drawable/send_btn_bg"
            android:text="发送" />

        <EditText
            android:id="@+id/id_input_msg"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@id/id_send_msg"
            android:background="@drawable/login_edit_normal"
            android:textSize="18sp" />
    RelativeLayout>

    <ListView
        android:id="@+id/id_listview_msgs"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/id_ly_bottom"
        android:layout_below="@id/id_ly_top"
        android:divider="@null"
        android:dividerHeight="5dp" >
    ListView>

RelativeLayout>

适配Adapter

public class ChatMessageAdapter extends BaseAdapter
{
    private LayoutInflater mInflater;
    private List mDatas;

    public ChatMessageAdapter(Context context, List mDatas)
    {
        mInflater = LayoutInflater.from(context);
        this.mDatas = mDatas;
    }

    @Override
    public int getCount()
    {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public int getItemViewType(int position)
    {
        ChatMessage chatMessage = mDatas.get(position);
        if (chatMessage.getType() == Type.INCOMING)
        {
            return 0;
        }
        return 1;
    }

    @Override
    public int getViewTypeCount()
    {
        return 2;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ChatMessage chatMessage = mDatas.get(position);
        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            // ͨ¹ýItemTypeÉèÖò»Í¬µÄ²¼¾Ö
            if (getItemViewType(position) == 0)
            {
                convertView = mInflater.inflate(R.layout.item_from_msg, parent,
                        false);
                viewHolder = new ViewHolder();
                viewHolder.mDate = (TextView) convertView
                        .findViewById(R.id.id_form_msg_date);
                viewHolder.mMsg = (TextView) convertView
                        .findViewById(R.id.id_from_msg_info);
            } else
            {
                convertView = mInflater.inflate(R.layout.item_to_msg, parent,
                        false);
                viewHolder = new ViewHolder();
                viewHolder.mDate = (TextView) convertView
                        .findViewById(R.id.id_to_msg_date);
                viewHolder.mMsg = (TextView) convertView
                        .findViewById(R.id.id_to_msg_info);
            }
            convertView.setTag(viewHolder);
        } else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // ÉèÖÃÊý¾Ý
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        viewHolder.mDate.setText(df.format(chatMessage.getDate()));
        viewHolder.mMsg.setText(chatMessage.getMsg());
        return convertView;
    }

    private final class ViewHolder
    {
        TextView mDate;
        TextView mMsg;
    }

}
public class MainActivity extends Activity
{

    private ListView mMsgs;
    private ChatMessageAdapter mAdapter;
    private List mDatas;

    private EditText mInputMsg;
    private Button mSendMsg;

    private Handler mHandler = new Handler()
    {
        public void handleMessage(android.os.Message msg)
        {
            // µÈ´ý½ÓÊÕ£¬×ÓÏß³ÌÍê³ÉÊý¾ÝµÄ·µ»Ø
            ChatMessage fromMessge = (ChatMessage) msg.obj;
            mDatas.add(fromMessge);
            mAdapter.notifyDataSetChanged();
            mMsgs.setSelection(mDatas.size()-1);
        };

    };

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

        initView();
        initDatas();
        // ³õʼ»¯Ê¼þ
        initListener();
    }

    private void initListener()
    {
        mSendMsg.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                final String toMsg = mInputMsg.getText().toString();
                if (TextUtils.isEmpty(toMsg))
                {
                    Toast.makeText(MainActivity.this, "·¢ËÍÏûÏ¢²»ÄÜΪ¿Õ£¡",
                            Toast.LENGTH_SHORT).show();
                    return;
                }

                ChatMessage toMessage = new ChatMessage();
                toMessage.setDate(new Date());
                toMessage.setMsg(toMsg);
                toMessage.setType(Type.OUTCOMING);
                mDatas.add(toMessage);
                mAdapter.notifyDataSetChanged();
                mMsgs.setSelection(mDatas.size()-1);

                mInputMsg.setText("");

                new Thread()
                {
                    public void run()
                    {
                        ChatMessage fromMessage = HttpUtils.sendMessage(toMsg);
                        Message m = Message.obtain();
                        m.obj = fromMessage;
                        mHandler.sendMessage(m);
                    };
                }.start();

            }
        });
    }

    private void initDatas()
    {
        mDatas = new ArrayList();
        mDatas.add(new ChatMessage("ÄãºÃ£¬Ð¡Ä½ÎªÄú·þÎñ", Type.INCOMING, new Date()));
        mAdapter = new ChatMessageAdapter(this, mDatas);
        mMsgs.setAdapter(mAdapter);
    }

    private void initView()
    {
        mMsgs = (ListView) findViewById(R.id.id_listview_msgs);
        mInputMsg = (EditText) findViewById(R.id.id_input_msg);
        mSendMsg = (Button) findViewById(R.id.id_send_msg);
    }

}
注:
Error:java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException:
可能是因为.9图不严格。可以在drawable查看图片,然后show bad patch

你可能感兴趣的:(Android进阶)