Feat: 增加计算 BMI 页面
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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%')
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"src" : [ "pages/Index",
|
||||
"pages/Hello",
|
||||
"pages/Text"
|
||||
"pages/Text",
|
||||
"pages/BMI"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user