HarmonyOS 在claas或者function里面想要拉起自定义的弹出框,这个要怎么实现,自带的alert可以用,自定义的提示不能使用

在claas或者function里面想要拉起自定义的弹出框,这个要怎么实现,自带的alert可以用,自定义的提示不能使用

HarmonyOS
2024-12-20 17:25:18
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

请参考以下代码示例:主页面

//在@entry中先设置builder,再直接调用showTest即可
import { customDialogBuilder, changeDialogBuilder, MyShowTest } from ‘…/pages/Page’
let myShowTest = new MyShowTest()
@Entry
@Component
struct Index {
  @State message: string = ‘Hello World’
  onPageShow(): void {
    changeDialogBuilder(customDialogBuilder.bind(this))
  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            myShowTest.showTest()
          })
      }
      .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.
  • 24.
  • 25.
  • 26.

自定义class类

// 自定义弹窗.ets
import promptAction from ‘@ohos.promptAction’
let myDialogBuilder: CustomBuilder;
let customDialogId: number = 0

@Builder
export function customDialogBuilder() {
  Column() {
    Text(‘Custom dialog Message’).fontSize(10)
    Row() {
      Button(“确认”).onClick(() => {
        promptAction.closeCustomDialog(customDialogId)
      })
      Blank().width(50)
      Button(“取消”).onClick(() => {
        promptAction.closeCustomDialog(customDialogId)
      })
    }
  }.height(200).padding(5)
}

export function changeDialogBuilder(builder: CustomBuilder) {
  myDialogBuilder = builder
}

export class MyShowTest{
  showTest() {
    if (myDialogBuilder === undefined) {
      return
    }
    promptAction.openCustomDialog({
      builder: myDialogBuilder
    }).then((dialogId: number) => {
      customDialogId = dialogId
    })
  }
}
  • 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.

改变customDialogBuilder() 为如下代码(Column 增加backGroundColor 和 width 属性),可自定义弹窗背景颜色

@Builder
export function customDialogBuilder() {
  Column() {
    Text(‘Custom dialog Message’).fontSize(10)
    Row() {
      Button(“确认”).onClick(() => {
        promptAction.closeCustomDialog(customDialogId)
      })
      Blank().width(50)
      Button(“取消”).onClick(() => {
        promptAction.closeCustomDialog(customDialogId)
      })
    }
  }.height(200).padding(5).backgroundColor(Color.Green).width(‘100%’)
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

使用backgroundImage和backgroundImageSize(ImageSize.Cover) 替换 backgroundColor 可达到类似的效果,但是弹窗的宽度大小现在不能改变

分享
微博
QQ
微信
回复
2024-12-20 19:53:07
相关问题
HarmonyOS 自定义不能全屏
903浏览 • 1回复 待解决
HarmonyOS 自定义键盘不能顶起输入
1225浏览 • 1回复 待解决
自定义如何在UIAbility中弹出
232浏览 • 0回复 待解决
自定义状态获取
1573浏览 • 1回复 待解决
JAVA卡片怎么自定义组件?
7540浏览 • 1回复 待解决
是否可以自定义权限弹文字
2518浏览 • 1回复 待解决
鸿蒙怎么实现自定义布局Dialog
10082浏览 • 2回复 已解决
HarmonyOS 自定义组件使用
776浏览 • 1回复 待解决
HarmonyOS 如何设置自定义颜色
685浏览 • 1回复 待解决