主页主要是一张图片以及一段健康科普语句进行推送。
用户在对自己进行完中医体质健康测评问卷之后,便可以得知自身的体质,从而得到相应的语句推送。饮食建议的内容也会随着用户体质状况的更新而更新。
下面先上图
首页部分的代码实现:
package com.example.healthymore
import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import java.text.SimpleDateFormat
import java.util.Date
//2022/11/27
//还差一个信息更新的功能
//科普语句推荐的功能
class MainActivity : AppCompatActivity() {
@SuppressLint("MissingInflatedId", "Range")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//隐藏顶部标题栏框
supportActionBar?.hide()
val extraData = intent.getStringExtra("extra_data")
val ms:TextView = findViewById(R.id.main_sentence)
val y = SimpleDateFormat("yyyy")
val year = y.format(Date())
val m = SimpleDateFormat("MM")
val month = m.format(Date())
val d = SimpleDateFormat("dd")
val day = d.format(Date())
//Log.d("WHYWHY_main","$extraData")
val datetext:TextView = findViewById(R.id.datee)
datetext.setText("$year/$month/$day")
val dbHelper = HealthDataBase(this,"Health.db",5)
val db = dbHelper.writableDatabase
var Users_constitution = ""
var Users_life_advise = "顺应四季变化,冷添衣,热去衣,以保持自身与自然界的整体阴阳平衡"
val Cursor = db.query("USERS",null,null,null,null,null,null)
if(Cursor.moveToFirst()) {
do {
val account_getInDB = Cursor.getString(Cursor.getColumnIndex("Users_account"))
val main_constitution = Cursor.getString(Cursor.getColumnIndex("Constitution_number"))
if (account_getInDB == extraData){
Users_constitution = main_constitution
Log.d("mainnn:",Users_constitution)
break;
}
} while (Cursor.moveToNext())
}
val cur = db.query("HEALTHSENTENCE",null,null,null,null,null,null)
if(cur.moveToFirst()) {
do {
val constitution_number_inDB = cur.getString(cur.getColumnIndex("Constitution_number"))
val life_inDB = cur.getString(cur.getColumnIndex("Health_life"))
if (constitution_number_inDB == Users_constitution) {
Users_life_advise = life_inDB
break;
}
} while(cur.moveToNext())
}
ms.setText(Users_life_advise)
//主页按钮
val buttonindex:Button = findViewById(R.id.index)
buttonindex.setOnClickListener{
val intentindex = Intent(this,MainActivity::class.java)
val account = extraData
intentindex.putExtra("extra_data",account)
startActivity(intentindex)
}
//规划按钮
val buttonplan:Button = findViewById(R.id.plan)
buttonplan.setOnClickListener{
val account = extraData
val intentplan = Intent(this,PlanActivity::class.java)
intentplan.putExtra("extra_data",account)
startActivity((intentplan))
}
//社区按钮
val buttoncommunity:Button = findViewById(R.id.community)
buttoncommunity.setOnClickListener{
val account = extraData
val intentcommunity = Intent(this,CommunicityActivity::class.java)
intentcommunity.putExtra("extra_data",account)
startActivity(intentcommunity)
}
//个人按钮
val buttomregister:Button = findViewById(R.id.register)
buttomregister.setOnClickListener{
val account = extraData
val intentregister = Intent(this,RegisterActivity::class.java)
intentregister.putExtra("extra_data", account)
startActivity(intentregister)
}
}
}
规划界面的代码实现
package com.example.healthymore
import android.annotation.SuppressLint
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import java.text.SimpleDateFormat
import java.util.*
class PlanActivity : AppCompatActivity() {
@SuppressLint("MissingInflatedId", "Range", "Recycle")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_plan)
supportActionBar?.hide()
val extraData = intent.getStringExtra("extra_data")
if (extraData != null) {
Log.d("planget:",extraData)
}
val extraConstitution = intent.getStringExtra("extra_constitution")
val launch_:TextView = findViewById(R.id.launch_advise)
val addwater:Button = findViewById(R.id.water_add_1)
val summwater:TextView = findViewById(R.id.water_num)
val water_d:TextView = findViewById(R.id.water_date)
/*
//Log.d("WHYWHY_plan","$extraData")
val y = SimpleDateFormat("yyyy")
val year = y.format(Date())
val m = SimpleDateFormat("MM")
val month = m.format(Date())
val d = SimpleDateFormat("dd")
val day = d.format(Date())
//Log.d("WHYWHY_main","$extraData")
//val date_water: TextView = findViewById(R.id.water_date)
//date_water.setText("日期:$year/$month/$day")
*/
val dbHelper = HealthDataBase(this,"Health.db",5)
val db = dbHelper.writableDatabase
var Users_constitution = ""
var Users_launch_advise = "忌食辛辣\n平衡膳食"
val Cursor = db.query("USERS",null,null,null,null,null,null)
if(Cursor.moveToFirst()) {
do {
val account_getInDB = Cursor.getString(Cursor.getColumnIndex("Users_account"))
val main_constitution = Cursor.getString(Cursor.getColumnIndex("Constitution_number"))
//val lasttime = Cursor.getString(Cursor.getColumnIndex("Users_time"))
//val lastcount = Cursor.getString(Cursor.getColumnIndex("Users_count"))
if (account_getInDB == extraData){
//summwater.setText(lastcount)
//water_d.setText(lasttime)
Users_constitution = main_constitution
Log.d("mainnn:",Users_constitution)
break;
}
} while (Cursor.moveToNext())
}
val cur = db.query("HEALTHSENTENCE",null,null,null,null,null,null)
if(cur.moveToFirst()) {
do {
val constitution_number_inDB = cur.getString(cur.getColumnIndex("Constitution_number"))
val launch_inDB = cur.getString(cur.getColumnIndex("Health_launch"))
Log.d("mainnn1:",Users_constitution)
Log.d("mainnnnumber:",constitution_number_inDB)
if (constitution_number_inDB == Users_constitution) {
Users_launch_advise = launch_inDB
Log.d("advise:",Users_launch_advise)
break;
} else if(constitution_number_inDB == extraConstitution){
Users_launch_advise = launch_inDB
break;
}
} while(cur.moveToNext())
}
launch_.setText(Users_launch_advise)
var sum_water = 0;
addwater.setOnClickListener {
sum_water++
summwater.setText("饮水量:$sum_water 杯")
val simple = SimpleDateFormat("HH:mm:ss")//定义一个时间格式
val ctime = Date(System.currentTimeMillis()) //获取系统当前时间
val time: String = simple.format(ctime) //用这个自定义日期格式格式化当前日期
water_d.setText("上次喝水时间:$time")
/*
db.execSQL("update USERS set Users_time = ? where Users_account = ?",
arrayOf(time,extraData))
db.execSQL("update USER set Users_count = ? where Users_account = ?",
arrayOf(sum_water,extraData))
*/
}
//主页按钮
val buttonindex: Button = findViewById(R.id.index)
buttonindex.setOnClickListener {
val intentindex = Intent(this, MainActivity::class.java)
val account = extraData
intentindex.putExtra("extra_data", account)
startActivity(intentindex)
}
//规划按钮
val buttonplan: Button = findViewById(R.id.plan)
buttonplan.setOnClickListener {
val account = extraData
val intentplan = Intent(this, PlanActivity::class.java)
intentplan.putExtra("extra_Data", account)
if (account != null) {
Log.d("plan:",account)
}
startActivity(intentplan)
}
//社区按钮
val buttoncommunity: Button = findViewById(R.id.community)
buttoncommunity.setOnClickListener {
val account = extraData
val intentcommunity = Intent(this, CommunicityActivity::class.java)
intentcommunity.putExtra("extra_data", account)
startActivity(intentcommunity)
}
//个人按钮
val buttomregister: Button = findViewById(R.id.register)
buttomregister.setOnClickListener {
val account = extraData
val intentregister = Intent(this, RegisterActivity::class.java)
intentregister.putExtra("extra_data", account)
startActivity(intentregister)
}
}
}
主页布局:
<?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"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@+id/pic1"
android:layout_marginTop="252dp"
android:background="@color/teal_700"
android:contentDescription="@string/todo" />
<TextView
android:id="@+id/datee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="@string/date"
android:textColor="@color/teal_700"
android:textSize="30sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/xian1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@+id/datee"
android:layout_marginTop="10dp"
android:background="@color/teal_700"
android:contentDescription="@string/todo" />
<ImageView
android:id="@+id/xian2"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@+id/pic1"
android:layout_marginTop="14dp"
android:background="@color/teal_700"
android:contentDescription="@string/todo" />
<Button
android:id="@+id/index"
android:layout_width="100dp"
android:layout_height="61dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:text="@string/index"
android:textColor="@color/teal_200"
android:backgroundTint="@color/teal_700"
android:textSize="20sp"
android:textStyle="bold"/>
<Button
android:id="@+id/plan"
android:layout_width="100dp"
android:layout_height="61dp"
android:layout_toEndOf="@+id/index"
android:layout_alignParentBottom="true"
android:text="@string/plan"
android:textColor="@color/teal_200"
android:backgroundTint="@color/teal_700"
android:textSize="20sp"
android:textStyle="bold"/>
<Button
android:id="@+id/community"
android:layout_width="100dp"
android:layout_height="61dp"
android:layout_toEndOf="@+id/plan"
android:layout_alignParentBottom="true"
android:text="@string/community"
android:textColor="@color/teal_200"
android:backgroundTint="@color/teal_700"
android:textSize="20sp"
android:textStyle="bold"/>
<Button
android:id="@+id/register"
android:layout_width="110dp"
android:layout_height="61dp"
android:layout_toEndOf="@+id/community"
android:layout_alignParentBottom="true"
android:text="@string/register"
android:textColor="@color/teal_200"
android:backgroundTint="@color/teal_700"
android:textSize="20sp"
android:textStyle="bold"/>
<ImageView
android:id="@+id/pic1"
android:layout_width="340dp"
android:layout_height="355dp"
android:layout_below="@+id/datee"
android:layout_alignParentEnd="true"
android:layout_marginTop="22dp"
android:layout_marginEnd="37dp"
android:contentDescription="@string/todo"
android:src="@drawable/pic1" />
<TextView
android:id="@+id/main_sentence"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="@+id/xian2"
android:layout_centerInParent="true"
android:layout_marginTop="60dp"
android:text="给我好好吃饭!\n好好睡觉!\n好好学习!\n好好学数据库!"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="130dp"
android:layout_marginTop="447dp"
android:text="生活小贴士"
android:textSize="30sp" />
</RelativeLayout>
规划界面布局:
<?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">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@+id/pan"
android:layout_marginTop="9dp"
android:background="@color/teal_700"
android:contentDescription="@string/todo" />
<Button
android:id="@+id/index"
android:layout_width="100dp"
android:layout_height="61dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:text="@string/index"
android:textColor="@color/teal_200"
android:backgroundTint="@color/teal_700"
android:textSize="20sp"
android:textStyle="bold"/>
<Button
android:id="@+id/plan"
android:layout_width="100dp"
android:layout_height="61dp"
android:layout_toEndOf="@+id/index"
android:layout_alignParentBottom="true"
android:text="@string/plan"
android:textColor="@color/teal_200"
android:backgroundTint="@color/teal_700"
android:textSize="20sp"
android:textStyle="bold"/>
<Button
android:id="@+id/community"
android:layout_width="100dp"
android:layout_height="61dp"
android:layout_toEndOf="@+id/plan"
android:layout_alignParentBottom="true"
android:text="@string/community"
android:textColor="@color/teal_200"
android:backgroundTint="@color/teal_700"
android:textSize="20sp"
android:textStyle="bold"/>
<Button
android:id="@+id/register"
android:layout_width="110dp"
android:layout_height="61dp"
android:layout_toEndOf="@+id/community"
android:layout_alignParentBottom="true"
android:text="@string/register"
android:textColor="@color/teal_200"
android:backgroundTint="@color/teal_700"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/pan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="健康规划"
android:textSize="30sp" />
<ImageView
android:id="@+id/imag2e"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="@+id/pan"
android:layout_marginTop="192dp"
android:background="@color/teal_700"
android:contentDescription="@string/todo" />
<TextView
android:id="@+id/water_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_alignParentStart="true"
android:layout_marginStart="43dp"
android:layout_marginTop="126dp"
android:text="上次喝水时间:0:0"
android:textSize="25sp" />
<TextView
android:id="@+id/water_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_alignParentStart="true"
android:layout_marginStart="43dp"
android:layout_marginTop="26dp"
android:text="饮水量:0杯"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_alignParentStart="true"
android:layout_marginStart="45dp"
android:layout_marginTop="76dp"
android:text="建议饮水量:7杯"
android:textSize="30sp" />
<Button
android:id="@+id/water_add_1"
android:layout_width="71dp"
android:layout_height="wrap_content"
android:layout_below="@+id/water_date"
android:layout_alignParentStart="true"
android:layout_marginStart="312dp"
android:layout_marginTop="-137dp"
android:text="+1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="146dp"
android:layout_marginTop="269dp"
android:text="饮食建议"
android:textSize="30sp" />
<TextView
android:id="@+id/launch_advise"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="54dp"
android:layout_marginTop="319dp"
android:text="多喝热水!少吃甜食!多吃蔬菜!"
android:textSize="26sp" />
</RelativeLayout>