当前位置: 首页 > news >正文

鸿蒙小案例---心情日记

效果演示

代码实现

import { router, window } from '@kit.ArkUI'

@Entry
@Component
struct Index {
  async aboutToAppear(): Promise<void> {
    let w = await window.getLastWindow(getContext())
    w.setWindowSystemBarProperties({
      statusBarColor: '#00C6C3',
      statusBarContentColor: '#FFFFFF'
    })
  }

  build() {
    Column({ space: 16 }) {
      Text('心情日记 - 首页')
        .fontSize(26)
      Button('去写日记')
        .backgroundColor('#00c6c3')
        .onClick(() => {
          router.pushUrl({
            url: 'pages/WriteDiary'
          })
        })

    }
    .width('100%')
    .height('100%')
    .padding(20)

    // .backgroundColor(Color.Pink)
  }
}
import { Font, window } from '@kit.ArkUI'

@Entry
@Component
struct WriteDiary {
  static readonly BASE_FONT_SIZE = 22 //页面基础字体大小
  @State diaryContent: string = '' // 日记内容
  @LocalStorageLink('WeatherFromUIAbility') weather: string = '选择天气'
  @LocalStorageLink('MoodFromUIAbility') mood: string = '选择心情'
  // 天气选择对话框
  weatherDialogCtrl = new CustomDialogController({
    builder: WeatherDialog({ weather: this.weather }),
    width: '100%',
    height: 333,
    alignment: DialogAlignment.Bottom,
    cornerRadius: 0,
    offset: {
      dx: 0,
      dy: 25
    }
  })

  // 心情选择对话框
  moodDialogCtrl = new CustomDialogController({
    builder: MoodDialog({ mood: this.mood }),
    width: '100%',
    height: 333,
    alignment: DialogAlignment.Bottom,
    cornerRadius: 0,
    offset: {
      dx: 0,
      dy: 25
    }
  })

  async aboutToAppear(): Promise<void> {
    let w = await window.getLastWindow(getContext())
    w.setWindowLayoutFullScreen(true)
  }

  build() {
    Column() {
      // 顶部栏
      Row() {
        Image('/images/back.svg')
          .width(11)
          .height(18)


        Text('2024年4月25日')
          .fontSize(14)
          .fontColor('#fff')
          .margin({ left: 10 })

        Blank()

        Row({ space: 3 }) {
          Text('101')
            .fontSize(13)
            .fontColor('#fff')
          Image('/images/down.svg')
            .height(5)
        }
        .width(46)
        .margin({ right: 20 })

        Image('/images/more.svg')
          .width(4)
          .height(16)
      }
      .height(94)
      .width('100%')
      .backgroundColor('#00C6C3')
      .alignItems(VerticalAlign.Bottom)
      .padding({ left: 20, right: 20, bottom: 20 })

      //   写日记
      Column() {
        TextArea({
          placeholder: '写日记'
        })// .placeholderFont(globalThis.parseInt(14))
          .placeholderColor('#AAAAAA')
          .placeholderFont({ size: WriteDiary.BASE_FONT_SIZE })
          .backgroundColor('#fff')
          .margin({ left: 20, top: 20 })
          .onChange((value: string) => {
            this.diaryContent = value
          })
      }
      .layoutWeight(1)

      //   添加图片
      Row() {
        Text('+')
          .textAlign(TextAlign.Center)
          .fontColor('#fff')
          .fontSize(30)
          .fontWeight(700)
          .width(111)
          .height(99)
          .backgroundColor('#f2f2f2')
          .borderRadius(5)
      }
      .width('100%')
      .justifyContent(FlexAlign.Start)
      .margin({ left: 40 })

      //   选择天气
      Row({ space: 10 }) {
        Image('/images/weather.png')
          .width(20)
          .height(14)
        Text(this.weather)
          .fontSize(13)
          .fontColor('#7f7f7f')
          .onClick(_ => {
            this.weatherDialogCtrl.open()
          })
      }
      .width('100%')
      .justifyContent(FlexAlign.Start)
      .margin({ left: 40, top: 20 })

      //   选择心情
      Row({ space: 10 }) {
        Image('/images/excited.png')
          .width(19)
          .height(19)
        Text(this.mood)
          .fontSize(13)
          .fontColor('#7f7f7f')
          .onClick(_=>{
            this.moodDialogCtrl.open()
          })
      }
      .width('100%')
      .justifyContent(FlexAlign.Start)
      .margin({ left: 40, bottom: 40, top: 15 })

    }
    .width('100%')
    .height('100%')
    .backgroundColor('#FFFFFF')

    // .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])
  }
}

// 天气选择对话框
@CustomDialog
struct WeatherDialog {
  ctrl: CustomDialogController
  @Link weather: string

  build() {
    Grid(){
      GridItem(){
        Column({space:6}){
          Image('/images/sun.png')
            .width(22)
          Text('晴')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.weather = '晴'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/cloud.png')
            .width(22)
          Text('多云')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.weather = '多云'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/overcast.png')
            .width(22)
          Text('阴')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.weather = '阴'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/rain.png')
            .width(22)
          Text('雨')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.weather = '雨'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/snow.png')
            .width(22)
          Text('雪')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.weather = '雪'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/frost.png')
            .width(22)
          Text('霜冻')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.weather = '霜冻'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/thunder.png')
            .width(22)
          Text('雷')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.weather = '雷'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/thunderrain.png')
            .width(22)
          Text('雷雨')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.weather = '雷雨'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/fog.png')
            .width(22)
          Text('雾')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.weather = '雾'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/wind.png')
            .width(22)
          Text('风')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.weather = '风'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/sunrise.png')
            .width(22)
          Text('日出')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.weather = '日出'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/sunset.png')
            .width(22)
          Text('日落')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.weather = '日落'
          this.ctrl.close()
        })
      }
    }
    .columnsTemplate('1fr 1fr 1fr 1fr')
    .rowsTemplate('1fr 1fr 1fr')
  }
}

// 心情选择对话框
@CustomDialog
struct MoodDialog {
  ctrl: CustomDialogController
  @Link mood: string
  build() {
    Grid(){
      GridItem(){
        Column({space:6}){
          Image('/images/happy.png')
            .width(22)
          Text('开心')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.mood = '开心'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/sad.png')
            .width(22)
          Text('伤心')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.mood = '伤心'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/wannacry.png')
            .width(22)
          Text('想哭')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.mood = '想哭'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/excited.png')
            .width(22)
          Text('兴奋')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.mood = '兴奋'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/speechless.png')
            .width(22)
          Text('无语')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.mood = '无语'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/angry.png')
            .width(22)
          Text('愤怒')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.mood = '愤怒'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/frightened.png')
            .width(22)
          Text('惊吓')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.mood = '惊吓'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/expected.png')
            .width(22)
          Text('期待')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.mood = '期待'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/melancholy.png')
            .width(22)
          Text('惆怅')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.mood = '惆怅'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/shocked.png')
            .width(22)
          Text('裂开')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.mood = '裂开'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/daze.png')
            .width(22)
          Text('发呆')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.mood = '发呆'
          this.ctrl.close()
        })
      }
      GridItem(){
        Column({space:6}){
          Image('/images/emo.png')
            .width(22)
          Text('emo')
            .fontSize(WriteDiary.BASE_FONT_SIZE)
        }
        .width('100%')
        .height('100%')
        .justifyContent(FlexAlign.Center)
        .onClick(_=>{
          this.mood = 'emo'
          this.ctrl.close()
        })
      }
    }
    .rowsTemplate('1fr 1fr 1fr')
    .columnsTemplate('1fr 1fr 1fr 1fr')
  }
}

http://www.dtcms.com/a/122920.html

相关文章:

  • VSCode解决中文乱码方法
  • 【c语言】深度剖析数据在内存中的存储
  • SpringAI调用硅基流动免费模型
  • 应急响应-进程排查
  • Ceph异地数据同步之-Cephfs异地同步复制
  • 【图书管理系统】全栈开发图书管理系统获取图书列表接口(后端:计算图书页数、查询当前页展示的书籍)
  • 前端三件套—HTML入门
  • 数论学习笔记:素数筛
  • 数据库事务隔离级别
  • 前端性能指标详解
  • 【Leetcode-Hot100】盛最多水的容器
  • React 响应事件
  • 如何实现文本回复Ai ChatGPT DeepSeek 式文字渐显效果?前端技术详解(附完整代码)
  • 【MySQL】安装
  • CD25.【C++ Dev】类和对象(16) static成员(上)
  • redis(2)-mysql-锁
  • 经典算法 最近点对问题
  • 猜猜乐游戏(python)
  • Trae AI 保姆级教程:从安装到调试全流程指南
  • FastAdmin和thinkPHP学习文档
  • 国标GB28181协议EasyCVR视频融合平台:5G时代远程监控赋能通信基站安全管理
  • 文字识别 (OCR) 工具
  • js 拷贝-包含处理循环引用问题
  • c++和python复制java文件到指定目录
  • AQS机制详解与总结
  • java方法07:加减乘除计算器
  • rkmpp 解码 精简mpi_dec_test.c例程
  • LeetCode 热题 100 题解记录
  • Docker Hello World
  • 计算机网络 实验三:子网划分与组网