当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
1.4K Star 7.4K Fork 8.2K

OpenHarmony/docs
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
docker
en
zh-cn
application-dev
IDL
ability-deprecated
application-models
application-test
connectivity
database
device-usage-statistics
device
dfx
faqs
file-management
internationalization
key-features/multi-device-app-dev
media
napi
notification
public_sys-resources
quick-start
reference
apis
arkui-js
arkui-ts
figures
public_sys-resources
Readme-CN.md
ts-animatorproperty.md
ts-appendix-enums.md
ts-basic-components-blank.md
ts-basic-components-button.md
ts-basic-components-checkbox.md
ts-basic-components-checkboxgroup.md
ts-basic-components-datapanel.md
ts-basic-components-datepicker.md
ts-basic-components-divider.md
ts-basic-components-gauge.md
ts-basic-components-image.md
ts-basic-components-imageanimator.md
ts-basic-components-loadingprogress.md
ts-basic-components-marquee.md
ts-basic-components-navdestination.md
ts-basic-components-navigation.md
ts-basic-components-navrouter.md
ts-basic-components-patternlock.md
ts-basic-components-plugincomponent.md
ts-basic-components-progress.md
ts-basic-components-qrcode.md
ts-basic-components-radio.md
ts-basic-components-rating.md
ts-basic-components-remotewindow.md
ts-basic-components-richtext.md
ts-basic-components-scrollbar.md
ts-basic-components-search.md
ts-basic-components-select.md
ts-basic-components-slider.md
ts-basic-components-span.md
ts-basic-components-stepper.md
ts-basic-components-stepperitem.md
ts-basic-components-text.md
ts-basic-components-textarea.md
ts-basic-components-textclock.md
ts-basic-components-textinput.md
ts-basic-components-textpicker.md
ts-basic-components-texttimer.md
ts-basic-components-timepicker.md
ts-basic-components-toggle.md
ts-basic-components-web.md
ts-basic-components-xcomponent.md
ts-basic-gestures-longpressgesture.md
ts-basic-gestures-pangesture.md
ts-basic-gestures-pinchgesture.md
ts-basic-gestures-rotationgesture.md
ts-basic-gestures-swipegesture.md
ts-basic-gestures-tapgesture.md
ts-canvasrenderingcontext2d.md
ts-combined-gestures.md
ts-components-canvas-canvas.md
ts-components-canvas-canvasgradient.md
ts-components-canvas-imagebitmap.md
ts-components-canvas-imagedata.md
ts-components-canvas-lottie.md
ts-components-canvas-path2d.md
ts-components-summary.md
ts-container-ability-component.md
ts-container-alphabet-indexer.md
ts-container-badge.md
ts-container-column.md
ts-container-columnsplit.md
ts-container-counter.md
ts-container-flex.md
ts-container-flowitem.md
ts-container-grid.md
ts-container-gridcol.md
ts-container-gridcontainer.md
ts-container-griditem.md
ts-container-gridrow.md
ts-container-list.md
ts-container-listitem.md
ts-container-listitemgroup.md
ts-container-navigator.md
ts-container-panel.md
ts-container-refresh.md
ts-container-relativecontainer.md
ts-container-row.md
ts-container-rowsplit.md
ts-container-scroll.md
ts-container-sidebarcontainer.md
ts-container-stack.md
ts-container-swiper.md
ts-container-tabcontent.md
ts-container-tabs.md
ts-container-waterflow.md
ts-drawing-components-circle.md
ts-drawing-components-ellipse.md
ts-drawing-components-line.md
ts-drawing-components-path.md
ts-drawing-components-polygon.md
ts-drawing-components-polyline.md
ts-drawing-components-rect.md
ts-drawing-components-shape.md
ts-explicit-animation.md
ts-gesture-settings.md
ts-media-components-video.md
ts-methods-action-sheet.md
ts-methods-alert-dialog-box.md
ts-methods-custom-dialog-box.md
ts-methods-datepicker-dialog.md
ts-methods-menu.md
ts-methods-textpicker-dialog.md
ts-methods-timepicker-dialog.md
ts-motion-path-animation.md
ts-offscreencanvasrenderingcontext2d.md
ts-page-transition-animation.md
ts-pixel-units.md
ts-state-management.md
ts-transition-animation-component.md
ts-transition-animation-shared-elements.md
ts-types.md
ts-universal-attributes-background.md
ts-universal-attributes-backgroundBlurStyle.md
ts-universal-attributes-border-image.md
ts-universal-attributes-border.md
ts-universal-attributes-click.md
ts-universal-attributes-component-id.md
ts-universal-attributes-enable.md
ts-universal-attributes-flex-layout.md
ts-universal-attributes-focus.md
ts-universal-attributes-gradient-color.md
ts-universal-attributes-grid.md
ts-universal-attributes-hit-test-behavior.md
ts-universal-attributes-hover-effect.md
ts-universal-attributes-image-effect.md
ts-universal-attributes-layout-constraints.md
ts-universal-attributes-location.md
ts-universal-attributes-menu.md
ts-universal-attributes-opacity.md
ts-universal-attributes-overlay.md
ts-universal-attributes-polymorphic-style.md
ts-universal-attributes-popup.md
ts-universal-attributes-restoreId.md
ts-universal-attributes-sharp-clipping.md
ts-universal-attributes-size.md
ts-universal-attributes-text-style.md
ts-universal-attributes-touch-target.md
ts-universal-attributes-transformation.md
ts-universal-attributes-visibility.md
ts-universal-attributes-z-order.md
ts-universal-component-area-change-event.md
ts-universal-component-visible-area-change-event.md
ts-universal-events-click.md
ts-universal-events-drag-drop.md
ts-universal-events-key.md
ts-universal-events-show-hide.md
ts-universal-events-touch.md
ts-universal-focus-event.md
ts-universal-mouse-key.md
errorcodes
figures
js-service-widget-ui
native-apis
native-lib
Readme-CN.md
syscap-list.md
syscap.md
security
task-management
telephony
tools
ui
webgl
windowmanager
Readme-CN.md
application-dev-guide-for-gitee.md
application-dev-guide.md
website.md
contribute
design
device-dev
figures
readme
release-notes
third-party-components
Legal-Notices.md
OpenHarmony-Overview_zh.md
glossary.md
readme.md
website.md
.gitattributes
.gitignore
CODEOWNERS
DCO.txt
LICENSE
OAT.xml
README.md
README_zh.md
克隆/下载
ts-methods-custom-dialog-box.md 4.44 KB
一键复制 编辑 原始数据 按行查看 历史

自定义弹窗

通过CustomDialogController类显示自定义弹窗。使用弹窗组件时,可优先考虑自定义弹窗,便于自定义弹窗的样式与内容。

说明:

从API Version 7开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。

接口

CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean, alignment?: DialogAlignment, offset?: Offset, customStyle?: boolean, gridCount?: number})

参数:

参数名 参数类型 必填 参数描述
builder CustomDialog 自定义弹窗内容构造器。
cancel () => void 点击遮障层退出时的回调。
autoCancel boolean 是否允许点击遮障层退出。
默认值:true
alignment DialogAlignment 弹窗在竖直方向上的对齐方式。
默认值:DialogAlignment.Default
offset Offset 弹窗相对alignment所在位置的偏移量。
customStyle boolean 弹窗容器样式是否自定义。
默认值:false
gridCount8+ number 弹窗宽度占栅格宽度的个数。
默认值为4,异常值按默认值处理,最大栅格数为系统最大栅格数。

CustomDialogController

导入对象

dialogController : CustomDialogController = new CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean})

open()

open(): void

显示自定义弹窗内容,若已显示,则不生效。

close

close(): void

关闭显示的自定义弹窗,若已关闭,则不生效。

示例

// xxx.ets
@CustomDialog
struct CustomDialogExample {
  @Link textValue: string
  @Link inputValue: string
  controller: CustomDialogController
  cancel: () => void
  confirm: () => void

  build() {
    Column() {
      Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })
      TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')
        .onChange((value: string) => {
          this.textValue = value
        })
      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('cancel')
          .onClick(() => {
            this.controller.close()
            this.cancel()
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button('confirm')
          .onClick(() => {
            this.inputValue = this.textValue
            this.controller.close()
            this.confirm()
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })
    }
  }
}

@Entry
@Component
struct CustomDialogUser {
  @State textValue: string = ''
  @State inputValue: string = 'click me'
  dialogController: CustomDialogController = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: this.onCancel,
      confirm: this.onAccept,
      textValue: $textValue,
      inputValue: $inputValue
    }),
    cancel: this.existApp,
    autoCancel: true,
    alignment: DialogAlignment.Default,
    offset: { dx: 0, dy: -20 },
    gridCount: 4,
    customStyle: false
  })

  onCancel() {
    console.info('Callback when the first button is clicked')
  }

  onAccept() {
    console.info('Callback when the second button is clicked')
  }

  existApp() {
    console.info('Click the callback in the blank area')
  }

  build() {
    Column() {
      Button(this.inputValue)
        .onClick(() => {
          this.dialogController.open()
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}

zh-cn_image_0000001219744203

Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
其他
1
https://gitee.com/openharmony/docs.git
git@gitee.com:openharmony/docs.git
openharmony
docs
docs
OpenHarmony-3.2-Beta4

搜索帮助