HarmonyOS Next应用开发实战——隐私政策页面实现(part2)

122 阅读1分钟
3. 隐私同意状态保存

saveIsPrivacy 方法用于将用户同意隐私政策的状态保存到本地存储中。

saveIsPrivacy() {
  preferencesUtil.putPreferencesValue(CommonConstants.PREFERENCES_FILE_NAME,CommonConstants.PREFERENCES_KEY_PRIVACY,true)
}
4. 跳转到广告页面

jumpToAdvertisingPage 方法通过 setTimeout 延迟一定时间后,将当前页面替换为广告页面。

jumpToAdvertisingPage() {
  this.timerId = setTimeout(() => {
    this.isJumpToAdvertising = true;
    this.pageInfos.replacePath({ name: 'SplashScreenPage2' }, false);
  }, CommonConstants.LAUNCHER_DELAY_TIME);
}
5. 页面布局与交互逻辑

build 方法中,构建了页面的整体布局,包括标题、隐私内容滚动区域和操作按钮。用户点击“拒绝并退出”按钮可退出应用,点击“同意”按钮则保存隐私同意状态并跳转到广告页面。

build() {
  NavDestination(){
    GridRow({
      breakpoints: { value: CommonConstants.BREAK_POINTS_VALUE, reference: BreakpointsReference.WindowSize },
      columns: { sm: CommonConstants.COLUMN_SM, md: CommonConstants.COLUMN_MD, lg: CommonConstants.COLUMN_LG },
      direction: GridRowDirection.Row
    }) {
      GridCol({ span: { sm: 0, md: 0, lg: 2 } }) {}
      GridCol({
        span: { sm: CommonConstants.COLUMN_SM, md: CommonConstants.COLUMN_MD, lg: CommonConstants.COLUMN_MD }
      }) {
        Flex({ direction: FlexDirection.Column }) {
          Row() {
            Text('隐私政策').fontWeight(700)
          }
          .justifyContent(FlexAlign.Center)
          .height('60vp')
          .width(CommonConstants.COLUMN_WIDTH)
          .overlay(this.OverlayNode(), { align: Alignment.Start })

          Scroll(this.scroller) {
            Column() {
              Text('隐私内容')
            }
          }
          .layoutWeight(1)
          .width(CommonConstants.COLUMN_WIDTH)

          Flex({ justifyContent: FlexAlign.SpaceEvenly }) {
            Button('拒绝并退出')
              .width('40%')
              .borderWidth(0.8)
              .backgroundColor(Color.Transparent)
              .fontColor($r('app.color.init_page_color'))
              .borderColor($r('app.color.init_page_color'))
              .onClick(() => {
                // TODO 退出应用
                this.context?.terminateSelf();
              })
            Button('同意').width('40%')
              .backgroundColor(Color.Transparent)
              .linearGradient({
                direction: GradientDirection.Bottom, // 渐变方向
                // repeating: true, // 渐变颜色是否重复
                colors: [['#00F5FF', 0.0], [$r('app.color.init_page_color'), 0.3]] // 数组末尾元素占比小于1时满足重复着色效果
              })
              .onClick(() => {
                // TODO 进入闪屏页面
                this.saveIsPrivacy()
                this.jumpToAdvertisingPage()
              })
          }
          .width(CommonConstants.COLUMN_WIDTH)
          .padding({ top: '10vp', bottom: '10vp' })
          .backgroundColor(Color.White)
        }
        .height(CommonConstants.COLUMN_HEIGHT)
        .padding({ top: this.topHeight, bottom: this.bottom })
        }
      GridCol({ span: { sm: 0, md: 0, lg: 2 } }) {}
    }
    .onBreakpointChange((breakpoints: string) => {
      this.breakPoint = breakpoints;
    })
  }
  .hideTitleBar(true)
}

通过以上核心功能的实现,开发者可以在HarmonyOS Next应用中创建一个功能完整的隐私政策页面。