1.4K Star 7.4K Fork 8.1K

OpenHarmony/docs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
docker
en
zh-cn
application-dev
ability
connectivity
database
device-usage-statistics
device
dfx
internationalization
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-navigation.md
ts-basic-components-progress.md
ts-basic-components-qrcode.md
ts-basic-components-radio.md
ts-basic-components-rating.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-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-grid.md
ts-container-gridcontainer.md
ts-container-griditem.md
ts-container-list.md
ts-container-listitem.md
ts-container-navigator.md
ts-container-panel.md
ts-container-refresh.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-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-interpolation-calculation.md
ts-matrix-transformation.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-transition-animation-component.md
ts-transition-animation-geometrytransition.md
ts-transition-animation-shared-elements.md
ts-types.md
ts-universal-attributes-background.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-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-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-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
js-service-widget-ui
native-apis
native-lib
Readme-CN.md
security
task-management
telephony
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
OpenHarmony-Overview_zh.md
glossary.md
readme.md
website.md
.gitattributes
.gitignore
CODEOWNERS
DCO.txt
LICENSE
OAT.xml
README.md
README_zh.md
克隆/下载
ts-basic-components-button.md 4.95 KB
一键复制 编辑 原始数据 按行查看 历史
tianyu 提交于 3年前 . uodate

Button

提供按钮组件。

说明:

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

权限列表

子组件

可以包含单个子组件。

接口

方法1: Button(options?: {type?: ButtonType, stateEffect?: boolean})

表1 options参数说明

参数名 参数类型 必填 默认值 参数描述
type ButtonType Capsule 描述按钮风格。
stateEffect boolean true 按钮按下时是否开启切换效果,当状态置为false时,点击效果关闭。

方法2: Button(label?: ResourceStr, options?: { type?: ButtonType, stateEffect?: boolean })

使用文本内容创建相应的按钮组件,此时Button无法包含子组件。

表2 value参数说明

参数名 参数类型 必填 默认值 参数描述
label ResourceStr - 按钮文本内容。
options Object - 见options参数说明。

属性

名称 参数类型 默认值 描述
type ButtonType ButtonType.Capsule 设置Button样式。
stateEffect boolean true 状态切换时是否开启切换效果,当状态置为false时,点击效果关闭。

ButtonType枚举说明

名称 描述
Capsule 胶囊型按钮(圆角默认为高度的一半)。
Circle 圆形按钮。
Normal 普通按钮(默认不带圆角)。

说明:

  • 按钮圆角通过通用属性borderRadius设置(不支持通过border接口设置圆角)。
  • 当按钮类型为Capsule时,borderRadius设置不生效,按钮圆角始终为高度的一半。
  • 当按钮类型为Circle时,borderRadius即为按钮半径,若未设置borderRadius按钮半径则为宽、高中较小值的一半。
  • 按钮文本通过通用文本样式进行设置。

示例

// xxx.ets
@Entry
@Component
struct ButtonExample {
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Start, justifyContent: FlexAlign.SpaceBetween }) {
      Text('Common button').fontSize(9).fontColor(0xCCCCCC)
      Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
        Button('Ok', { type: ButtonType.Normal, stateEffect: true }).borderRadius(8).backgroundColor(0x317aff).width(90)
        Button({ type: ButtonType.Normal, stateEffect: true }) {
          Row() {
            Image($r('app.media.loading')).width(20).height(20).margin({ left: 12 })
            Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
          }.alignItems(VerticalAlign.Center)
        }.borderRadius(8).backgroundColor(0x317aff).width(90)
        Button('Disable', { type: ButtonType.Normal, stateEffect: false }).opacity(0.5)
          .borderRadius(8).backgroundColor(0x317aff).width(90)
      }

      Text('Capsule button').fontSize(9).fontColor(0xCCCCCC)
      Flex({ alignItems: ItemAlign.Center, justifyContent: FlexAlign.SpaceBetween }) {
        Button('Ok', { type: ButtonType.Capsule, stateEffect: true }).backgroundColor(0x317aff).width(90)
        Button({ type: ButtonType.Capsule, stateEffect: true }) {
          Row() {
            Image($r('app.media.loading')).width(20).height(20).margin({ left: 12 })
            Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
          }.alignItems(VerticalAlign.Center).width(90)
        }.backgroundColor(0x317aff)
        .onClick((event: ClickEvent) => {
          AlertDialog.show({ message: 'The login is successful' })
        })
        Button('Disable', { type: ButtonType.Capsule, stateEffect: false }).opacity(0.5)
          .backgroundColor(0x317aff).width(90)
      }

      Text('Circle button').fontSize(9).fontColor(0xCCCCCC)
      Flex({ alignItems: ItemAlign.Center, wrap: FlexWrap.Wrap }) {
        Button({ type: ButtonType.Circle, stateEffect: true }) {
          Image($r('app.media.ic_public_app_filled')).width(20).height(20)
        }.width(55).height(55).backgroundColor(0x317aff)
        Button({ type: ButtonType.Circle, stateEffect: true }) {
          Image($r('app.media.ic_public_delete_filled')).width(30).height(30)
        }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)
      }
    }.height(400).padding({ left: 35, right: 35, top: 35 })
  }
}

zh-cn_image_0000001219864141

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

搜索帮助