From 2ae7a8881892276ca1dd0125b0c78aaadcbf06ea Mon Sep 17 00:00:00 2001 From: wonder Date: Tue, 23 Dec 2025 18:24:15 +0800 Subject: [PATCH] =?UTF-8?q?Feat:=20=E5=A2=9E=E5=8A=A0=E8=AE=A1=E7=AE=97=20?= =?UTF-8?q?BMI=20=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entry/src/main/ets/pages/BMI.ets | 99 +++++++++++++++++++ entry/src/main/ets/pages/Index.ets | 10 ++ .../resources/base/profile/main_pages.json | 3 +- 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 entry/src/main/ets/pages/BMI.ets diff --git a/entry/src/main/ets/pages/BMI.ets b/entry/src/main/ets/pages/BMI.ets new file mode 100644 index 0000000..64510d8 --- /dev/null +++ b/entry/src/main/ets/pages/BMI.ets @@ -0,0 +1,99 @@ +// BMI计算页面组件 +@Entry +@Component +struct BMIPage { + @State humanWeight: string = '' + @State humanHeight: string = '' + @State bmiValue: number = 0 + @State healthAssessment: string = '' + @State showError: boolean = false + + // 计算BMI + calculateBMI() { + // 输入验证 + if (!this.humanWeight || !this.humanHeight || + isNaN(Number(this.humanWeight)) || + isNaN(Number(this.humanHeight))) { + this.showError = true + return + } + + this.showError = false + const humanWeightNum = parseFloat(this.humanWeight) + const humanHeightNum = parseFloat(this.humanHeight) / 100.0 + this.bmiValue = humanWeightNum / (humanHeightNum * humanHeightNum) + + // 健康评估逻辑 + if (this.bmiValue < 18.5) { + this.healthAssessment = "偏瘦 | 建议增加体重" + } else if (this.bmiValue < 24.9) { + this.healthAssessment = "正常 | 保持良好的生活习惯" + } else if (this.bmiValue < 29.9) { + this.healthAssessment = "偏胖 | 建议减少体重" + } else { + this.healthAssessment = "肥胖 | 建议就医咨询" + } + } + + build() { + Column() { + // 输入区域 + Column({ space: 20 }) { + // 体重输入 + Row() { + Text('体重(公斤)').width(100) + TextInput({ text: this.humanWeight }) + .width('60%') + .type(InputType.Number) + .onChange((value: string) => { + this.humanWeight = value + }) + } + + // 身高输入 + Row() { + Text('身高(厘米)').width(100) + TextInput({ text: this.humanHeight }) + .width('60%') + .type(InputType.Number) + .onChange((value: string) => { + this.humanHeight = value + }) + } + }.padding(20) + + // 错误提示 + if (this.showError) { + Text('请输入有效的数字') + .fontColor(Color.Red) + .margin({ bottom: 10 }) + } + + // 计算按钮 + Button('计算BMI') + .width('80%') + .height(50) + .onClick(() => { + this.calculateBMI() + }) + + // 结果展示 + if (this.bmiValue > 0 && !this.showError) { + Column() { + Text(`BMI值: ${this.bmiValue.toFixed(1)}`) + .fontSize(20) + .margin({ bottom: 10 }) + Text(this.healthAssessment) + .fontColor('#666') + } + .width('90%') + .padding(20) + .backgroundColor('#f5f5f5') + .borderRadius(8) + .margin({ top: 20 }) + } + } + .width('100%') + .padding(20) + } +} \ No newline at end of file diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 8d370a8..d79b74e 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -37,6 +37,16 @@ struct Index { .height('100%') .justifyContent(FlexAlign.End) .alignItems(HorizontalAlign.Center) + .onClick(() => { + console.log("用户操作:点击按钮") + let uiContext: UIContext = this.getUIContext(); + let router = uiContext.getRouter(); + router.pushUrl({ + url: 'pages/BMI' + }).then(() => { + console.log('系统提示:跳转成功') + }) + }) } .width('100%') .height('100%') diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json index 528157c..406ceb2 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -1,6 +1,7 @@ { "src" : [ "pages/Index", "pages/Hello", - "pages/Text" + "pages/Text", + "pages/BMI" ] } \ No newline at end of file