按钮(Button)

2024-01-25 13:14 更新

Button是按钮组件,通常用于响应用户的点击操作,其类型包括胶囊按钮、圆形按钮、普通按钮。Button当做为容器使用时可以通过添加子组件实现包含文字、图片等元素的按钮。具体用法请参考Button

创建按钮

Button通过调用接口来创建,接口调用有以下两种形式:

  • 创建不包含子组件的按钮。
    1. Button(label?: string, options?: { type?: ButtonType, stateEffect?: boolean })

    该接口用于创建不包含子组件的按钮,其中label用来设置按钮文字,type用于设置Button类型,stateEffect属性设置Button是否开启点击效果。

    1. Button('Ok', { type: ButtonType.Normal, stateEffect: true })
    2. .borderRadius(8)
    3. .backgroundColor(0x317aff)
    4. .width(90)
    5. .height(40)

  • 创建包含子组件的按钮。
    1. Button(options?: {type?: ButtonType, stateEffect?: boolean})

    该接口用于创建包含子组件的按钮,只支持包含一个子组件,子组件可以是基础组件或者容器组件

    1. Button({ type: ButtonType.Normal, stateEffect: true }) {
    2. Row() {
    3. Image($r('app.media.loading')).width(20).height(40).margin({ left: 12 })
    4. Text('loading').fontSize(12).fontColor(0xffffff).margin({ left: 5, right: 12 })
    5. }.alignItems(VerticalAlign.Center)
    6. }.borderRadius(8).backgroundColor(0x317aff).width(90).height(40)

设置按钮类型

Button有三种可选类型,分别为Capsule(胶囊类型)、Circle(圆形按钮)和Normal(普通按钮),通过type进行设置。

  • 胶囊按钮(默认类型)

    此类型按钮的圆角自动设置为高度的一半,不支持通过borderRadius属性重新设置圆角。

    1. Button('Disable', { type: ButtonType.Capsule, stateEffect: false })
    2. .backgroundColor(0x317aff)
    3. .width(90)
    4. .height(40)

  • 圆形按钮

    此类型按钮为圆形,不支持通过borderRadius属性重新设置圆角。

    1. Button('Circle', { type: ButtonType.Circle, stateEffect: false })
    2. .backgroundColor(0x317aff)
    3. .width(90)
    4. .height(90)

  • 普通按钮

    此类型的按钮默认圆角为0,支持通过borderRadius属性重新设置圆角。

    1. Button('Ok', { type: ButtonType.Normal, stateEffect: true })
    2. .borderRadius(8)
    3. .backgroundColor(0x317aff)
    4. .width(90)
    5. .height(40)

自定义样式

  • 设置边框弧度。
    一般使用通用属性来自定义按钮样式。例如通过borderRadius属性设置按钮的边框弧度。
    1. Button('circle border', { type: ButtonType.Normal })
    2. .borderRadius(20)
    3. .height(40)

  • 设置文本样式。

    通过添加文本样式设置按钮文本的展示样式。

    1. Button('font style', { type: ButtonType.Normal })
    2. .fontSize(20)
    3. .fontColor(Color.Pink)
    4. .fontWeight(800)

  • 设置背景颜色。

    添加backgroundColor属性设置按钮的背景颜色。

    1. Button('background color').backgroundColor(0xF55A42)

  • 用作功能型按钮。

    为删除操作创建一个按钮。

    1. Button({ type: ButtonType.Circle, stateEffect: true }) {
    2. Image($r('app.media.ic_public_delete_filled')).width(30).height(30)
    3. }.width(55).height(55).margin({ left: 20 }).backgroundColor(0xF55A42)

添加事件

Button组件通常用于触发某些操作,可以绑定onClick事件来响应点击操作后的自定义行为。

  1. Button('Ok', { type: ButtonType.Normal, stateEffect: true })
  2. .onClick(()=>{
  3. console.info('Button onClick')
  4. })

场景示例

  • 用于启动操作。

    可以用按钮启动任何用户界面元素,按钮会根据用户的操作触发相应的事件。例如,在List容器里通过点击按钮进行页面跳转。

    1. // xxx.ets
    2. import router from '@ohos.router';
    3. @Entry
    4. @Component
    5. struct ButtonCase1 {
    6. build() {
    7. List({ space: 4 }) {
    8. ListItem() {
    9. Button("First").onClick(() => {
    10. router.pushUrl({ url: 'pages/first_page' })
    11. })
    12. .width('100%')
    13. }
    14. ListItem() {
    15. Button("Second").onClick(() => {
    16. router.pushUrl({ url: 'pages/second_page' })
    17. })
    18. .width('100%')
    19. }
    20. ListItem() {
    21. Button("Third").onClick(() => {
    22. router.pushUrl({ url: 'pages/third_page' })
    23. })
    24. .width('100%')
    25. }
    26. }
    27. .listDirection(Axis.Vertical)
    28. .backgroundColor(0xDCDCDC).padding(20)
    29. }
    30. }

  • 用于表单的提交。

    在用户登录/注册页面,使用按钮进行登录或注册操作。

    1. // xxx.ets
    2. @Entry
    3. @Component
    4. struct ButtonCase2 {
    5. build() {
    6. Column() {
    7. TextInput({ placeholder: 'input your username' }).margin({ top: 20 })
    8. TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 })
    9. Button('Register').width(300).margin({ top: 20 })
    10. .onClick(() => {
    11. // 需要执行的操作
    12. })
    13. }.padding(20)
    14. }
    15. }

  • 悬浮按钮

    在可以滑动的界面,滑动时按钮始终保持悬浮状态。

    1. // xxx.ets
    2. @Entry
    3. @Component
    4. struct HoverButtonExample {
    5. private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    6. build() {
    7. Stack() {
    8. List({ space: 20, initialIndex: 0 }) {
    9. ForEach(this.arr, (item) => {
    10. ListItem() {
    11. Text('' + item)
    12. .width('100%').height(100).fontSize(16)
    13. .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
    14. }
    15. }, item => item)
    16. }.width('90%')
    17. Button() {
    18. Image($r('app.media.ic_public_add'))
    19. .width(50)
    20. .height(50)
    21. }
    22. .width(60)
    23. .height(60)
    24. .position({x: '80%', y: 600})
    25. .shadow({radius: 10})
    26. .onClick(() => {
    27. // 需要执行的操作
    28. })
    29. }
    30. .width('100%')
    31. .height('100%')
    32. .backgroundColor(0xDCDCDC)
    33. .padding({ top: 5 })
    34. }
    35. }

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号