在duyu200上体验eTS-警告弹窗

144 阅读2分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第12天,点击查看活动详情

今天开发板到手了,于是想体验一些新的东西。于是就从弹窗做起。

显示警告弹窗组件,可设置文本内容与响应回调。 从API Version 7开始支持

属性

名称参数类型默认值参数描述
showoptions: { paramObject1paramObject2}-定义并显示AlertDialog组件

paramObject1参数

参数名参数类型必填默认值参数描述
titlestring-弹窗标题。
messagestring-弹窗内容。
autoCancelbooleantrue点击遮障层时,是否关闭弹窗。
confirm{ value: string, fontColor?: Colornumberstring, backgroundColor?: Colornumberstring, action: () => void }-确认按钮的文本内容、文本色、按钮背景色和点击回调。
cancel() => void-点击遮障层关闭dialog时的回调。
alignmentDialogAlignmentDialogAlignment.Default弹窗在竖直方向上的对齐方式。
offset{ dx: Length, dy: Length}-弹窗相对alignment所在位置的偏移量。
gridCountnumber-弹窗容器宽度所占用栅格数。

paramObject2参数

参数名参数类型必填默认值参数描述
titlestring-弹窗标题。
messagestring-弹窗内容。
autoCancelbooleantrue点击遮障层时,是否关闭弹窗。
primaryButton{ value: stringfontColor?: Colornumberstring, backgroundColor?: Colornumberstring, action: () => void; }-按钮的文本内容、文本色、按钮背景色和点击回调。
secondaryButton{ value: string, fontColor?: Colornumberstring, backgroundColor?: Colornumberstring, action: () => void; }-按钮的文本内容、文本色、按钮背景色和点击回调。
cancel() => void-点击遮障层关闭dialog时的回调。
alignmentDialogAlignmentDialogAlignment.Default弹窗在竖直方向上的对齐方式。
offset{ dx: Length, dy: Length}-弹窗相对alignment所在位置的偏移量。
gridCountnumber-弹窗容器宽度所占用栅格数。

案例

@Entry
@Component
struct AlertDialogExample {
  build() {
    Column({ space: 50}) {
      Button('one button')
        .onClick(() => {
          AlertDialog.show(
            {
              title: '弹窗标题',
              message: '弹窗内容',
              autoCancel:false,
              confirm: {
                value: '确认按钮的文本内容',
​
                action: () => {
                  console.info('Button-clicking callback')
                }
              },
              cancel: () => {
                console.info('Closed callbacks')
              }
            }
          )
        })
        .backgroundColor(0x317aff)
      Button('two button ')
        .onClick(() => {
          AlertDialog.show(
            {
              title: 'title',
              message: 'text',
              primaryButton: {
                value: 'cancel',
                action: () => {
​
                  console.info('Callback when the first button is clicked')
                }
              },
              secondaryButton: {
                value: 'ok',
                action: () => {
                  console.info('Callback when the second button is clicked')
                }
              },
              cancel: () => {
                console.info('Closed callbacks')
              }
            }
          )
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 500 })
  }
}

效果图:

image-20220414222020931

image-20220414221934325

image-20220414221954348

然后我们可以运行在真机上。

image-20220414222102122