本文原创发布在华为开发者社区。
介绍
本示例使用\@CustomDialog实现隐私弹窗,使用AppStorage+PersistentStorage保存用户是否同意。
效果预览
使用说明
- 用户进入应用弹出隐私弹窗,在点击同意后进入应用首页。
- 点击不同意弹出温馨提示弹窗,再次请求用户同意隐私保护协议。点击同意进入应用首页。
点击仍不同意后弹出需要同意隐私协议才能再次使用应用。点击再次查看回到温馨提示弹窗,点击退出应用结束使用。
实现思路
使用\@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)
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。