harmony 鸿蒙Alert Dialog Box

2022-08-09 浏览 (943)

Alert Dialog Box

You can set the text content and response callback for an alert dialog box.

NOTE

The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.

The functionality of this module depends on UI context. This means that the APIs of this module cannot be used where the UI context is unclear. For details, see UIContext.

Since API version 10, you can use the showAlertDialog API in UIContext to obtain the UI context.

Attributes

NameTypeDescription
showAlertDialogParamWithConfirm |AlertDialogParamWithButtons |AlertDialogParamWithOptionsDefines and displays the <AlertDialog> component.

AlertDialogParamWithConfirm

NameTypeMandatoryDescription
titleResourceStrNoTitle of the dialog box.
subtitle10+ResourceStrNoSubtitle of the dialog box.
messageResourceStrYesContent of the dialog box.
autoCancelbooleanNoWhether to close the dialog box when the overlay is clicked.
Default value: true
confirm{
enabled10+?: boolean,
defaultFocus10+?: boolean,
style10+?: DialogButtonStyle,
value: ResourceStr,
fontColor?: ResourceColor,
backgroundColor?: ResourceColor,
action: () => void
}
NoInformation about the confirm button.
enabled: whether to respond when the button is clicked.
Default value: true
defaultFocus: whether the button is the default focus.
Default value: false
style: button style.
Default value: DialogButtonStyle.DEFAULT
value: text of the button.
fontColor: font color of the button.
backgroundColor: background color of the button.
action: callback when the button is selected.
cancel() => voidNoCallback invoked when the dialog box is closed after the overlay is clicked.
alignmentDialogAlignmentNoAlignment mode of the dialog box in the vertical direction.
Default value: DialogAlignment.Default
offsetOffsetNoOffset of the dialog box relative to the alignment position.
Default value: { dx: 0 , dy: 0 }
gridCountnumberNoNumber of grid columns occupied by the width of the dialog box.
Default value: 4
maskRect10+RectangleNoMask area of the dialog box. Events outside the mask area are transparently transmitted, and events within the mask area are not.
Default value: { x: 0, y: 0, width: '100%', height: '100%' }

Priorities of the confirm parameters: fontColor and backgroundColor > style > defaultFocus

backgroundColorfontColorstyledefaultFocusEffect
GreenRed--Red text on green background
Green-DialogButtonStyle.HIGHLIGHT-White text on green background
Green-DialogButtonStyle.DEFAULT-Blue text on green background
Green--TRUEWhite text on green background
Green--FALSE/-Blue text on green background
-RedDialogButtonStyle.HIGHLIGHT-Red text on blue background
-RedDialogButtonStyle.DEFAULT-Red text on white background
-Red-TRUERed text on blue background
-Red-FALSE/-Red text on white background
--DialogButtonStyle.HIGHLIGHT-White text on blue background
--DialogButtonStyle.DEFAULT-Blue text on white background
---TRUEWhite text on blue background
---FALSE/-Blue text on white background

AlertDialogParamWithButtons

NameTypeMandatoryDescription
titleResourceStrNoTitle of the dialog box.
subtitle10+ResourceStrNoSubtitle of the dialog box.
messageResourceStrYesContent of the dialog box.
autoCancelbooleanNoWhether to close the dialog box when the overlay is clicked.
Default value: true
primaryButton{
enabled10+?: boolean,
defaultFocus10+?: boolean,
style10+?: DialogButtonStyle,
value: ResourceStr,
fontColor?: ResourceColor,
backgroundColor?: ResourceColor,
action: () => void;
}
NoInformation about the confirm button.
enabled: whether to respond when the button is clicked.
Default value: true
defaultFocus: whether the button is the default focus.
Default value: false
style: button style.
Default value: DialogButtonStyle.DEFAULT
value: text of the button.
fontColor: font color of the button.
backgroundColor: background color of the button.
action: callback when the button is selected.
secondaryButton{
enabled10+?: boolean,
defaultFocus10+?: boolean,
style10+?: DialogButtonStyle,
value: ResourceStr,
fontColor?: ResourceColor,
backgroundColor?: ResourceColor,
action: () => void;
}
NoInformation about the confirm button.
enabled: whether to respond when the button is clicked.
Default value: true
defaultFocus: whether the button is the default focus.
Default value: false
style: button style.
Default value: DialogButtonStyle.DEFAULT
value: text of the button.
fontColor: font color of the button.
backgroundColor: background color of the button.
action: callback when the button is selected.
cancel() => voidNoCallback invoked when the dialog box is closed after the overlay is clicked.
alignmentDialogAlignmentNoAlignment mode of the dialog box in the vertical direction.
Default value: DialogAlignment.Default
offsetOffsetNoOffset of the dialog box relative to the alignment position.
gridCountnumberNoNumber of grid columns occupied by the width of the dialog box.
maskRect10+RectangleNoMask area of the dialog box. Events outside the mask area are transparently transmitted, and events within the mask area are not.
Default value: { x: 0, y: 0, width: '100%', height: '100%' }

AlertDialogParamWithOptions10+

NameTypeMandatoryDescription
titleResourceStrNoTitle of the dialog box.
subtitle10+ResourceStrNoSubtitle of the dialog box.
messageResourceStrYesContent of the dialog box.
autoCancelbooleanNoWhether to close the dialog box when the overlay is clicked.
Default value: true
cancel() => voidNoCallback invoked when the dialog box is closed after the overlay is clicked.
alignmentDialogAlignmentNoAlignment mode of the dialog box in the vertical direction.
Default value: DialogAlignment.Default
offsetOffsetNoOffset of the dialog box relative to the alignment position.
gridCountnumberNoNumber of grid columns occupied by the width of the dialog box.
maskRect10+RectangleNoMask area of the dialog box. Events outside the mask area are transparently transmitted, and events within the mask area are not.
Default value: { x: 0, y: 0, width: '100%', height: '100%' }
buttons10+Array<AlertDialogButtonOptions>NoButtons in the dialog box.
buttonDirection10+DialogButtonDirectionNoDirection in which buttons are laid out.
Default value: DialogButtonDirection.AUTO
When there are more than three buttons, the Auto mode (which automatically switches to the vertical layout when there are more than two buttons) is recommended. In non-Auto mode, buttons that extend beyond the display area are clipped.

AlertDialogButtonOptions10+

NameTypeMandatoryDescription
enabledbooleanNoWhether to respond when the button is clicked.
Default value: true
defaultFocusbooleanNoWhether the button is the default focus.
Default value: false
styleDialogButtonStyleNoStyle of the button.
Default value: DialogButtonStyle.DEFAULT
valueResourceStrYesText of the button. If the value is null, the button is not displayed.
fontColorResourceColorNoFont color of the button.
backgroundColorResourceColorNoBackground color of the button.
action() => voidYesCallback when the button is selected.

DialogButtonDirection10+

NameDescription
AUTOButtons are laid out horizontally when there are two or fewer buttons and vertically otherwise.
HORIZONTALButtons are laid out horizontally.
VERTICALButtons are laid out vertically.

Priorities of the confirm parameters: fontColor and backgroundColor > style > defaultFocus

backgroundColorfontColorstyledefaultFocusEffect
GreenRed--Red text on green background
Green-DialogButtonStyle.HIGHLIGHT-White text on green background
Green-DialogButtonStyle.DEFAULT-Blue text on green background
Green--TRUEWhite text on green background
Green--FALSE/-Blue text on green background
-RedDialogButtonStyle.HIGHLIGHT-Red text on blue background
-RedDialogButtonStyle.DEFAULT-Red text on white background
-Red-TRUERed text on blue background
-Red-FALSE/-Red text on white background
--DialogButtonStyle.HIGHLIGHT-White text on blue background
--DialogButtonStyle.DEFAULT-Blue text on white background
---TRUEWhite text on blue background
---FALSE/-Blue text on white background

DialogAlignment

NameDescription
TopVertical top alignment.
CenterVertical center alignment.
BottomVertical bottom alignment.
DefaultDefault alignment.
TopStart8+Top left alignment.
TopEnd8+Top right alignment.
CenterStart8+Center left alignment.
CenterEnd8+Center right alignment.
BottomStart8+Bottom left alignment.
BottomEnd8+Bottom right alignment.

Rectangle10+

The Rectangle type is used to represent a mask area of a dialog box.

NameTypeMandatoryDescription
xLengthNoX coordinate of the mask area of the dialog box relative to the upper left corner of the window.
Default value: 0vp
yLengthNoY coordinate of the mask area of the dialog box relative to the upper left corner of the window.
Default value: 0vp
widthLengthNoWidth of the mask area of the dialog box.
Default value: '100%'
heightLengthNoHeight of the mask area of the dialog box.
Default value: '100%'

NOTE

x and y can be set to a positive or negative percentage value. When x is set to '100%', the mask area is moved rightward by the window width. When x is set to '-100%', the mask area is moved leftward by the window width. When y is set to '100%', the mask area is moved downward by the window height. When y is set to '-100%', the mask area is moved upward by the window height.

width and height can be set in percentage and can only be set to positive values. If they are set to negative values, the default values are used instead.

The percentage is calculated based on the width and height of the window.

DialogButtonStyle10+

NameDescription
DEFAULTBlue text on white background (black background under the dark theme).
HIGHLIGHTWhite text on blue background.

Example

// xxx.ets
@Entry
@Component
struct AlertDialogExample {
  build() {
    Column({ space: 5 }) {
      Button('one button dialog')
        .onClick(() => {
          AlertDialog.show(
            {
              title: 'title',
              message: 'text',
              autoCancel: true,
              alignment: DialogAlignment.Bottom,
              offset: { dx: 0, dy: -20 },
              gridCount: 3,
              confirm: {
                value: 'button',
                action: () => {
                  console.info('Button-clicking callback')
                }
              },
              cancel: () => {
                console.info('Closed callbacks')
              }
            }
          )
        })
        .backgroundColor(0x317aff)
      Button('two button dialog')
        .onClick(() => {
          AlertDialog.show(
            {
              title: 'title',
              subtitle: 'subtitle',
              message: 'text',
              autoCancel: true,
              alignment: DialogAlignment.Bottom,
              gridCount: 4,
              offset: { dx: 0, dy: -20 },
              primaryButton: {
                value: 'cancel',
                action: () => {
                  console.info('Callback when the first button is clicked')
                }
              },
              secondaryButton: {
                enabled: true,
                defaultFocus: true,
                style: DialogButtonStyle.HIGHLIGHT,
                value: 'ok',
                action: () => {
                  console.info('Callback when the second button is clicked')
                }
              },
              cancel: () => {
                console.info('Closed callbacks')
              }
            }
          )
        }).backgroundColor(0x317aff)
        Button('three button dialog')
        .onClick(() => {
          AlertDialog.show(
            {
              title: 'title',
              subtitle: 'subtitle',
              message: 'text',
              autoCancel: true,
              alignment: DialogAlignment.Bottom,
              gridCount: 4,
              offset: { dx: 0, dy: -20 },
              buttonDirection: DialogButtonDirection.HORIZONTAL,
              buttons: [
                {
                  value: 'Button',
                  action: () => {
                    console.info('Callback when button1 is clicked')
                  }
                },
                {
                  value: 'Button',
                  action: () => {
                    console.info('Callback when button2 is clicked')
                  }
                },
                {
                  value: 'Button',
                  enabled: true,
                  defaultFocus: true,
                  style: DialogButtonStyle.HIGHLIGHT,
                  action: () => {
                    console.info('Callback when button3 is clicked')
                  }
                },
              ],
              cancel: () => {
                console.info('Closed callbacks')
              }
            }
          )
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}

en-us_image_alert

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS-based Declarative Development Paradigm

harmony 鸿蒙@ohos.multimedia.avCastPicker (AVCastPicker)

harmony 鸿蒙Property Animation

harmony 鸿蒙Enums

harmony 鸿蒙Blank

harmony 鸿蒙Button

harmony 鸿蒙CalendarPicker

harmony 鸿蒙Checkbox

harmony 鸿蒙CheckboxGroup

harmony 鸿蒙DataPanel

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/3d911a3bbf264cb3841ef9b6c9a26daf