前言:智能农业这个项目可以说是大多都是我们自己完成的。是对于前一个多月Android学习的一个检验,同时也是对我们成绩的一个凭据。意义非常重大。

一、对于布局方面

下面我放两张例图,是我认为在布局上对我有难度的点


可以看到页面最下方的分别是首页,设置和帮助。而各自的页面上又有各自的内容,所以我们就要用到Fragment和ViewPager现结合,来实现页面的滑动和点击翻页。(由于布局很多,我就挑着些重要的说)
1.首先定义一个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"android:orientation="vertical"tools:context="com.example.myapplication15.classActivity"><android.support.v4.view.ViewPager
        android:id="@+id/class_viewpager"android:layout_width="match_parent"android:layout_height="570dp"></android.support.v4.view.ViewPager><LinearLayout
        android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@id/class_viewpager"android:orientation="horizontal"><LinearLayout
        android:id="@+id/main_ll"android:layout_width="0dp"android:layout_height="match_parent"android:orientation="vertical"android:layout_weight="1"><ImageView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/b1"android:layout_gravity="center"/><TextView
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="首页"android:textSize="15sp"android:layout_gravity="center"/></LinearLayout><LinearLayout
            android:id="@+id/set_ll"android:layout_width="0dp"android:layout_height="match_parent"android:orientation="vertical"android:layout_weight="1"><ImageView
                android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/b2"android:layout_gravity="center"/><TextView
                android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="设置"android:textSize="15sp"android:layout_gravity="center"/></LinearLayout><LinearLayout
            android:id="@+id/help_ll"android:layout_width="0dp"android:layout_height="match_parent"android:orientation="vertical"android:layout_weight="1"><ImageView
                android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/b3"android:layout_gravity="center"/><TextView
                android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="帮助"android:textSize="15sp"android:layout_gravity="center"/></LinearLayout></LinearLayout></RelativeLayout>

2.接下来就是上面三个Fragment页面用使用Fragment和ViewPager来实现页面的滑动和点击翻页,在这里面我们就是要对布局页面进行填充了。(我用首页来举例)

  1. 在布局文件里对首页的进行代码编程
<FrameLayout 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"tools:context="com.example.myapplication15.fragment.MainFragment"><!-- TODO: Update blank fragment layout --><LinearLayout
        android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextView
            android:layout_width="match_parent"android:layout_height="60dp"android:gravity="center"android:text="智能农业"android:textColor="#28c014"android:textSize="30sp" /><android.support.v4.view.ViewPager
            android:id="@+id/title_pic_viewpager"android:layout_width="match_parent"android:layout_height="150dp"></android.support.v4.view.ViewPager><ScrollView
            android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayout
                android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><RelativeLayout
                    android:layout_width="match_parent"android:layout_height="0dp"android:layout_margin="10dp"android:layout_weight="1"><com.makeramen.roundedimageview.RoundedImageView
                        android:id="@+id/co2_img"android:layout_width="150dp"android:layout_height="match_parent"android:layout_margin="10dp"android:src="@mipmap/co2"app:riv_oval="true" /><TextView
                        android:id="@+id/top_tv"android:layout_width="60dp"android:layout_height="20dp"android:layout_alignParentTop="true"android:layout_marginTop="20dp"android:layout_toEndOf="@+id/co2_img"android:layout_toRightOf="@+id/co2_img"android:text="CO2浓度"android:textColor="#000000" /><TextView
                        android:id="@+id/co2_tv1"android:layout_width="50dp"android:layout_height="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/co2_img"android:text="CO2:"android:textColor="#000000" /><TextView
                        android:id="@+id/co2_tv"android:layout_width="50dp"android:layout_height="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/co2_tv1"android:text="200"android:textColor="#ff44" /><TextView
                        android:id="@+id/bottom_tv"android:layout_width="50dp"android:layout_height="20dp"android:layout_alignLeft="@id/co2_tv1"android:layout_alignParentBottom="true"android:layout_marginBottom="20dp"android:layout_toRightOf="@id/co2_img"android:gravity="center"android:text="设定值"android:textColor="#44ffff" /><TextView
                        android:layout_width="100dp"android:layout_height="20dp"android:layout_alignParentBottom="true"android:layout_marginBottom="20dp"android:layout_toRightOf="@id/bottom_tv"android:text="500-1000" /><ImageView
                        android:layout_width="80dp"android:layout_height="30dp"android:layout_alignParentRight="true"android:src="@mipmap/p2" /><ImageView
                        android:id="@+id/co2_intent_img"android:layout_width="30dp"android:layout_height="40dp"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:src="@mipmap/right" /></RelativeLayout><RelativeLayout
                    android:layout_width="match_parent"android:layout_height="0dp"android:layout_margin="10dp"android:layout_weight="1"><com.makeramen.roundedimageview.RoundedImageView
                        android:id="@+id/guangzhao_img"android:layout_width="150dp"android:layout_height="match_parent"android:layout_margin="10dp"android:src="@mipmap/guangzhao"app:riv_oval="true" /><TextView
                        android:id="@+id/top1_tv"android:layout_width="60dp"android:layout_height="20dp"android:layout_alignParentTop="true"android:layout_marginTop="20dp"android:layout_toEndOf="@+id/guangzhao_img"android:layout_toRightOf="@+id/guangzhao_img"android:text="光照强度"android:textColor="#000000" /><TextView
                        android:id="@+id/shine_tv1"android:layout_width="50dp"android:layout_height="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/guangzhao_img"android:text="光照"android:textColor="#000000" /><TextView
                        android:id="@+id/shine_tv"android:layout_width="50dp"android:layout_height="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/shine_tv1"android:text="500"android:textColor="#ff44" /><TextView
                        android:id="@+id/bottom2_tv"android:layout_width="50dp"android:layout_height="20dp"android:layout_alignLeft="@id/shine_tv1"android:layout_alignParentBottom="true"android:layout_marginBottom="20dp"android:layout_toRightOf="@id/guangzhao_img"android:gravity="center"android:text="设定值"android:textColor="#44ffff" /><TextView
                        android:layout_width="100dp"android:layout_height="20dp"android:layout_alignParentBottom="true"android:layout_marginBottom="20dp"android:layout_toRightOf="@id/bottom2_tv"android:text="500-1000" /><ImageView
                        android:layout_width="80dp"android:layout_height="30dp"android:layout_alignParentRight="true"android:src="@mipmap/p2" /><ImageView
                        android:id="@+id/shine_intent_img"android:layout_width="30dp"android:layout_height="40dp"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:src="@mipmap/right" /></RelativeLayout><RelativeLayout
                    android:layout_width="match_parent"android:layout_height="0dp"android:layout_margin="10dp"android:layout_weight="1"><com.makeramen.roundedimageview.RoundedImageView
                        android:id="@+id/turang_img"android:layout_width="150dp"android:layout_height="match_parent"android:layout_margin="10dp"android:src="@mipmap/turang"app:riv_oval="true" /><TextView
                        android:id="@+id/top_tv3"android:layout_width="60dp"android:layout_height="20dp"android:layout_alignParentTop="true"android:layout_marginTop="20dp"android:layout_toEndOf="@+id/turang_img"android:layout_toRightOf="@+id/turang_img"android:text="土壤指数"android:textColor="#000000" /><TextView
                        android:id="@+id/turangwendu_tv1"android:layout_width="40dp"android:layout_height="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/turang_img"android:text="温度"android:textColor="#000000" /><TextView                        android:id="@+id/turangwendu_tv"android:layout_width="30dp"android:layout_height="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/turangwendu_tv1"android:text="35"android:textColor="#ff44" /><TextView
                        android:id="@+id/turangshidu_tv1"android:layout_width="40dp"android:layout_height="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/turangwendu_tv"android:text="湿度"android:textColor="#000000" /><TextView
                        android:id="@+id/turangshidu_tv"android:layout_width="30dp"android:layout_height="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/turangshidu_tv1"android:text="32"android:textColor="#ff44" /><TextView
                        android:id="@+id/bottom_tv3"android:layout_width="80dp"android:layout_height="20dp"android:layout_alignLeft="@id/turangwendu_tv1"android:layout_alignParentBottom="true"android:layout_marginBottom="10dp"android:layout_toRightOf="@id/turang_img"android:gravity="center"android:text="湿度设定值"android:textColor="#44ffff" /><TextView
                        android:layout_width="100dp"android:layout_height="20dp"android:layout_alignParentBottom="true"android:layout_marginBottom="10dp"android:layout_toRightOf="@id/bottom_tv3"android:text="0~90" /><ImageView
                        android:layout_width="80dp"android:layout_height="30dp"android:layout_alignParentRight="true"android:src="@mipmap/p2" /><ImageView
                        android:id="@+id/turang_intent_img"android:layout_width="30dp"android:layout_height="40dp"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:src="@mipmap/right" /></RelativeLayout><RelativeLayout
                    android:layout_width="match_parent"android:layout_height="0dp"android:layout_margin="10dp"android:layout_weight="1"><com.makeramen.roundedimageview.RoundedImageView
                        android:id="@+id/kongqi_img"android:layout_width="150dp"android:layout_height="match_parent"android:layout_margin="10dp"android:src="@mipmap/kongqi"app:riv_oval="true" /><TextView
                        android:id="@+id/top_tv4"android:layout_width="60dp"android:layout_height="20dp"android:layout_alignParentTop="true"android:layout_marginTop="20dp"android:layout_toEndOf="@+id/kongqi_img"android:layout_toRightOf="@+id/kongqi_img"android:text="空气指数"android:textColor="#000000" /><TextView
                        android:layout_width="50dp"android:layout_height="20dp"android:layout_marginTop="20dp"android:layout_toRightOf="@id/top_tv4"android:text="PM2.5"android:textColor="#ffc144" /><TextView
                        android:id="@+id/airwendu_tv1"android:layout_width="40dp"android:layout_height="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/kongqi_img"android:text="温度"android:textColor="#000000" /><TextView
                        android:id="@+id/airwendu_tv"android:layout_width="30dp"android:layout_height="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/airwendu_tv1"android:text="35"android:textColor="#ff44" /><TextView
                        android:id="@+id/airshidu_tv1"android:layout_width="40dp"android:layout_height="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/airwendu_tv"android:text="湿度"android:textColor="#000000" /><TextView
                        android:id="@+id/airshidu_tv"android:layout_width="30dp"android:layout_height="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/airshidu_tv1"android:text="32"android:textColor="#ff44" /><TextView
                        android:id="@+id/bottom_tv4"android:layout_width="80dp"android:layout_height="20dp"android:layout_alignLeft="@id/airwendu_tv1"android:layout_alignParentBottom="true"android:layout_marginBottom="10dp"android:layout_toRightOf="@id/kongqi_img"android:gravity="center"android:text="湿度设定值"android:textColor="#44ffff" /><TextView
                        android:layout_width="50dp"android:layout_height="20dp"android:layout_alignParentBottom="true"android:layout_marginBottom="10dp"android:layout_toRightOf="@id/bottom_tv4"android:text="0~90" /><ImageView
                        android:layout_width="80dp"android:layout_height="30dp"android:layout_alignParentRight="true"android:src="@mipmap/p2" /><ImageView
                        android:id="@+id/air_intent_img"android:layout_width="30dp"android:layout_height="40dp"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:src="@mipmap/right" /></RelativeLayout></LinearLayout></ScrollView></LinearLayout>
</FrameLayout>

注意点:这里的四张图片是圆形的,网上的方法有难的有简单的。我用的是首先在build.gradle(app)里导一个包就是compile 'com.makeramen:roundedimageview:2.2.1'
然后就是我上面代码写的把Image改成RoundedImageView,同时加上app:riv_oval="true"图片就变成圆形了。


  1. 新建一个Fragment类,命名为MainFragment,在里面进行定义,绑定ID等一系列操作
public class MainFragment extends Fragment implements View.OnClickListener {private ImageView co2Img;private ImageView shineImg;private ImageView turangImg;private ImageView airImg;

注意点:在首页的最上方有个三张图片的滑动效果,和我上面说的页面滑动是一个意思,用到Fragment和ViewPager。这里我就不多加赘述了


3.其他页面还有很多,我就不都一一详述了。总体概括起来就是设置页面,填充主体类,再进行各个页面之间的点击跳转。

二、控制沙盘和获取沙盘数据

由于我们需要控制和获取沙盘数据,而在这里我们有两个选择,一个是android-async-http和okhttp这两个方法。由于后者速度更迅速,用户可以节省很长时间,所以我们用okhttp。同时还要用到分别的接口文件,这个不用担心,老师会发给你

1.要用okhttp我们首先要在build.gradle(app)里导一个包implementation 'com.squareup.okhttp3:okhttp:3.10.0'然后下载一下。然后新建一个类,在里面来定义sendOkHttpRequest和postJSONByOkhttp两个方法,以便于接下来的使用

public class OkHttp {public static final MediaType mediaType = MediaType.parse("application/json;charset=UTF-8");private static final String BASE_URL = "http://" + MainActivity.URL + ":8890/type/jason/action/";private static OkHttpClient client = new OkHttpClient();public static void sendOkHttpRequest(String url, okhttp3.Callback callback) {Request request = new Request.Builder().url(getAbsoluteUrl(url)).build();client.newCall(request).enqueue(callback);}public static void postJSONByOkhttp(String url, String json, int number, Callback callback) {try {JSONObject jsonObject = new JSONObject();jsonObject.put(json, number);RequestBody requestBody = RequestBody.create(mediaType, jsonObject.toString());Request request = new Request.Builder().url(getAbsoluteUrl(url)).post(requestBody).build();client.newCall(request).enqueue(callback);} catch (JSONException e) {e.printStackTrace();}   }private static String getAbsoluteUrl(String relativeUrl) {return BASE_URL + relativeUrl;}
}

注意点:这里一定都是要导okhttp3下的包,不然会报错


3.接下来就是回到要获取数据的地方,也就是我们的MainFragment。由于使用对象是用户,所以我们要设置一个进度条。来显示进度

 private ProgressDialog progressDialog;
 /*** 设置进度条*/progressDialog = new ProgressDialog(getContext());progressDialog.setTitle("正在请求数据");progressDialog.setMessage("请稍后");progressDialog.setCancelable(false);progressDialog.show();

4.接着就是在里面调用OkHttp 类里的方法来进行控制和获取

private void getSensorInfoByOkHttp() {OkHttp.sendOkHttpRequest("getSensor", new Callback() {@Overridepublic void onFailure(Call call, IOException e) {progressDialog.dismiss();// Toast.makeText(IntelligentActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();}@Overridepublic void onResponse(Call call, Response response) throws IOException {progressDialog.dismiss();String result = response.body().string();try {JSONObject object = new JSONObject(result);int a1 = object.getInt("co2");int a2 = object.getInt("light");int a3 = object.getInt("soilTemperature");int a4 = object.getInt("soilHumidity");int a5 = object.getInt("airTemperature");int a6 = object.getInt("airHumidity");//更新UIrunOnUiThread(new Runnable() {@Overridepublic void run() {co2TV.setText(a1 + "");shineTV.setText(a2 + "");turangwenduTV.setText(a3 + "");turangshiduTV.setText(a4 + "");airwenduTV.setText(a5 + "");airshiduTV.setText(a6 + "");}});} catch (JSONException e) {e.printStackTrace();}}});

注意点:由于我们调试代码时,模拟器要和沙盘在同一个局域网里,所以要设置成一样。另外由于有时候会获取不到,所以我们会需要不停的开关,这就是有时候我们代码没错,沙盘却没反应的原因。


大体就是这样了,有些代码在GitHub里能找到,链接源在我上个博客里,希望能对你有所帮助,笔芯!

Android开发项目——智能农业(知识点整理回顾)相关推荐

  1. 安卓开发项目——智能农业

    智能农业的开发步骤 首先需要导一下数据包. compile 'com.makeramen:roundedimageview:2.2.1'--让图片变成圆形的效果compile 'com.loopj.a ...

  2. Android项目——智能农业

    总结一下这一周关于智能农业APP项目的学习. ####一.布局 APP最重要的就是与用户的交互界面,布局很重要.在智能农业APP这个项目中,布局十分简单,用户不需要花费多少时间便能熟练使用. #### ...

  3. 实战项目——智能农业沙盘

    智能农业的实现 前言:前面我们学习了一系列的Android基础知识点,而一些重要知识点将在我们的实战项目中灵活运用到. 关于智能农业 这是一款基于Android开发的农业管理系统,通过移动端(Andr ...

  4. Android开发工程师常见面试题整理

    主要分为以下几部分: (1)java面试题 (2)Android面试题 (3)高端技术面试题 (4)非技术性问题&HR问题汇总 1. java面试题 熟练掌握java是很关键的,大公司不仅仅要 ...

  5. flutter全屏时钟!为什么有人说Android开发不再吃香?吐血整理

    开头 互联网时代的到来,让我们获取知识变得更加简单,理论上讲只要你想学,便会有不尽的知识等你,只要方法得当,够努力,任何人都可以都有可能成为大牛. 自己在努力的基础上,还学习了一些高效的学习方法,让我 ...

  6. 【Android开发】计算机网络基础知识点,如何完成网络请求过程?

    (一)计算机网络基础知识:从一次完整的网络请求过程分析 (1)域名解析 1.1)域名与ip地址 (1)ip地址:ip地址是一个32位(4字节)的二进制数(IPV4),常见格式为:192.168.1.1 ...

  7. Google Android开发精华教程(apkbus整理)

    转载  Google Android开发精华教程 http://www.apkbus.com/android-13503-1-1.html

  8. android开发项目app实例!我在华为做Android外包的真实经历!深度好文

    都说Android最近行情不好,很多人都遇到瓶颈或放弃或转行.其实这种情况17年18年也是如此,相对比之下,个人认为今年比去年好多了,Android接下来将会走向复苏的春天. 自从Google开始推出 ...

  9. Android开发项目实战:实现折叠式布局,android组件化开发与sdk

    android:layout_width="match_parent" android:layout_height="200dp"> <androi ...

最新文章

  1. linux运维必学python吗_linux运维一定要学python吗?
  2. CodeForces - 1484D Playlist(循环链表+bfs)
  3. zabbix常用key和自定义key的讲解
  4. C/C++越来越不行了?让我们看看C++的未来趋势
  5. 【预测模型】Logistic 人口阻滞增长模型
  6. 【UML】聊聊系统建模
  7. 道路交通安全隐患排查的方法研究
  8. esp_image: Image length xxxx doesn‘t fit in partition length 1048576问题解决思路
  9. c51单片机音乐门铃C语言程序,89c51六首歌曲的音乐门铃程序
  10. Youtube视频传输规律
  11. ReadHub android版
  12. wp 主题,wp 主题大全,wp 主题模板
  13. 在Mac上怎么使用Charles进行抓包
  14. 数据库分库分表,分片配置轻松入门!
  15. Android 图形架构之一 ——概述
  16. RESIZE DATAFILE与ORA-03297
  17. 32位系统和64位系统的区别是什么
  18. alter database命令
  19. 表格对角线两边打字_表格对角线两边打字_表格斜线一分为二怎么打字(excel斜杠分割表格打字)...
  20. webrtc】windows 构建 :depot_tools 采坑

热门文章

  1. 2W字!详解20道Redis经典面试题!(珍藏版)二
  2. 算法进阶指南每日一题(碎碎念念)
  3. Arduino实现增量式旋转编码器(欧姆龙e6b2-cwz6c)程序编写
  4. 计算式二级python_python实现自动生成小学四则运算题目(软工第二次项目作业)...
  5. 屌炸天的内核来袭,史上最小chromium内核miniblink!
  6. 调用操作符和函数对象
  7. 导出功能: 导出为excel文档,后端做导出处理,js代码发送接口直接下载
  8. 地铁用户体验问题之自动售票机(上篇)
  9. 快速判断一个数是否是2的幂次方,若是,并判断出来是多少次方!
  10. Map大家族的那点事儿(一) Map