体温登记app——进度8
今天将体温APP使用了数据库,即当点击提交按钮时可以将数据输入到数据库中
代码如下:
activity_main.xml(提交按钮)
1 <Button 2 android:id="@+id/btn_submit" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:text="提交" 6 android:layout_below="@id/temperature" 7 android:layout_marginTop="20dp" 8 android:background="#1E86FD" 9 />
Constants.java
1 public class Constants { 2 public static final String DATABASE_NAME ="TemperatureAPP.db"; 3 public static final int VERSION_CODE=1; 4 public static final String TABLE_NAME="temper"; 5 }
MyDatabaseHelper.java
1 import android.content.Context; 2 import android.database.sqlite.SQLiteDatabase; 3 import android.database.sqlite.SQLiteOpenHelper; 4 5 public class MyDatabaseHelper extends SQLiteOpenHelper { 6 7 private Context mContext; 8 public MyDatabaseHelper(Context context){ 9 super(context,Constants.DATABASE_NAME,null,Constants.VERSION_CODE); 10 mContext=context; 11 } 12 13 @Override 14 public void onCreate(SQLiteDatabase db){ 15 //创建时的回调 16 String sql="create table " + Constants.TABLE_NAME + "(name varchar,data varchar,time varchar,local varchar,temperature varchar)"; 17 db.execSQL(sql); 18 } 19 20 @Override 21 public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){ 22 //升级时的回调 23 } 24 25 }
Dao.java
1 import android.content.Context; 2 import android.database.sqlite.SQLiteDatabase; 3 4 public class Dao { 5 6 private final MyDatabaseHelper mHelper; 7 8 public Dao(Context context){ 9 //创建数据库 10 mHelper=new MyDatabaseHelper(context); 11 } 12 13 public void insert(String Name,String Data,String Time,String Local,String Tem){ 14 SQLiteDatabase db=mHelper.getWritableDatabase(); 15 String sql="insert into "+Constants.TABLE_NAME+"(name,data,time,local,temperature) values(?,?,?,?,?)"; 16 db.execSQL(sql,new Object[]{Name,Data,Time,Local,Tem}); 17 db.close(); 18 } 19 20 }
MainActivity.java
1 //提交按钮 2 private Button mBtnSubmit; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.activity_main); 7 //提交按钮 8 mBtnSubmit = findViewById(R.id.btn_submit); 9 mBtnSubmit.setOnClickListener(new View.OnClickListener() { 10 @Override 11 public void onClick(View v) { 12 final EditText nameET=findViewById(R.id.name);//获取姓名位置 13 final EditText dataET=findViewById(R.id.data);//获取日期位置 14 final EditText timeET=findViewById(R.id.time);//获取时间位置 15 final EditText localET=findViewById(R.id.local);//获取地点位置 16 final EditText temperatureET=findViewById(R.id.temperature);//获取体温位置 17 18 //存数据 19 mBtnSubmit.setOnClickListener(new View.OnClickListener() { 20 @Override 21 public void onClick(View v) { 22 23 String NameData=nameET.getText().toString();//获取姓名 24 String DateData=dataET.getText().toString();//获取日期 25 String TimeData=timeET.getText().toString();//获取时间 26 String LocalData=localET.getText().toString();//获取地点 27 String TemData=temperatureET.getText().toString();//获取体温 28 29 new persion().setName(NameData); 30 new persion().setDate(DateData); 31 new persion().setTime(TimeData); 32 new persion().setLocal(LocalData); 33 new persion().setTemperature(TemData); 34 35 //创建数据库 36 MyDatabaseHelper helper=new MyDatabaseHelper(MainActivity.this); 37 helper.getWritableDatabase(); 38 Dao dao=new Dao(MainActivity.this); 39 40 if(NameData.equals("")|| DateData.equals("")||TimeData.equals("")||LocalData.equals("")||TemData.equals("")){ 41 Toast.makeText(MainActivity.this,"请将信息填写完整",Toast.LENGTH_SHORT).show(); 42 }else{ 43 dao.insert(NameData,DateData,TimeData,LocalData,TemData); 44 Toast.makeText(MainActivity.this,"保存成功",Toast.LENGTH_SHORT).show(); 45 //跳转到Button的演示界面,即submitActivity 46 Toast.makeText(MainActivity.this,"提交成功",Toast.LENGTH_SHORT).show(); 47 Intent intent=null; 48 intent=new Intent(MainActivity.this,SubmitActivity.class);//SubmitActivity为新建的一个Activity 49 startActivity(intent); 50 } 51 } 52 }); 53 } 54 }); 55 56 }
activity_submit.xml
1 <TextView 2 android:id="@+id/success" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:text="提交成功" 6 android:textSize="25sp" 7 android:gravity="center" 8 android:textColor="#000000" 9 />
效果图:
数据库效果图: