实现隐私弹窗鸿蒙示例代码

鸿蒙场景化示例代码技术工程师
发布于 2025-3-6 16:34
3115浏览
0收藏

本文原创发布在华为开发者社区

介绍

本示例使用@CustomDialog实现隐私弹窗,使用AppStorage+PersistentStorage保存用户是否同意。

实现隐私弹窗源码链接

效果预览

实现隐私弹窗鸿蒙示例代码-鸿蒙开发者社区

使用说明

  1. 用户进入应用弹出隐私弹窗,在点击同意后进入应用首页。
  2. 点击不同意弹出温馨提示弹窗,再次请求用户同意隐私保护协议。点击同意进入应用首页。
  3. 点击仍不同意后弹出需要同意隐私协议才能再次使用应用。点击再次查看回到温馨提示弹窗,点击退出应用结束使用。

实现思路

使用@CustomDialog实现隐私弹窗,使用AppStorage+PersistentStorage保存用户是否同意。核心代码如下:


Button('同意')
  .margin(5)
  .width(250)
  .backgroundColor($r('app.color.button_back_color'))
  .fontColor($r('sys.color.comp_background_list_card'))
  .onClick(() => {
    if (this.controller != undefined) {
      this.controller.close()
    }
    // 存储用户同意
    AppStorage.setOrCreate('isAgreed', true)
    PersistentStorage.persistProp('isAgreed', true)
    router.replaceUrl({ url: 'pages/MainPage' });
  })
  .height(40)


  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

分类
收藏
回复
举报


回复
    相关推荐