鴻蒙(HarmonyOS)實(shí)現(xiàn)隱私政策彈窗效果
在實(shí)現(xiàn)用戶協(xié)議彈窗時(shí),通常我們會(huì)想到使用系統(tǒng)自定義彈窗,并在彈窗中點(diǎn)擊跳轉(zhuǎn)到Web頁面。但在HarmonyOS中,由于系統(tǒng)彈窗的顯示優(yōu)先級(jí)高于其他組件,即使跳轉(zhuǎn)到Web頁面,彈窗依然會(huì)顯示在最上層。
為了解決這個(gè)問題,我們可以自定義一個(gè)組件來模擬彈窗,這樣當(dāng)跳轉(zhuǎn)到Web頁面時(shí),Web內(nèi)容會(huì)覆蓋這個(gè)模擬的彈窗。
效果圖如下:
首先,我們來看程序的入口代碼。最外層使用了一個(gè)RelativeContainer容器組件,通過showAgreePrivacyPolicy變量控制隱私政策彈窗的顯示狀態(tài)。每次啟動(dòng)應(yīng)用時(shí),該變量默認(rèn)設(shè)置為true,以顯示隱私政策彈窗。在實(shí)際開發(fā)中,你可以通過preferences保存這個(gè)變量的狀態(tài)。
struct Index { @State showAgreePrivacyPolicy:boolean=true; build() { RelativeContainer(){ Row({space:10}){ Image($r('app.media.app_icon')).width(60).height(60) Column({space:5}){ Text($r('app.string.app_name')).fontSize(22).fontColor($r('app.color.title_color')) Text($r('app.string.launcher_tip')).fontSize(14).fontColor($r('app.color.other_color')) }.alignItems(HorizontalAlign.Start) }.alignRules({ bottom: {anchor: "__container__", align: VerticalAlign.Bottom}, middle: { anchor: '__container__', align: HorizontalAlign.Center } //以父容器為錨點(diǎn),水平方向居中對(duì)齊 }).margin({ bottom:30 }) if(this.showAgreePrivacyPolicy){//通過變量控制隱私政策彈窗是否顯示 PrivacyPolicyDialog({ cancel:this.onCancel.bind(this),//取消按鈕點(diǎn)擊 confirm:this.onAgree.bind(this),//確定按鈕點(diǎn)擊 }) } }.width('100%') .height('100%').backgroundColor($r('app.color.white')) } onCancel():void { (getContext(this) as common.UIAbilityContext)?.terminateSelf() } onAgree():void {//同意隱私政策 this.showAgreePrivacyPolicy = false; } }
接下來是PrivacyPolicyDialog組件代碼:
- 根組件填充100%寬高,設(shè)置黑色背景,透明度30%內(nèi)容區(qū)域使用Stack組件,背景為白色,圓角邊框,寬度占父組件的80%。
- 文字內(nèi)容用Text和Span組件包裹,支持滾動(dòng)功能。
- 點(diǎn)擊用戶協(xié)議和隱私政策的文字時(shí),使用router打開Web頁面。
@Component export default struct PrivacyPolicyDialog{ cancel!: () => void confirm!: () => void build() { Stack(){ Column() { Text($r('app.string.simple_user_policy')).fontSize(18).fontColor($r('app.color.title_color')).margin({ top: 30, bottom: 19 }) Scroll(){ Text(){ Span($r('app.string.privacy_policy_start')) Span($r('app.string.user_agreement_two')).fontColor($r('app.color.mainColor')).onClick(() => { this.openWebUrl("/useragreement.html"); }) Span($r('app.string.and')) Span($r('app.string.privacy_policy_two')).fontColor($r('app.color.mainColor')).onClick(() => { this.openWebUrl("/privacypolicy.html"); }) Span($r('app.string.simple_privacy_policy')) }.fontSize(16).fontColor($r('app.color.body_color')).margin({ left:25, right:25 }) }.height(120) Column(){ Button($r('app.string.disagree_privacy_policy')).onClick(() => { this.cancel(); }).fontColor($r('app.color.other_color')).fontSize(15).backgroundColor(Color.Transparent) Button($r('app.string.agree_privacy_policy')).onClick(() => { this.confirm(); }).fontColor($r('app.color.white')).fontSize(17) .linearGradient({ direction: GradientDirection.Right, colors:[[$r('app.color.start_main_color'),0.0],[$r('app.color.end_main_color'),1.0]] }).width('80%').margin({ top:15,bottom:21 }).borderRadius(24) } }.backgroundColor($r('app.color.white')).borderRadius(13).width('80%') }.width('100%') .height('100%').backgroundColor("#4D000000")//黑色背景 透明度約為 30% } openWebUrl(urlSuffix:string){ let url= "https://www.srzs.top"+urlSuffix; router.pushUrl({ url: 'pages/WebViewPage', params:{ data1: 'message', url: url // 傳遞的 URL 參數(shù) } }, router.RouterMode.Single) } }
WebViewPage展示W(wǎng)eb網(wǎng)頁的代碼就不貼出來了,大家可直接下載源碼。還有加載網(wǎng)頁一定要網(wǎng)絡(luò)權(quán)限,在entry/module.json5這個(gè)文件中配置。
源碼下載
到此這篇關(guān)于鴻蒙(HarmonyOS)實(shí)現(xiàn)隱私政策彈窗的文章就介紹到這了,更多相關(guān)鴻蒙隱私政策彈窗內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Prometheus和NodeExporter安裝監(jiān)控?cái)?shù)據(jù)說明
這篇文章主要為大家介紹了Prometheus和node?exporter安裝監(jiān)控?cái)?shù)據(jù)說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07聊聊Druid register mbean error的問題
這篇文章主要介紹了Druid register mbean error的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11JetBrains 學(xué)生認(rèn)證教程(Pycharm,IDEA… 等學(xué)生認(rèn)證教程)
這篇文章主要介紹了JetBrains 學(xué)生認(rèn)證教程(Pycharm,IDEA… 等學(xué)生認(rèn)證教程)文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09superset在linux和windows下的安裝和部署詳細(xì)教程
Superset 是 Airbnb開源的數(shù)據(jù)探查與可視化平臺(tái),是個(gè)輕量級(jí)的BI工具,開發(fā)者可以在其開源代碼上根據(jù)需要進(jìn)行二次開發(fā)。這篇文章主要介紹了superset在linux和windows下的安裝和部署詳細(xì)教程,需要的朋友可以參考下2020-10-10將新型冠狀病毒轉(zhuǎn)二進(jìn)制的代碼(首發(fā))
這篇文章主要介紹了新型冠狀病毒轉(zhuǎn)二進(jìn)制的相關(guān)知識(shí),分為java,js,php,pthon等語言的實(shí)例代碼,需要的朋友可以參考下2020-02-02