HarmonyOS 如何实现弹窗跳转之后返回还在

HarmonyOS
2024-12-20 17:38:07
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

弹窗目前暂不支持点击跳转之后返回,弹窗还在。想要实现跳的时候希望和正常页面一样跳,返回后弹窗还在,且支持一下动态刷新自定义弹框的参数,可以通过Stack布局来模拟实现Dialog的效果,同时可通过状态变量动态刷新数据。

代码如下:

import { router } from '@kit.ArkUI';
@Entry
@Component
struct DialogFirst {
  @State textValue: string = '请阅读隐私协议'
  // 显隐控制设置为不占用
  @State visible: Visibility = Visibility.None

  onPageShow() {
    const params = router.getParams() as Record<string, string>; // 获取传递过来的参数对象
    if (params) {
      this.textValue = params.info as string; // 获取info属性的值
    }
  }

  build() {
    Stack() {
      // 初始页面
      Row() {
        Column() {
          Text('Hello World')
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
          Button('click')
            .onClick(() => {
              console.log("hit me!")
              if (this.visible == Visibility.Visible) {
                this.visible = Visibility.None
              } else {
                this.visible = Visibility.Visible
              }
            })
            .backgroundColor(0x777474)
            .fontColor(0x000000)
        }
        .width('100%')
      }
      .height('100%')
      .backgroundColor(0x885555)

      //加了一个半透明灰色的蒙层效果
      Text('')
        .onClick(() => {
          if (this.visible == Visibility.Visible) {
            this.visible = Visibility.None
          } else {
            this.visible = Visibility.Visible
          }
        })
        .width('100%')
        .height('100%')
        .opacity(0.16)
        .backgroundColor(0x000000)
        .visibility(this.visible)
      Column() {
        Column() {
          Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
          TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
            .onChange((value: string) => {
              this.textValue = value
            })
          Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
          Flex({ justifyContent: FlexAlign.SpaceAround }) {
            Button('cancel')
              .onClick(() => {
                if (this.visible == Visibility.Visible) {
                  this.visible = Visibility.None
                } else {
                  this.visible = Visibility.Visible
                }

              }).backgroundColor(0xffffff).fontColor(Color.Black)
            Button('jump')
              .onClick(() => {
                router.pushUrl({
                  url: 'pages/DialogSecond'
                })
              }).backgroundColor(0xffffff).fontColor(Color.Red)
          }.margin({ bottom: 10 })
        }
        .backgroundColor(0xffffff)
        .visibility(this.visible)
        .clip(true)
        .borderRadius(20)
      }.width('95%') //设置弹窗宽度
    }
  }
}


import { router } from '@kit.ArkUI';

@Entry
@Component
struct DialogSecond {
  @State message: string = '点击返回';
  build() {
    Column() {
      Button(this.message)
        .fontSize(50)
        .fontWeight(FontWeight.Bold).onClick(() => {
        router.back({
          url: 'pages/DialogFirst',
          params: {
            info: '隐私已阅读'
          }
        });
      })
    }.width('100%').height('100%')
  }
}
 

import { BusinessError } from '@ohos.base';
import { ComponentContent } from "@ohos.ArkUI.node";

class Params {
  text: string = ""
  constructor(text: string) {
    this.text = text;
  }
}
@Builder
function buildText(params: Params) {
  Column() {
    Text(params.text)
      .fontSize(50)
      .fontWeight(FontWeight.Bold)
      .margin({bottom: 36})
  }.backgroundColor('#FFF0F0F0')
}
@Entry
@Component
struct Index {
  @State message: string = "hello"
  build() {
    Row() {
      Column() {
        Button("click me")
          .onClick(() => {
            let uiContext = this.getUIContext();
            let promptAction = uiContext.getPromptAction();
            let contentNode = new ComponentContent(uiContext, wrapBuilder(buildText), new Params(this.message));
            try {
              promptAction.openCustomDialog(contentNode);
            } catch (error) {
              let message = (error as BusinessError).message;
              let code = (error as BusinessError).code;
            };
            setTimeout(() => {
              try {
                promptAction.updateCustomDialog(contentNode, { alignment: DialogAlignment.CenterEnd });
              } catch (error) {
                let message = (error as BusinessError).message;
                let code = (error as BusinessError).code;
              };
            }, 2000);   //2&#31186;&#21518;&#33258;&#21160;&#26356;&#26032;&#24377;&#31383;&#20301;&#32622;
          })
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
2024-12-20 19:11:18
相关问题
弹窗跳转到页面后返回弹窗不消失
2951浏览 • 1回复 待解决
HarmonyOS 弹窗如何拦截返回
1028浏览 • 1回复 待解决
HarmonyOS dialog弹窗跳转问题
871浏览 • 1回复 待解决
HarmonyOS router跳转返回如何传参
1029浏览 • 1回复 待解决
HarmonyOS PageA跳转PageB,弹窗消失
938浏览 • 1回复 待解决
HarmonyOS 权限弹窗如何实现
1640浏览 • 1回复 待解决
请求成功之后返回SSL is null
1821浏览 • 1回复 待解决
HarmonyOS 申请权限返回是否弹窗结果
1057浏览 • 1回复 待解决