harmonyos 黑马课程学习记录1-健康生活项目

项目介绍:

综合运用harmony os所学知识进行编程的一个有关饮食健康的软件,可以对饮食,食物热量进行计算,同时根据上限热量判断出每一天所需的正常热量值,对每天食物热量进行分析判断,有没有吃很多高热量食物,系统排版简洁明了,以最直观的方式判断热量分配,一目了然,保持每天健康生活。

项目概览:

开始页面

代码介绍:

1:开始页面背景代码

import PreferencesUtil from '../common/utils/PreferenceUtil'
import common from '@ohos.app.ability.common'
import UserPrivacyDialog from '../view/Welcome/UserPrivacyDialog'
import router from '@ohos.router'

@Extend(Text) function opacityWhiteText(opacity: number, fontSide: number = 10) {
  .fontSize(fontSide)
  .opacity(opacity).fontColor(Color.White)
}

const Pref_key = 'userPrivacyKey'

@Entry
@Component
struct Index {
  //上下文
  context = getContext(this) as common.UIAbilityContext
  //自定义对话框
  controller: CustomDialogController = new CustomDialogController({
    builder: UserPrivacyDialog({
      //传参
      confirm: () => this.onConform(),
      cancel: () => this.exitApp()
    })
  })

  async aboutToAppear() {
    //生命周期构造
    //加载搜选项
    let isAgree = await PreferencesUtil.getPreferenceValue(Pref_key, false)

    //异步处理
    if (isAgree) {
      this.jumpToIndex()

    } else {
      //不同意
      this.controller.open()
    }
  }

  //同意
  onConform() {
    //同意 跳转首页 //保存首选项
    PreferencesUtil.putPreferenceValue(Pref_key, true)
    this.jumpToIndex()
  }

  jumpToIndex() {
    setTimeout(() => {
      //定时任务
      router.replaceUrl({
        url: 'pages/Index'
      })
    }, 1000) //ms
  }
  //pushUrl会压栈,但是本页面只弹窗一次,不需要入栈
  //不同
  exitApp() {
    //退出app
    this.context.terminateSelf() //终结上下文
  }

  @State message: string = 'Hello World'

  build() {
    Column({ space: 10 }) {
      //中央图片
      Row() {
        Image($r('app.media.home_slogan')).width(260)
      }
      .layoutWeight(1)
      //logo
      Image($r('app.media.home_logo')).width(150)
      //信息
      Row() {
        Text('黑马健康').opacityWhiteText(1, 10)
        Text('IPv6')
          .opacityWhiteText(1, 10)
          .border({ style: BorderStyle.Solid, width: 1, color: Color.White, radius: 15 })
          .padding({ left: 5, right: 5 })
        Text('网络').opacityWhiteText(1, 10)
      }

      Text(`‘减更多’指黑马健康app希望通过软件工具的形式,帮助更多用户实现身材管理`)
        .opacityWhiteText(0.8)
      Text(`浙ICP备0000000000号-36D`)
        .opacityWhiteText(0.6)
        .margin({ bottom: 30 })
    }
    .width('100%')
    .height('100%')
    .backgroundColor($r('app.color.welcome_page_background'))
  }
}

2:开始页面弹窗:

import { CommonConstants } from '../../common/constants/CommonConstants'
//UI预览

@CustomDialog
 export default  struct UserPrivacyDialog {
  //export default用于导出一个模块的默认值。

  controller:CustomDialogController
  confirm:()=>void
  cancel:()=>void

  build() {
    Column({space:10}){
      //标题
      Text($r('app.string.user_privacy_title'))
        .fontSize(20)
        //CommonConstants通常用于声明和存储应用程序中常用的常量值
        .fontWeight(CommonConstants.FONT_WEIGHT_700)
//内容
      Text($r('app.string.user_privacy_content'))
      //按钮
    Button($r('app.string.agree_label'))
      .width(150)
      .backgroundColor($r('app.color.primary_color'))
      .onClick(()=>{
        this.confirm()//关闭方法
        this.controller.close()
      })
      Button($r('app.string.refuse_label'))
        .width(150)
        .backgroundColor($r('app.color.lightest_primary_color'))
        //设置文本颜色的属性
        .fontColor($r('app.color.light_gray'))
        .onClick(()=>{
          this.cancel()//关闭方法
          this.controller.close()
        })

    }
    .width('100%')
    //内边距
    .padding(15)
    .borderColor('#ffacabab')
   .borderRadius(20)//圆角
  }
}

*注:代码中所调用的各种图牌、颜色、字体等均为黑马官方素材。

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值