HarmonyOS 当前页面需要打开一个类似于模态的半屏蒙版,层级怎么设置

当前页面需要打开一个类似于模态的半屏蒙版,层级怎么设置,例如点击蒙版上的按钮push到一个新页面,新页面会再蒙版的下方被蒙版遮挡,用什么方案可以再push后,蒙版再页面的栈下面。

HarmonyOS
2024-12-25 15:02:33
633浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

想要实现跳的时候和正常页面一样跳,返回后弹窗还在,且支持一下动态刷新自定义弹框的参数,可以通过Stack布局来模拟实现Dialog的效果,同时可通过状态变量动态刷新数据。参考代码如下:

// DialogFirst.ets
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/Index2'
                })
              }).backgroundColor(0xffffff).fontColor(Color.Red)
          }.margin({ bottom: 10 })
        }
        .backgroundColor(0xffffff)
        .visibility(this.visible)
        .clip(true)
        .borderRadius(20)
      }.width('95%') //设置弹窗宽度
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
// Index2.ets
import { router } from '@kit.ArkUI';

@Entry
@Component
struct Index2 {
  @State message: string = '点击返回';

  build() {
    Column() {
      Button(this.message)
        .fontSize(50)
        .fontWeight(FontWeight.Bold).onClick(() => {
        router.back({
          url: 'dialog/DialogFirst',
          params: {
            info: '隐私已阅读'
          }
        });
      })
    }.width('100%').height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

同时设置弹框autoCancel: false,可以做到点击遮障层弹框不退出。

分享
微博
QQ
微信
回复
2024-12-25 16:25:13


相关问题
HarmonyOS 类似于andServer插件替代
529浏览 • 1回复 待解决
如何展示一个广告
1456浏览 • 1回复 待解决
如何新开一个半透明页面
769浏览 • 1回复 待解决
HarmonyOS 有没有类似于渐变效果
637浏览 • 1回复 待解决
鸿蒙-如何实现类似于HOME键功能
11568浏览 • 2回复 待解决