可借用代码部分:
1.页面跳转功能
?添加要跳转的页面java名。
?建一个要跳转的页面的java文件
写一个与之对应的layout
?主文件中先写入触发绑定的事件,如按到什么键。此程序是按到相应的菜单后跳转,
随后输入代码
Intent intent1 = new Intent();
intent1.setClass(MainActivity.this, Baojing.class);
startActivity(intent1);
Toast.makeText(MainActivity.this, "正在加载报警设定值 ", Toast.LENGTH_LONG).show();
Intent进行绑定,Baojing换成你要跳转的页面。
跳转页面的java文件写入该代码
?2.跳出弹窗消息代码
Toast.makeText(MainActivity.this, "正在加载报警设定值 ", Toast.LENGTH_LONG).show();
"可自己加"如果要在加上参数则可仿造下面这样
Toast.makeText(MainActivity.this,"最高湿度是:"+max2,Toast.LENGTH_LONG).show();
3.右上角菜单显示
第一步先定义
static final int MENU_01 = Menu.FIRST;
static final int MENU_02 = Menu.FIRST+1;
static final int MENU_03 = Menu.FIRST+2;
static final int MENU_04 = Menu.FIRST+3;
static final int SUB_MENU_A = Menu.FIRST+4;
static final int SUB_MENU_B = Menu.FIRST+5;
static final int SUB_MENU_C = Menu.FIRST+6;
static final int SUB_MENU_D = Menu.FIRST+7;
前面4个为一级菜单,后面A,B,C为2级菜单,
写入代码
public boolean onCreateOptionsMenu(Menu menu){
SubMenu uploadMenu=(SubMenu)menu.addSubMenu(0,MENU_01,1,"城市空气");
uploadMenu.setHeaderTitle("空气系统设置");
uploadMenu.add(0, SUB_MENU_A, 0, "PM2.5");
uploadMenu.add(0, SUB_MENU_B, 0, "甲醛");
SubMenu menu2 = (SubMenu)menu.addSubMenu(0,MENU_02,1,"城市温度");
menu2.setHeaderTitle("温度系统设置");
menu2.add(0, SUB_MENU_C, 0, "平均温度");
return true;
}
@Override
写完代码他只是空有其表,在加入代码,让他可以有对应的操作,
先在开头定义
TextView tv1;
之后
public boolean onOptionsItemSelected(MenuItem item){
tv1 = (TextView) findViewById(R.id.info);
switch (item.getItemId())
{
case SUB_MENU_A:
Intent intent4 = new Intent();
intent4.setClass(MainActivity.this, AirActivity.class);
startActivity(intent4);
return true;
case SUB_MENU_B:
return true;
case SUB_MENU_C:
int sum1 = 0;
for (int i = 0;i<7;i++){
sum1 = sum1+temperatureArray[i];
}
Toast.makeText(MainActivity.this,"平均温度是:"+sum1/7,Toast.LENGTH_LONG).show();
tv1.setText("平均温度是:"+sum1/7);
return true;
case SUB_MENU_D:
int min1 = temperatureArray[0];
for (int i = 1;i<7;i++){
if (min1>temperatureArray[i])
min1 = temperatureArray[i];
}
Toast.makeText(MainActivity.this,"最低温度是:"+min1,Toast.LENGTH_LONG).show();
tv1.setText("最低温度是:" + min1);
return true;
case SUB_MENU_E:
int max1 = temperatureArray[0];
for (int i = 1;i<7;i++){
if (max1<temperatureArray[i])
max1 = temperatureArray[i];
}
tv1.setText("最高温度是:"+max1);
if (max1>30)
Toast.makeText(MainActivity.this,"高温警报!!",Toast.LENGTH_LONG).show();
return true;
case SUB_MENU_F:
float sum2 = 0;
for (int i = 0;i<7;i++){
sum2 = sum2+humidityArray[i];
}
Toast.makeText(MainActivity.this,"平均湿度是:"+sum2/7,Toast.LENGTH_LONG).show();
tv1.setText("平均湿度是:"+sum2/7);
return true;
case SUB_MENU_G:
float min2 = humidityArray[0];
for (int i = 1;i<7;i++){
if (min2>humidityArray[i])
min2 = humidityArray[i];
}
Toast.makeText(MainActivity.this,"最低湿度是:"+min2,Toast.LENGTH_LONG).show();
tv1.setText("最低湿度是:"+min2);
return true;
case SUB_MENU_H:
float max2 = humidityArray[0];
for (int i = 1;i<7;i++){
if (max2<humidityArray[i])
max2 = humidityArray[i];
}
Toast.makeText(MainActivity.this,"最高湿度是:"+max2,Toast.LENGTH_LONG).show();
tv1.setText("最高湿度是:" + max2);
return true;
case SUB_MENU_I:
Intent intent1 = new Intent();
intent1.setClass(MainActivity.this, Baojing.class);
startActivity(intent1);
Toast.makeText(MainActivity.this, "正在加载报警设定值 ", Toast.LENGTH_LONG).show();
return true;
}
return true;
}
}
?来加入内容,每个菜单都有不同的功能。
4.单片机传到安卓软件的数据存储以及数组的使用与遍历
?前面有了数据的java文件后,我们需要在主函数里面定义
static final int temperatureArray[] = new int[7];
static final float humidityArray[] = new float[7];
在第一个函数的里面写入该代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] s1 = DateGenerater.getTemperatures().split(",");
for(int i = 0;i < 7;i++){
temperatureArray[i] = Integer.valueOf(s1[i].trim());
}
String[] s2 = DateGenerater.getHumiditys().split(",");
for (int i = 0;i < 7;i++){
humidityArray[i]= Float.valueOf(s2[i].trim());
}
}
意思是把DateGenerater.getTemperatures的这个数据变成String类型,split(“,”)是为了把逗号去掉,因为我们的java函数里面有逗号,需要运用函数去除,之后采用for循环进行遍历,trim是移除字符串两侧的空白字符或其他预定义字符。
Float.valueOf(s2[i].trim());意思是把s2【i】变量变成Float类型。这样? humidityArray[i]最终得到的是float类型的值。
前面的步骤完成后写入代码
case SUB_MENU_F:
float sum2 = 0;
for (int i = 0;i<7;i++){
sum2 = sum2+humidityArray[i];
}
Toast.makeText(MainActivity.this,"平均湿度是:"+sum2/7,Toast.LENGTH_LONG).show();
tv1.setText("平均湿度是:"+sum2/7);
return true;
效果图:
?
5.tv的显示
tv.setText("text");
视频展示:
全部代码
MainActivity:
package com.huangfushi.hfs192025214_shuzhicaidan2;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
static final int MENU_01 = Menu.FIRST;
static final int MENU_02 = Menu.FIRST+1;
static final int MENU_03 = Menu.FIRST+2;
static final int MENU_04 = Menu.FIRST+3;
static final int SUB_MENU_A = Menu.FIRST+4;
static final int SUB_MENU_B = Menu.FIRST+5;
static final int SUB_MENU_C = Menu.FIRST+6;
static final int SUB_MENU_D = Menu.FIRST+7;
static final int SUB_MENU_E = Menu.FIRST+8;
static final int SUB_MENU_F = Menu.FIRST+9;
static final int SUB_MENU_G = Menu.FIRST+10;
static final int SUB_MENU_H = Menu.FIRST+11;
static final int SUB_MENU_I = Menu.FIRST+12;
static final int temperatureArray[] = new int[7];
static final float humidityArray[] = new float[7];
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] s1 = DateGenerater.getTemperatures().split(",");
for(int i = 0;i < 7;i++){
temperatureArray[i] = Integer.valueOf(s1[i].trim());
}
String[] s2 = DateGenerater.getHumiditys().split(",");
for (int i = 0;i < 7;i++){
humidityArray[i]= Float.valueOf(s2[i].trim());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
SubMenu uploadMenu=(SubMenu)menu.addSubMenu(0,MENU_01,1,"城市空气");
uploadMenu.setHeaderTitle("空气系统设置");
uploadMenu.add(0, SUB_MENU_A, 0, "PM2.5");
uploadMenu.add(0, SUB_MENU_B, 0, "甲醛");
SubMenu menu2 = (SubMenu)menu.addSubMenu(0,MENU_02,1,"城市温度");
menu2.setHeaderTitle("温度系统设置");
menu2.add(0, SUB_MENU_C, 0, "平均温度");
menu2.add(0, SUB_MENU_D, 0, "最低温度");
menu2.add(0, SUB_MENU_E, 0, "最高温度");
SubMenu menu3 = (SubMenu)menu.addSubMenu(0,MENU_03,1,"城市湿度");
menu3.setHeaderTitle("湿度系统设置");
menu3.add(0, SUB_MENU_F, 0, "平均湿度");
menu3.add(0,SUB_MENU_G,0,"最低湿度");
menu3.add(0, SUB_MENU_H, 0, "最高湿度");
SubMenu menu4 = (SubMenu)menu.addSubMenu(0,MENU_04,1,"报警设置");
menu4.setHeaderTitle("报警系统设置");
menu4.add(0, SUB_MENU_I, 0, "参数阀值设置");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
tv1 = (TextView) findViewById(R.id.info);
switch (item.getItemId())
{
case SUB_MENU_A:
Intent intent4 = new Intent();
intent4.setClass(MainActivity.this, AirActivity.class);
startActivity(intent4);
return true;
case SUB_MENU_B:
return true;
case SUB_MENU_C:
int sum1 = 0;
for (int i = 0;i<7;i++){
sum1 = sum1+temperatureArray[i];
}
Toast.makeText(MainActivity.this,"平均温度是:"+sum1/7,Toast.LENGTH_LONG).show();
tv1.setText("平均温度是:"+sum1/7);
return true;
case SUB_MENU_D:
int min1 = temperatureArray[0];
for (int i = 1;i<7;i++){
if (min1>temperatureArray[i])
min1 = temperatureArray[i];
}
Toast.makeText(MainActivity.this,"最低温度是:"+min1,Toast.LENGTH_LONG).show();
tv1.setText("最低温度是:" + min1);
return true;
case SUB_MENU_E:
int max1 = temperatureArray[0];
for (int i = 1;i<7;i++){
if (max1<temperatureArray[i])
max1 = temperatureArray[i];
}
tv1.setText("最高温度是:"+max1);
if (max1>30)
Toast.makeText(MainActivity.this,"高温警报!!",Toast.LENGTH_LONG).show();
return true;
case SUB_MENU_F:
float sum2 = 0;
for (int i = 0;i<7;i++){
sum2 = sum2+humidityArray[i];
}
Toast.makeText(MainActivity.this,"平均湿度是:"+sum2/7,Toast.LENGTH_LONG).show();
tv1.setText("平均湿度是:"+sum2/7);
return true;
case SUB_MENU_G:
float min2 = humidityArray[0];
for (int i = 1;i<7;i++){
if (min2>humidityArray[i])
min2 = humidityArray[i];
}
Toast.makeText(MainActivity.this,"最低湿度是:"+min2,Toast.LENGTH_LONG).show();
tv1.setText("最低湿度是:"+min2);
return true;
case SUB_MENU_H:
float max2 = humidityArray[0];
for (int i = 1;i<7;i++){
if (max2<humidityArray[i])
max2 = humidityArray[i];
}
Toast.makeText(MainActivity.this,"最高湿度是:"+max2,Toast.LENGTH_LONG).show();
tv1.setText("最高湿度是:" + max2);
return true;
case SUB_MENU_I:
Intent intent1 = new Intent();
intent1.setClass(MainActivity.this, Baojing.class);
startActivity(intent1);
Toast.makeText(MainActivity.this, "正在加载报警设定值 ", Toast.LENGTH_LONG).show();
return true;
}
return true;
}
}
DateGenerater:
package com.huangfushi.hfs192025214_shuzhicaidan2;
public class DateGenerater {
public static String getpm(){
return "21,80,160";
}
public static String getTemperatures(){
return "21,22,19,28,29,32,35";
}
public static String getHumiditys() {
return "0.45, 0.23,0.58,0.32,0.44,0.56,0.48";
}
public static int gaowen=10;
}
Baojing;
package com.huangfushi.hfs192025214_shuzhicaidan2;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Baojing extends MainActivity {
@Override
protected void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
setContentView(R.layout.baojing);
Button btnButton = (Button) findViewById(R.id.btnBack);
btnButton.setOnClickListener(listener);//为Button设置监听事件
}
private View.OnClickListener listener = new View.OnClickListener() {//创建监听对象
@Override
public void onClick(View v) {
Intent intent2 = new Intent();// 创建Intent对象
setResult(1, intent2);//设置返回标识
finish();//关闭Activity
}
};
}
AirActivity:
package com.huangfushi.hfs192025214_shuzhicaidan2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class AirActivity extends Activity {
private TextView t1;
private TextView t2;
private TextView t3;
static final int pmArray[] = new int[3];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.air);
String[] s3 = DateGenerater.getpm().split(",");
for(int i = 0;i < 3;i++){
pmArray[i] = Integer.valueOf(s3[i].trim());
}
t1 = (TextView) findViewById(R.id.air_tv1);
t2 = (TextView) findViewById(R.id.air_tv2);
t3 = (TextView) findViewById(R.id.air_tv3);
setValue(t1, pmArray[0]);
setValue(t2, pmArray[1]);
setValue(t3, pmArray[2]);
int sum1 = 0;
sum1 = pmArray[1];
if (sum1<50)
Toast.makeText(AirActivity.this,"经检测PM2.5,空气质量优",Toast.LENGTH_LONG).show();
if(sum1>50&sum1<100)
Toast.makeText(AirActivity.this,"经检测PM2.5,空气质量良",Toast.LENGTH_LONG).show();
if(sum1>100&sum1<150)
Toast.makeText(AirActivity.this,"经检测PM2.5,达到轻度污染",Toast.LENGTH_LONG).show();
if(sum1>150)
Toast.makeText(AirActivity.this,"经检测PM2.5,达到重度污染",Toast.LENGTH_LONG).show();
}
private void setValue(TextView textView, int pm) {
textView. setText(pm+"ppm");
}
}
layout部分:
<?xml version="1.0" encoding="utf-8"?>
<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:background="@drawable/city2"
tools:context="com.huangfushi.hfs192025214_shuzhicaidan.MainActivity">
<TextView
android:id= "@+id/title"
android:layout_width="wrap_content"
android:layout_height= "wrap_content"
android:layout_centerHorizontal= "true"
android:layout_centerVertical= "true"
android:textSize="70px"
android:text= "物联网温度监测系统" />
<TextView
android:id= "@+id/info"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_centerHorizontal= "true"
android:layout_centerVertical= "true"
android:layout_below= "@id/title"
android:textSize="70px"
android:text= "监测到" />
</RelativeLayout>
air.xm
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity= "center"
android:orientation= "vertical"
android:layout_centerInParent= "true">
<RelativeLayout
android:id= "@+id/edit_div"
android:layout_marginLeft= "2dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id= "@+id/air_image1"
android:layout_width= "100dip"
android:layout_height= "80dip"
android:layout_alignParentLeft= "true"
android:layout_gravity= "center"
android:src= "@drawable/pm1" />
<ImageView
android:id= "@+id/air_image2"
android:layout_width= "100dip"
android:layout_height= "80dip"
android:layout_gravity= "center"
android:layout_marginLeft= "2dip"
android:layout_toRightOf="@+id/air_image1"
android:src= "@drawable/pm2" />
<ImageView
android:id= "@+id/air_image3"
android:layout_width= "100dip"
android:layout_height= "80dip"
android:layout_gravity= "center"
android:layout_marginLeft= "2dip"
android:layout_toRightOf="@+id/air_image2"
android:src= "@drawable/pm10" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/edit_div2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip">
<TextView
android:id="@+id/air_label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="15dip"
android:text="数值"
android:textColor= "#000000"
android:textSize= "12.0sp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="23dp" />
<TextView
android:id= "@+id/air_tv1"
android:layout_width= "55dp"
android:layout_height= "wrap_content"
android:layout_gravity="center"
android:text= "--ppm"
android:textColor="#000000"
android:textSize= "12.0sp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/air_label1" />
<TextView
android:id= "@+id/air_Label2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="15dip"
android:text="数值"
android:textColor= "#000000"
android:textSize="12.0sp"
android:layout_marginStart="24dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/air_tv1" />
<TextView
android:id= "@+id/air_tv2"
android:layout_width= "55dp"
android:layout_height= "wrap_content"
android:layout_gravity="center"
android:text= "--ppm"
android:textColor="#000000"
android:textSize= "12.0sp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/air_Label2" />
<TextView
android:id= "@+id/air_Label3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="15dip"
android:text="数值"
android:textColor= "#000000"
android:textSize="12.0sp"
android:layout_marginStart="19dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/air_tv2" />
<TextView
android:id= "@+id/air_tv3"
android:layout_width= "55dp"
android:layout_height= "wrap_content"
android:layout_gravity="center"
android:text= "--ppm"
android:textColor="#000000"
android:textSize= "12.0sp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/air_Label3" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
baojing.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">
<TextView
android:id= "@+id/t1"
android:layout_width="wrap_content"
android:layout_height= "wrap_content"
android:layout_centerHorizontal= "true"
android:layout_centerVertical= "true"
android:textSize="70px"
android:text= "PM2.5设置值" />
<EditText
android:id="@+id/name1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:textSize="24dp"
android:text="75"
android:layout_gravity="center_vertical"
/>
<TextView
android:id= "@+id/t2"
android:layout_width="wrap_content"
android:layout_height= "wrap_content"
android:layout_centerHorizontal= "true"
android:layout_centerVertical= "true"
android:textSize="70px"
android:text= "温度警报阈值" />
<EditText
android:id="@+id/name2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:textSize="24dp"
android:text="30"
android:layout_gravity="center_vertical"
/>
<TextView
android:id= "@+id/t3"
android:layout_width="wrap_content"
android:layout_height= "wrap_content"
android:layout_centerHorizontal= "true"
android:layout_centerVertical= "true"
android:textSize="70px"
android:text= "湿度警报阈值" />
<EditText
android:id="@+id/name3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:textSize="24dp"
android:text="30"
android:layout_gravity="center_vertical"
/>
<Button
android:layout_width="60dp"
android:layout_height="40dp"
android:id="@+id/btnBack"
android:text="确定设置"
android:layout_gravity="right" />
</LinearLayout>
主头文件写该代码。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.huangfushi.hfs192025214_shuzhicaidan2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="报警设置"
android:name=".Baojing" >
</activity>
<activity
android:label="泉州环境监测"
android:name=".AirActivity" >
</activity>
</application>
</manifest>
完整工程布局:
?
|