App截图;
设计思路:在日期和事件的填写上使用日期和事件选择器,为提交按钮绑定检验数据是否合格的事件。数据的存储使用SQLite数据库,保存在date目录中,触发查看按钮转发到另一个activity,从数据库读取数据保存在Cursor中,循环光标为线性布局管理器添加文本组 件,在文本组件下添加删除按钮,通过时间进行删除操作,在actionbar中添加菜单,选项为搜索日期和清空数据,搜索日期同样选择日期选择器,除此之外为查看数据的activity指定父activity也就是等级数据的activity,一遍在activitybar中添加返回上一级的箭头。
源代码:
MainActivity:
package com.example.temperatureapp; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private SQLhelper sqLhelper; private SQLiteDatabase sqliteDatabase; @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0x11 && resultCode == 0x11) { Bundle bundle = data.getExtras(); String time = bundle.getString("time"); EditText time1 = findViewById(R.id.time); time1.setText(time); } else if (requestCode == 0x22 && resultCode == 0x22) { Bundle bundle = data.getExtras(); String time = bundle.getString("time"); EditText time1 = findViewById(R.id.time2); time1.setText(time); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sqLhelper=new SQLhelper(MainActivity.this,"Temperature",null,1); ActionBar actionBar=getSupportActionBar(); actionBar.hide(); setContentView(R.layout.activity_main); final EditText time2=findViewById(R.id.time2); final EditText time=findViewById(R.id.time); final EditText Temp=findViewById(R.id.Temp); final TextView button=findViewById(R.id.addButton); final TextView lookbutton=findViewById(R.id.lookButton); time.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus){ Intent intent=new Intent(MainActivity.this,getData_activity.class); startActivityForResult(intent,0x11); } } }); time2.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus){ Intent intent=new Intent(MainActivity.this,getTime_Activity.class); startActivityForResult(intent,0x22); } } }); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (TextUtils.isEmpty(Temp.getText().toString().trim()) || TextUtils.isEmpty(time.getText().toString().trim())) { Toast.makeText(MainActivity.this, "请填写完整", Toast.LENGTH_SHORT).show(); } else { sqliteDatabase = sqLhelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("Temper", Temp.getText().toString()); values.put("time", time.getText().toString()); values.put("time2", time2.getText().toString()); sqliteDatabase.insert("temperature", null, values); sqliteDatabase.close(); Temp.setText(""); time.setText(""); time2.setText(""); } /* String sql="insert into tb_Temp1(Temper,time) values("+Temp.getText().toString()+","+time.getText().toString()+")"; sqliteDatabase.execSQL(sql); Cursor c = sqliteDatabase.rawQuery("select * from tb_Temp where Temper=?",new String[]{"36.352"}); c.moveToNext(); Toast.makeText(MainActivity.this, c.getString(1), Toast.LENGTH_SHORT).show(); */ } }); lookbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,Main2Activity.class); startActivity(intent); } }); } @Override protected void onDestroy() { super.onDestroy(); if(sqLhelper!=null) { sqLhelper.close(); } } }
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="100dp" android:paddingTop="200dp" android:background="@drawable/by" tools:context=".MainActivity" > <EditText android:id="@+id/Temp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:hint="温度:(例如36.5)" android:importantForAutofill="no" /> <EditText android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:layout_below="@+id/Temp" android:hint="选择日期" /> <EditText android:id="@+id/time2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:layout_below="@+id/time" android:hint="选择时间" /> <TextView android:id="@+id/addButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/time2" android:paddingTop="10dp" android:textSize="20dp" android:layout_centerHorizontal="true" android:hint="提交" /> <TextView android:id="@+id/lookButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/addButton" android:paddingTop="10dp" android:textSize="20dp" android:layout_centerHorizontal="true" android:hint="查看数据" /> </RelativeLayout>
Main2Activity:
package com.example.temperatureapp; import androidx.annotation.ColorRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.NavUtils; import androidx.core.view.MenuItemCompat; import androidx.fragment.app.Fragment; import android.annotation.SuppressLint; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Color; import android.os.Bundle; import android.view.ContextMenu; import android.view.Gravity; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.LinearLayout; import android.widget.SearchView; import android.widget.TextView; import android.widget.Toast; public class Main2Activity extends AppCompatActivity { private SQLhelper sqLhelper; private SQLiteDatabase sqLiteDatabase; Cursor c=null; private String TEMP=null; @SuppressLint("RestrictedApi") protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0x11 && resultCode == 0x11) { Bundle bundle = data.getExtras(); String time = bundle.getString("time"); LinearLayout linearLayout=findViewById(R.id.linear1); linearLayout.removeAllViews(); setview(time); } } @SuppressLint("RestrictedApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); ActionBar actionBar=getSupportActionBar(); if (NavUtils.getParentActivityName(Main2Activity.this) != null) { actionBar.setDisplayShowTitleEnabled(false); actionBar.setDefaultDisplayHomeAsUpEnabled(true); } setview(); } public void setview() { String TEMP=null; final LinearLayout linearLayout=findViewById(R.id.linear1); sqLhelper = new SQLhelper(Main2Activity.this, "Temperature", null, 1); sqLiteDatabase = sqLhelper.getReadableDatabase(); c = sqLiteDatabase.rawQuery("select * from temperature order by Time desc", null); if(c.moveToNext()) { c.moveToFirst(); do { TEMP= "温度:" + c.getString(c.getColumnIndex("Temper")) + "℃" + " " + "时间:" + c.getString(c.getColumnIndex("Time"))+","+ c.getString(c.getColumnIndex("Time2")); final TextView textView = new TextView(this); textView.setText(TEMP); textView.setTextSize(30); textView.setPadding(0, 0, 0, 10); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); textView.setLayoutParams(params); linearLayout.addView(textView); TextView textView1 = new TextView(this); textView1.setText("删除"); textView1.setTextSize(20); textView1.setPadding(0, 0, 0, 10); params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); textView1.setLayoutParams(params); textView1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { delete(textView.getText().toString().split("\,")[1], linearLayout); } }); linearLayout.addView(textView1); } while (c.moveToNext()); }else{ final TextView textView = new TextView(this); textView.setText("目前没有添加体温记录!"); textView.setTextColor(0xffff0000); textView.setTextSize(19); textView.setPadding(0, 0, 0, 10); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); textView.setLayoutParams(params); linearLayout.addView(textView); } } public void setview(String a) { final LinearLayout linearLayout=findViewById(R.id.linear1); TextView text = new TextView(this); text.setText("搜索结果:"); text.setTextSize(20); text.setPadding(0, 0, 0, 10); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); text.setLayoutParams(params); linearLayout.addView(text); sqLhelper = new SQLhelper(Main2Activity.this, "Temperature", null, 1); sqLiteDatabase = sqLhelper.getReadableDatabase(); c = sqLiteDatabase.rawQuery("select * from temperature where Time='"+a+"' order by Time desc",null); if(c.moveToNext()) { c.moveToFirst(); do { TEMP= "温度:" + c.getString(c.getColumnIndex("Temper")) + "℃" + " " + "时间:" + c.getString(c.getColumnIndex("Time"))+","+ c.getString(c.getColumnIndex("Time2")); final TextView textView = new TextView(this); textView.setText(TEMP); textView.setTextSize(30); textView.setPadding(0, 0, 0, 10); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); textView.setLayoutParams(param); linearLayout.addView(textView); TextView textView1 = new TextView(this); textView1.setText("删除"); textView1.setTextSize(20); textView1.setPadding(0, 0, 0, 10); params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); textView1.setLayoutParams(params); textView1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { delete(textView.getText().toString().split("\,")[1], linearLayout); } }); linearLayout.addView(textView1); } while (c.moveToNext()); }else{ final TextView textView = new TextView(this); textView.setText("目前没有添加体温记录!"); textView.setTextColor(0xffff0000); textView.setTextSize(19); textView.setPadding(0, 0, 0, 10); LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); textView.setLayoutParams(p); linearLayout.addView(textView); } } public void delete(final String a, LinearLayout b) { AlertDialog alertDialog=new AlertDialog.Builder(Main2Activity.this).create(); alertDialog.setTitle("提示"); alertDialog.setMessage("是否真的删除数据?"); alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { sqLhelper.getWritableDatabase().execSQL("delete from temperature where Time2='"+a+"'"); Toast.makeText(Main2Activity.this,"删除成功",Toast.LENGTH_SHORT).show(); finish(); Intent intent = new Intent(Main2Activity.this, Main2Activity.class); startActivity(intent); } }); alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); alertDialog.show(); } public void deleteall() { LinearLayout b=findViewById(R.id.linear1); AlertDialog alertDialog=new AlertDialog.Builder(Main2Activity.this).create(); alertDialog.setTitle("提示"); alertDialog.setMessage("是否真的删除数据?"); alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { sqLhelper.getWritableDatabase().execSQL("delete from temperature"); // sqLhelper.getWritableDatabase().execSQL("update sqlite_sequence SET seq = 0 where name ='temperature'"); Toast.makeText(Main2Activity.this,"清空成功",Toast.LENGTH_SHORT).show(); finish(); Intent intent = new Intent(Main2Activity.this, Main2Activity.class); startActivity(intent); } }); alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); alertDialog.show(); } @Override public boolean onCreatePanelMenu(int featureId, Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); MenuItem menuItem = menu.findItem(R.id.search); MenuItem deleteItem = menu.findItem(R.id.delete); menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { Intent intent=new Intent(Main2Activity.this,getData_activity.class); startActivityForResult(intent,0x11); return false; } }); deleteItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { deleteall(); return false; } }); return super.onCreatePanelMenu(featureId, menu); } // @Override // public boolean onCreatePanelMenu(int featureId, Menu menu) { // getMenuInflater().inflate(R.menu.menu,menu); // //获取搜索的菜单组件 // MenuItem menuItem = menu.findItem(R.id.search); // SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem); // //设置搜索的事件 // searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { // @Override // public boolean onQueryTextSubmit(String query) { // Toast t = Toast.makeText(Main2Activity.this, query, Toast.LENGTH_SHORT); // t.setGravity(Gravity.TOP, 0, 0); // t.show(); // return false; // } // @Override // public boolean onQueryTextChange(String newText) { // return false; // } // }); // return super.onCreateOptionsMenu(menu); // } @Override protected void onDestroy() { super.onDestroy(); if(sqLhelper!=null) { sqLiteDatabase.close(); sqLhelper.close(); } } }
activity_main2.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:background="@drawable/by" android:orientation="vertical" tools:context=".Main2Activity"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/linear1" > </LinearLayout> </ScrollView> </LinearLayout>
SQLhelper:
package com.example.temperatureapp; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import androidx.annotation.Nullable; public class SQLhelper extends SQLiteOpenHelper { final String ADDSQL="create table temperature (Temper varchar(20),Time varchar(20),Time2 varchar(20))"; public SQLhelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context, name, null, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(ADDSQL); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.i("体温","--版本更新"+oldVersion+"-->"+newVersion); String sql = "alter table tb_Temp1 add sex varchar(8)"; db.execSQL(sql); } }
getData_Activity:
package com.example.temperatureapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import androidx.annotation.Nullable; import java.util.Calendar; public class getData_activity extends Activity { public int year,month,day; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gettime_f); Button button=findViewById(R.id.ok); DatePicker datePicker=findViewById(R.id.Dpicker); Calendar calendar=Calendar.getInstance(); year=calendar.get(Calendar.YEAR); month=calendar.get(Calendar.MONTH); day=calendar.get(Calendar.DAY_OF_MONTH); datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) { getData_activity .this.year=year; getData_activity .this.month=monthOfYear; getData_activity .this.day=dayOfMonth; } }); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=getIntent(); String date=getData_activity.this.year+"年"+getData_activity.this.month+"月"+getData_activity.this.day+"日"; Bundle bundle=new Bundle(); bundle.putString("time",date); intent.putExtras(bundle); setResult(0x11,intent); finish(); } }); } }
activity_get_data.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:gravity="center" android:background="#BEA4A4" > <DatePicker android:id="@+id/Dpicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/Dpicker" android:layout_centerHorizontal="true" android:text="确定" android:background="#E4B1B1"/> </RelativeLayout>
getTime_Activity:
package com.example.temperatureapp; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TimePicker; public class getTime_Activity extends Activity { private int h,m; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_get_time_); Button button=findViewById(R.id.Tok); TimePicker timePicker=findViewById(R.id.TDpicker); timePicker.setIs24HourView(true); timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { @Override public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { getTime_Activity.this.h=hourOfDay; getTime_Activity.this.m=minute; } }); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=getIntent(); String date=getTime_Activity.this.h+"时"+getTime_Activity.this.m+"分"; Bundle bundle=new Bundle(); bundle.putString("time",date); intent.putExtras(bundle); setResult(0x22,intent); finish(); } }); } }
xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="#BEA4A4" tools:context=".getTime_Activity"> <TimePicker android:id="@+id/TDpicker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <Button android:id="@+id/Tok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/TDpicker" android:layout_centerHorizontal="true" android:text="确定" android:background="#E4B1B1"/> </RelativeLayout>
munu文件:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/search" app:actionViewClass="android.support.v7.widget.SearchView" android:icon="@android:drawable/ic_menu_search" android:orderInCategory="100" android:title="搜索日期" app:showAsAction="never" /> <item android:id="@+id/delete" app:actionViewClass="android.support.v7.widget.SearchView" android:icon="@android:drawable/ic_menu_search" android:orderInCategory="100" android:title="删除全部" app:showAsAction="never" /> </menu>