首页 > 其他分享 >机智的图灵机器人

机智的图灵机器人

时间:2022-12-21 11:06:19浏览次数:40  
标签:layout 机器人 图灵 public content 机智 import android id


机智的图灵机器人

主启动页面

VideoListActivity.java

package com.wangdeqiang.www.machine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;

/**
* Created by Deligence on 2017/5/21.
* @author 王德强
* 功能:页面加载显示
*/

class VideoListActivity extends Activity{
private boolean isStartVideoList = false; //判断是否启动标志
private Handler handler = new Handler();
@Override
protected void onCreate( Bundle savedInstanceState) { //去除标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.videolist_activity);
//启动线程 让里面的方法运行2秒后关闭
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startVideList();//启动方法
}
}, 2000);
}

private void startVideList() {

if(!isStartVideoList){//判断是否第一次运行
isStartVideoList = true;
Intent intent = new Intent(this,MainActivity.class);//转到MainActivity页面
startActivity(intent);
//关闭当前的启动页面
finish();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//当触摸屏幕的时候 就会立马进入这个方法
startVideList();
return super.onTouchEvent(event);
}

@Override
protected void onDestroy() {
//把所有的消息和回调移除
handler.removeCallbacksAndMessages(null);
super.onDestroy();
}
}

布局文件
videolist_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/beijing"
>
<ImageView
android:id="@+id/iv_splash_icon"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:src="@mipmap/robot1" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv_splash_icon"
android:layout_centerInParent="true"
android:gravity="center_vertical"
android:orientation="horizontal">

<ProgressBar
android:layout_width="30dp"
android:layout_height="30dp" />

<TextView
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="程序正在玩命加载中..."
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>


</RelativeLayout>

界面如图所示

机智的图灵机器人_图灵机器人

MainActivity.java代码

package com.wangdeqiang.www.machine;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author 王德强
* @功能:图灵机器人数据收集与回复
*/

public class MainActivity extends AppCompatActivity implements View.OnClickListener, HttpGetData {

private HttpData httpData;
//创建List集合,存放数据
private List<ListData> list;
private ListView lv;
private Button send_btn;
private EditText sendtext;
private String content_str;
private AdapterText adapter;
private String [] welcomeArray; //界面欢迎语数组
//显示时间
private double currenttime,oldTime=0;


@Override
protected void onCreate(Bundle savedInstanceState) { //消除页面标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}

//初始化
private void initView(){
//实例化集合
list = new ArrayList<ListData>();
lv = (ListView) findViewById(R.id.lv);
send_btn= (Button) findViewById(R.id.send_btn);
sendtext = (EditText) findViewById(R.id.senText);
send_btn.setOnClickListener(this);//设置点击时间
adapter=new AdapterText(list,this);
lv.setAdapter(adapter);
ListData listData=null;
listData=new ListData(getRandomWelcomeTips(),listData.receiver,getTime());
list.add(listData);
}

@Override
public void onClick(View v) {
getTime();
content_str=sendtext.getText().toString();
//每次发送完数据都设为空
sendtext.setText("");
//解决数据不能有空格 回车 问题
String dropk=content_str.replace(" ","");
String droph=dropk.replace("\n","");
ListData listData = null;
listData = new ListData(content_str,listData.send,getTime());
Log.i("Tag","sfds"+listData);
list.add(listData);
//当数据过多的时候,移除数据
if(list.size()>30){
for(int i=0;i<list.size();i++){
list.remove(i);
}
}
adapter.notifyDataSetChanged();
//异步请求对象进行实例化
//第一个参数传递url,第二个数据传递this
httpData = (HttpData) new HttpData("http://www.tuling123.com/openapi/api?key=[图灵机器人API](www.tuling123.com)&info="+droph,this).execute();


}
//功能:将数据传递给其他页面进行使用
//回调的方式创建这个接口
public void getDataUrl(String data) {
//调用解析的方法
parseText(data);
}
//获得JSON中的键值对数据,解析数据
public void parseText(String str){
try {
//传递JSON数据
JSONObject jb=new JSONObject(str);
/*Log.i("Tag",jb.getString("code"));
Log.i("Tag",jb.getString("text"))*/
//对象实例化
ListData listData = null;
listData=new ListData(jb.getString("text"),listData.receiver,getTime());
//向集合中添加数据
list.add(listData);
adapter.notifyDataSetChanged();
} catch (Exception e) {
// TODO: handle exception
}
}
//获得随机的见面欢迎语
public String getRandomWelcomeTips() {
String welcome_tips=null;
welcomeArray=this.getResources().getStringArray(R.array.welcome_tips);
//获取ID
int index = (int)(Math.random()*(welcomeArray.length-1));
welcome_tips=welcomeArray[index];
return welcome_tips;
}
//获得现在时间
public String getTime() {
currenttime=System.currentTimeMillis();
//创建显示时间的格式
SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date curdata=new Date();
//把获取的时间给它
String str = format.format(curdata);
//判断是否超过5秒
if (currenttime-oldTime>=5*60*1000){
oldTime=currenttime;
return str;
}else {
return "";
}

}

}

activity_main.xml

<?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:layout_height="match_parent"
android:background="@drawable/beijing2"
>

<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:divider="@null"
android:listSelector="@android:color/transparent"
android:transcriptMode="alwaysScroll"></ListView>

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

<EditText
android:id="@+id/senText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/bg_toast"
/>

<Button
android:id="@+id/send_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/send" />

</LinearLayout>

</LinearLayout>

运行界面如图所示

机智的图灵机器人_android_02

HttpData.java

package com.wangdeqiang.www.machine;

import android.os.AsyncTask;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
* Created by Deligence on 2017/5/21.
* @author 王德强
* 功能:异步数据请求
*/

public class HttpData extends AsyncTask<String,Void,String>{
//通过HttpClient请求
private HttpClient httpClient;
//请求方式
private HttpGet httpGet;
private String url;
//获取请求的返回
private HttpResponse httpResponse;
private HttpEntity httpEntity;
private InputStream in;
private HttpGetData listener;
//指定一个构造方法请求url
public HttpData(String url, HttpGetData listener) {
this.url = url;
this.listener = listener;
}

@Override
protected String doInBackground(String... params) {
//实例化客户端
httpClient = new DefaultHttpClient();
//Get方式进行请求,传递一个具体请求的url
httpGet = new HttpGet(url);
try {
//通过客户端进行发送
httpResponse = httpClient.execute(httpGet);
/*通过Response获得数据 */ httpEntity=httpResponse.getEntity();
//转化成流文件
in=httpEntity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line=null;
//储存所有的数据
StringBuffer sb = new StringBuffer();
while ((line=br.readLine())!=null){
sb.append(line);
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
/*添加一个网络权限*/
}
//重载这个方法,通过这个方法获得数据
@Override
protected void onPostExecute(String s) {
//返回数据
listener.getDataUrl(s);
super.onPostExecute(s);
}

}

HttpGetData.interface

package com.wangdeqiang.www.machine;

/**
* Created by Deligence on 2017/5/21.
* @author 王德强
*/

public interface HttpGetData {
void getDataUrl(String Data);
}

ListData.java

package com.wangdeqiang.www.machine;

/**
* Created by Deligence on 2017/5/21.
* @author 王德强
* 功能:数据封装
*/

public class ListData {
private String content;
public static final int send = 1;
public static final int receiver = 2;
private int flag;
private String time;
//创建构造方法
public ListData(String content, int flag, String time) {
this.content = content;
this.flag = flag;
this.time = time;
}
//创建Getter和Setter
public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public static int getSend() {
return send;
}

public static int getReceiver() {
return receiver;
}

public int getFlag() {
return flag;
}

public void setFlag(int flag) {
this.flag = flag;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}
}

AdapterText.java

package com.wangdeqiang.www.machine;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

/**
* Created by Deligence on 2017/5/21.
* @author 王德强
* 功能:自定义ListView的适配器
*/

public class AdapterText extends BaseAdapter{
private List<ListData> lists;
private Context mContext;
private RelativeLayout layout;

public AdapterText(List<ListData> lists, Context mContext) {
this.lists = lists;
this.mContext = mContext;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater =LayoutInflater.from(mContext);
if(lists.get(position).getFlag()==ListData.receiver){
layout= (RelativeLayout) inflater.inflate(R.layout.leftitem,null);
}
if (lists.get(position).getFlag()==ListData.send)
{
layout = (RelativeLayout) inflater.inflate(R.layout.rightitem,null);
}
TextView tv = (TextView) layout.findViewById(R.id.tv);
TextView time = (TextView) layout.findViewById(R.id.time);
time.setText(lists.get(position).getTime());
tv.setText(lists.get(position).getContent());

return layout;
}
}

leftitem.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" />

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/time"
android:layout_marginLeft="45dp"
android:layout_marginTop="55dp"
android:gravity="center"
android:background="@drawable/aio_friend_bg_nor_11"
/>

<com.makeramen.roundedimageview.RoundedImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:gravity="center_horizontal"
android:src="@drawable/picture1"
app:riv_border_color="#333333"
app:riv_border_width="3dip"
app:riv_corner_radius="10dip"
app:riv_mutate_background="true"
app:riv_oval="true" />
</RelativeLayout>

运行界面如图所示

机智的图灵机器人_图灵机器人_03

rightitem.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:id="@+id/time"

/>

<TextView
android:layout_below="@id/iv"
android:layout_marginLeft="55dp"
android:layout_toLeftOf="@id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"
android:background="@drawable/aio_user_bg_nor_11"
android:gravity="center"
/>

<com.makeramen.roundedimageview.RoundedImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/iv"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/time"
app:riv_border_color="#333333"
app:riv_border_width="3dip"
app:riv_corner_radius="10dip"
app:riv_mutate_background="true"
android:src="@drawable/pic1"
app:riv_oval="true" />

</RelativeLayout>

运行界面如图所示

机智的图灵机器人_android_04


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wangdeqiang.www.machine">
<!-- 打开网络连接 -->
<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/robot"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".VideoListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
</application>

</manifest>


标签:layout,机器人,图灵,public,content,机智,import,android,id
From: https://blog.51cto.com/u_12606187/5958618

相关文章