HarmonyOS个人项目案例——综合开发过程记录(终)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

        此篇内容是以前四篇开发过程为基底的一个总结。


一、整体代码介绍

        程序经过一个弹窗询问用户是否接受隐私协议,如果是则进入登录界面,否则退出程序,进入登陆界面后,可以输入账号密码点击注册来注册账号,注册成功会有注册成功提示,也可点击忘记密码来更改密码,更改成功后会有更改成功提示,输入账号密码以进行登录进入第一个小游戏,在第一个小游戏中可通过上下左右箭头来控制海马的移动,点击下一个项目按钮则进入创建文档页面,点击创建文档来新建一个文档,单击此文档来进入编写文档内容页面,可在此页面进行编写文档标题和文档的内容。

二、程序代码

1.代码

/弹窗/

@CustomDialog
export default struct tanchuang {
  controller: CustomDialogController
  confirm: () => void
  cancel: () => void

  build() {
    Column({ space: 10 }) {
      Text('欢迎使用此app').fontSize(20).fontWeight(20)
      Text('本应用尊重并保护所有使用服务用户的个人隐私权。为了给您提供更准确、更有个性化的服务,本应用会按照本隐私权政策的规定使用和披露您的个人信息。但本应用将以高度的勤勉、审慎义务对待这些信息。除本隐私权政策另有规定外,在未征得您事先许可的情况下,本应用不会将这些信息对外披露或向第三方提供。本应用会不时更新本隐私权政策。 您在同意本应用服务使用协议之时,即视为您已经同意本隐私权政策全部内容。本隐私权政策属于本应用服务使用协议不可分割的一部分。')
      Button('同意')
        .width(150)
        .backgroundColor($r('app.color.welcome_color'))
        .onClick(() => {
          this.confirm()
          this.controller.close()
        })
      Button('不同意')
        .width(150)
        .backgroundColor($r('app.color.color2'))
        .fontColor($r('app.color.color3'))
        .onClick(() => {
          this.cancel()
          this.controller.close()
        })
        .width('100%')
        .padding(10)
    }
  }
}

/欢迎页面/
import userAuth from '@ohos.userIAM.userAuth'
import tanchuang from '../view/tanchuang'
import preferences from '@ohos.data.preferences'
import router from '@ohos.router'
import systemDateTime from '@ohos.systemDateTime'
import app from '@system.app';
@Extend(Text) function opacityWhitetext(opacity: number,fontSize: number=10){
  .fontSize(fontSize)
    .opacity(opacity)
    .fontColor(Color.White)
}
@Entry
@Component
struct Welcome {
  controller:CustomDialogController=new CustomDialogController({
    builder: tanchuang({
      confirm: () => this.onConfirm(),
      cancel: () => app.terminate()
    })
  })
  aboutToAppear(){
    this.controller.open()
  }
  onConfirm(){
  this.jumpToIndex()
  }
  jumpToIndex(){
    setTimeout(()=>{
      router.replaceUrl({
        url:'pages/Index'
      })
    },1000)
  }

  build() {
    Column({ space: 10 }) {
      Row() {
        Image($r('app.media.ic_main')).width(260)
      }
      .layoutWeight(1)

      Row() {
        Text('用心生活').opacityWhitetext(0.8, 12)
        Text('IPV6')
          .opacityWhitetext(0.8)
          .border({ style: BorderStyle.Solid, width: 1, color: Color.White, radius: 15 })
          .padding({ left: 5, right: 5 })
        Text('网络').opacityWhitetext(0.8, 12)
      }

      Text('科技带来崭新生活方式,此app将用心帮助用户')
        .opacityWhitetext(0.6)
      Text('浙ICP备00000000号-36D')
        .opacityWhitetext(0.4)
        .margin({ bottom: 35 })
    }
    .width('100%')
    .height('100%')
    .backgroundColor($r('app.color.welcome_color'))
  }
}

/登录界面/
import router from '@ohos.router'
@Extend(Text) function opacityWhitetext(opacity: number,fontSize: number=10){
  .fontSize(fontSize)
  .opacity(opacity)
  .fontColor(Color.White)
}
@Entry
@Component
struct Index {
  build() {
    Column({ space: 10 }) {
      Row() {
      }

      Row() {
      }

      Row() {
        Image($r('app.media.ic_main')).width(150)
      }

      Row() {
        Text('用户登录').opacityWhitetext(1.5, 35)
      }

      Row() {
        Text('登录账号以提供更多服务').opacityWhitetext(1, 18)
      }

      Row() {
        TextInput({ placeholder: "邮箱/手机号/用户名" })
          .backgroundColor('#ffffff')
          .width(300)
          .type(InputType.Normal)
      }

      Row() {
        TextInput({ placeholder: "密码" })
          .backgroundColor('#ffffff')
          .width(300)
          .type(InputType.Password)
      }

      Row() {
        Button('登录').width(150).backgroundColor('#808080').onClick(()=>{
          router.pushUrl({
            url:'pages/page1'
          })
        })

      }
      Row() {
        Button('忘记密码').onClick(()=>{
          router.pushUrl({
            url:'pages/forget'
          })
        })
          .fontColor(Color.Black)
          .backgroundColor($r('app.color.welcome_color'))
        Button('注册').onClick(()=>{
          router.pushUrl({
            url:'pages/zhuce'
          })
        })
          .fontColor(Color.Black)
          .backgroundColor($r('app.color.welcome_color'))
      }
    }

        .width('100%')
        .height('100%')
        .backgroundColor($r('app.color.welcome_color'))
    }
  }

/注册成功界面/
import router from '@ohos.router'
@Extend(Text) function opacitytext(opacity: number,fontSize: number=10){
  .fontSize(fontSize)
  .opacity(opacity)
  .fontColor(Color.White)
}
@Entry
@Component
struct zhuce {
  build() {
    Column() {
      Row() {
        Image($r("app.media.ic_public_back"))
          .width(24)
          .height(24)
          .onClick(() => {
            router.back();
          })
        Text('注册成功').opacitytext(0.8, 20)
          .fontColor(Color.Black)
      }
      Row(){
        Image($r("app.media.ic_registration_success"))
          .objectFit(ImageFit.Contain)
          .width(72)
          .height(72)
          .position({x:130,y:300})
      }
      Row(){
          Text('注册成功').textAlign(TextAlign.Center).position({y:400,x:130})
      }
      Row(){
        Button('点我返回首页').position({y:420,x:105}).onClick(()=>{
          router.back();
        })
          .fontColor(Color.Black)
          .backgroundColor(Color.White)
      }
    }
  }
}

/忘记密码/
import router from '@ohos.router'
@Extend(Text) function opacitytext(opacity: number,fontSize: number=10){
  .fontSize(fontSize)
  .opacity(opacity)
  .fontColor(Color.White)
}
@Entry
@Component
struct forget {
  build() {
    Column({space:10}) {
      Row() {
        Image($r("app.media.ic_public_back"))
          .width(24)
          .height(24)
          .onClick(() => {
            router.back();
          })
        Text('忘记密码').opacitytext(0.8, 20)
          .fontColor(Color.Black)
      }
      .alignSelf(ItemAlign.Start)
      Row(){}
      Row() {
        TextInput({ placeholder: "邮箱/手机号/用户名" })
          .backgroundColor('#ffffff')
          .width(300)
          .type(InputType.Normal)
      }
      Row() {
        TextInput({ placeholder: "更改密码" })
          .backgroundColor('#ffffff')
          .width(300)
          .type(InputType.Password)
      }
      Row() {
        Button('提交').width(150).backgroundColor('#808080').onClick(()=>{
          router.pushUrl({
            url:'pages/changepassword'
          })
        })

      }
    }
  }
}

/更改密码成功/
import router from '@ohos.router'
@Extend(Text) function opacitytext(opacity: number,fontSize: number=10){
  .fontSize(fontSize)
  .opacity(opacity)
  .fontColor(Color.White)
}
@Entry
@Component
struct changepassword {
  build() {
    Column() {
      Row() {
        Image($r("app.media.ic_public_back"))
          .width(24)
          .height(24)
          .onClick(() => {
            router.back();
          })
        Text('更改成功').opacitytext(0.8, 20)
          .fontColor(Color.Black)
      }
      Row(){
        Image($r("app.media.ic_registration_success"))
          .objectFit(ImageFit.Contain)
          .width(72)
          .height(72)
          .position({x:130,y:300})
      }
      Row(){
        Text('更改成功').textAlign(TextAlign.Center).position({y:400,x:130})
      }
      Row(){
        Button('点我返回首页').position({y:420,x:105}).onClick(()=>{
          router.pushUrl({
            url:'pages/Index'
          });
        })
          .fontColor(Color.Black)
          .backgroundColor(Color.White)
      }
    }
  }
}

/简单小游戏/
import router from '@ohos.router'
@Extend(Text) function opacityWhitetext(opacity: number,fontSize: number=10){
  .fontSize(fontSize)
  .opacity(opacity)
  .fontColor(Color.White)
}
@Entry
@Component
struct Page1 {
  @State fishX: number = 200
  @State fishY: number = 180
  @State angle: number = 0
  @State src: Resource = $r('app.media.haima')

  @State isBegin: boolean = false

  build() {
    Row() {
      Stack(){
        Button('下一个项目')
          .position({x:130, y: 730})
          .backgroundColor('#201010')
          .onClick(() => {
            router.pushUrl({
              url:'pages/page2'
            })
          })

        if(!this.isBegin){
          Button('开始游戏')
            .onClick(() => {
              animateTo(
                {duration: 1000},
                () => {
                  this.isBegin = true
                }
              )
            })
        }else{
          Image(this.src)
            .position({x: this.fishX - 20, y: this.fishY - 20})
            .rotate({angle:this.angle, centerX: '50%', centerY: '50%'})
            .width(40)
            .height(85)
            .transition({
              type: TransitionType.Insert,
              opacity: 0,
              translate: {x: -250}
            })
        }
        Row(){
          Button('←').backgroundColor('#20101010')
            .onClick(() => {
              animateTo(
                {duration: 500},
                () => {
                  this.fishX -= 20
                  this.src = $r("app.media.haima")
                }
              )
            })
          Column({space: 40}){
            Button('↑').backgroundColor('#20101010')
              .onClick(() => {
                animateTo(
                  {duration: 500},
                  () => {
                    this.fishY -= 20
                  }
                )
              })
            Button('↓').backgroundColor('#20101010')
              .onClick(() => {
                animateTo(
                  {duration: 500},
                  () => {
                    this.fishY += 20
                  }
                )
              })
          }
          Button('→').backgroundColor('#20101010')
            .onClick(() => {
              animateTo(
                {duration: 500},
                () => {
                  this.fishX += 20
                  this.src = $r('app.media.haima')
                }
              )
            })
        }
        .height(240)
        .width(240)
        .justifyContent(FlexAlign.Center)
        .position({x:50,y:500})
      }
      .height('100%').width('100%')
    }
    .height('100%')
    .width('100%')
    .backgroundImage($r('app.media.sea'))
    .backgroundImageSize({height: '105%', width: '100%'})
  }
}

/创建文档/
import router from '@ohos.router'
@Entry
@Component
struct page2 {
  private index: number = 1
  @State docs: number[] = []
  build() {
    Row() {
      Column({space: 10}) {
        Button('回到第一个游戏')
          .backgroundColor('#201010')
          .onClick(() => {
            router.pushUrl({
              url: 'pages/page1'
            })
          })
        Divider()
        Button('新建文档')
          .onClick(() => {
            this.docs.push(this.index)
          })
        ForEach(this.docs, id => {
          Row({space:10}){
            Image($r('app.media.ic_files_doc'))
              .width(20)
            Text(`文档${id}`)
              .fontSize(18)
              .onClick(() => {
                router.pushUrl({
                  url:'pages/Page3'
                })
              })
          }
          .width('100%')
        })
      }
      .width('100%')
      .height('100%')
      .padding(20)
    }
  }
}


/编写文档内容/
import router from '@ohos.router'
@Entry
@Component
struct Page3 {
  @State editTitle: boolean = true
  @State title: string = '标题'
  @State content: string = ''

  build() {
    Row() {
      Column({ space: 10 }) {
        Row({ space: 10 }) {
          Image($r('app.media.ic_public_back'))
            .width(30)
            .onClick(() => {
              router.back()
            })
          if (!this.editTitle) {
            Text(this.title)
              .fontSize(30)
              .fontWeight(FontWeight.Bold)
              .onClick(() => this.editTitle = true)
          } else {
            TextInput({
              placeholder: '请输入标题',
              text: this.title
            })
              .layoutWeight(1)
              .onChange(val => this.title = val)
            Button('确定')
              .onClick(() => this.editTitle = false)
          }
        }
        .width('100%')

        Divider()
        TextArea({
          placeholder: '请输入文档内容',
          text: this.content
        })
          .layoutWeight(1)
          .onChange(val => this.content = val)
      }
      .padding(10)
      .width('100%')
      .height('100%')
    }
  }
}

2.运行效果(流程)

HarmonyOS个人项目演示流程


总结

        总体来说此程序相对较为简单基础,但代码部分简洁不繁杂,条理清晰不混乱,简单而不杂乱,对于本人初学者来说是相对综合的一个考察。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值