HarmonyOS-ArkUI固定样式弹窗(1)

发布于:2025-06-03 ⋅ 阅读:(32) ⋅ 点赞:(0)

固定样式弹窗指的就是ArkUI中为我们提供的一些具备界面模板性质的弹窗。样式是固定的,我们可以决定在这些模板里输入什么样的内容。常见的有,警告弹窗, 列表选择弹窗, 选择器弹窗,对话框,操作菜单。

下图是本文中要讲到的基类固定样式弹窗,其中选择器弹窗没有包含在内,需要开一篇新的文章专门描述。

警告弹窗

需要向用户提问或者得到用户许可的时候,可以使用警告弹窗。

  • 警告弹窗用于提示重要信息,但会中断当前用户对界面的点击操作。尽量提必要的信息和有用的操作。
  • 避免仅使用警告弹窗提供信息,用户不喜欢被信息丰富但不可操作的警告打断。

API

通过 UIContext中的showAlertDialog接口来实现。 详情请参考下方代码!

API图示

代码案例

@Entry
@Component
struct DialogsTest1 {
  @State time:string = '记时'
  alertDialog:string = '告警弹窗'
  second: number = 0

  aboutToAppear(): void {
     setInterval(()=>{
       this.time = `记时: ${this.convertSeconds(this.second++)}`
     }, 1000)
  }

  /**
   * 获取累计时长
   * @param seconds 累计的秒数
   * @returns
   */
  convertSeconds(seconds: number) : string {
    if (typeof  this.second !== 'number' || this.second < 0) {
      return 'error'
    }
    const h = Math.floor(this.second / 3600)
    const m = Math.floor((this.second % 3600) / 60)
    const s = Math.floor(this.second % 60)
    return `${h} : ${m} : ${s}`
  }

  build() {
    RelativeContainer() {
      Text(this.time)
        .id('time')
        .fontSize($r('app.float.page_text_font_size'))
        .fontWeight(FontWeight.Bold)
        .alignRules({
          top: { anchor: '__container__', align: VerticalAlign.Top},
          start: {anchor: '__container__', align: HorizontalAlign.Start},
          end: {anchor: '__container__', align: HorizontalAlign.End},
        })

      Text(this.alertDialog)
        .id('

网站公告

今日签到

点亮在社区的每一天
去签到