HarmonyOS 弹窗的相关问题

在A页面,弹窗展示,通过弹窗的按钮点击跳转到B页面,从B页面back返回到A后,弹窗是消失的为什么?

HarmonyOS
2024-09-29 10:27:06
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

弹窗是会消失的,若是不想弹窗消失,可以使用Stack组件模拟实现Dialog的效果,页面跳转之后返回 可以做到Dialog依然显示的效果,代码如下所示:

import router from '@ohos.router';  
@Entry  
@Component  
struct First {  
  @State textValue: string  = 'Hello World'  
  // 显隐控制设置为不占用  
  @State visible: Visibility = Visibility.None  
  build() {  
    // 使用stack可以实现假的dialog覆盖原页面上面  
    Stack() {  
      Row() {  
        // 初始页面  
        Column() {  
          Text('Hello World')  
            .fontSize(50)  
            .fontWeight(FontWeight.Bold)  
          // 触发dialog的地方  
          Button('click')  
            .onClick(() => {  
              //用于检测点击事件是否透传到原来的页面,我测了一下是没有透传的,符合dialog规范  
              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() {  
        // 这个可以调节对话框效果,栅格布局,xs,sm,md,lg分别为四种规格  
        // 下面的breakpoints是用来区别当前属于哪个类型的断点  
        // gridRow里的栅格数量为总数,gridCol里的就是偏移和假Dialog所占据的栅格数  
        GridRow({  
          columns:{xs:1 ,sm: 4, md: 8, lg: 12},  
          breakpoints: { value: ["400vp", "600vp", "800vp"],  
            reference: BreakpointsReference.WindowSize },  
        })  
        {  
          GridCol({  
            span:{xs:1 ,sm: 2, md: 4, lg: 8},  
            offset:{xs:0 ,sm: 1, md: 2, lg: 2}  
          }){  
            // 这里放的就是原Dialog里的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/Second'  
                    })  
                  }).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.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
分享
微博
QQ
微信
回复
2024-09-29 18:11:14
相关问题
HarmonyOS dialog弹窗相关问题
1142浏览 • 1回复 待解决
HarmonyOS 关于弹窗相关问题
709浏览 • 1回复 待解决
自定义弹窗使用相关问题
1764浏览 • 1回复 待解决
HarmonyOS @Entry组件相关问题
771浏览 • 1回复 待解决
HarmonyOS AccountKit相关问题
1233浏览 • 1回复 待解决
HarmonyOS Grid相关问题
1267浏览 • 1回复 待解决
HarmonyOS KVStore 相关问题
1033浏览 • 1回复 待解决
HarmonyOS Worker相关问题
909浏览 • 1回复 待解决
HarmonyOS 线程相关问题
1327浏览 • 1回复 待解决
HarmonyOS string相关问题
938浏览 • 1回复 待解决
HarmonyOS BindSheet相关问题
1428浏览 • 1回复 待解决
HarmonyOS 证书相关问题
1048浏览 • 1回复 待解决
HarmonyOS Lib相关问题
717浏览 • 1回复 待解决
HarmonyOS 全局弹窗问题
1321浏览 • 1回复 待解决
HarmonyOS 弹窗异常问题
1309浏览 • 1回复 待解决
HarmonyOS 权限弹窗问题
906浏览 • 1回复 待解决
HarmonyOS 触摸手势相关问题
564浏览 • 1回复 待解决
HarmonyOS 自定义弹窗问题
1640浏览 • 1回复 待解决
HarmonyOS 地图相关问题
1553浏览 • 1回复 待解决
适配HarmonyOS相关问题
980浏览 • 1回复 待解决
HarmonyOS CardRecognition相关问题
935浏览 • 1回复 待解决
HarmonyOS 混淆相关问题
791浏览 • 1回复 待解决
HarmonyOS RelativeContainer相关问题
732浏览 • 1回复 待解决