import { window } from '@kit.ArkUI'

@Entry
@Component
struct GuanggaoDemo {
  @State
  isShow: boolean = false
  @State
  isTimeStart: boolean = false
  @State
  timeShow: number = 5
  timer: number = 0

  aboutToAppear(): void {
    window.getLastWindow(getContext()).then((win) => {
      win.setWindowLayoutFullScreen(true)
    })
    this.setTime()
  }

  setTime() {
    this.isShow = true
    this.isTimeStart = true
    this.timeShow = 5
    if (this.isTimeStart) {
       this.timer = setInterval(() => {
        if (this.timeShow <= 0) {
          clearInterval(this.timer)
          this.isShow = false
          return
        } else {
          this.timeShow--
        }
      }, 1000)
    } else {
      this.timeShow = 5
    }
  }

  @Builder
  Guanggao() {
    Column() {
      Row() {

        Text(this.timeShow + "")
          .width(60)
          .borderRadius(30)
          .backgroundColor(Color.Blue)
          .fontColor(Color.White)
          .textAlign(TextAlign.Center)
          .onClick(() => {
            this.isShow = false
            clearInterval(this.timer)
          })
      }.width('100%').justifyContent(FlexAlign.End)

      .padding({
        top: 50,
        right: 40
      })

    }.width('100%').height('100%').backgroundColor(Color.Pink)
  }

  build() {
    Column() {
      Button("显示广告")
        .onClick(() => {
          this.setTime()
        })
    }
    .height('100%')
    .width('100%')
    .justifyContent(FlexAlign.Center)
    .bindContentCover($$this.isShow, this.Guanggao(), {
      modalTransition: ModalTransition.ALPHA
    })
  }
}

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐