HarmonyOS 鸿蒙Next ArkTS语言教程入门学习第78天,知识点按钮(Button)内容。

发布于 1周前 作者 phonegap100 来自 鸿蒙OS

HarmonyOS 鸿蒙Next ArkTS语言教程入门学习第78天,知识点按钮(Button)内容。

1、按钮(Button)

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

1.1、创建按钮

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

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

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

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

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

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

1.2、设置按钮类型

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

  • 胶囊按钮(默认类型)

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

Button('Disable', { type: ButtonType.Capsule, stateEffect: false }) 
  .backgroundColor(0x317aff) 
  .width(90)
  .height(40)
  • 圆形按钮

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

Button('Circle', { type: ButtonType.Circle, stateEffect: false }) 
  .backgroundColor(0x317aff) 
  .width(90) 
  .height(90)
  • 普通按钮

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

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

1.3、自定义样式

  • 设置边框弧度。

一般使用通用属性来自定义按钮样式。例如通过borderRadius属性设置按钮的边框弧度。

Button('circle border', { type: ButtonType.Normal }) 
  .borderRadius(20)
  .height(40)
  • 设置文本样式。

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

Button('font style', { type: ButtonType.Normal }) 
  .fontSize(20) 
  .fontColor(Color.Pink) 
  .fontWeight(800)
  • 设置背景颜色。

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

Button('background color').backgroundColor(0xF55A42)
  • 用作功能型按钮。

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

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)

1.4、添加事件

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

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

1.5、场景示例

  • 用于启动操作。

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

// xxx.ets
import router from '@ohos.router';
[@Entry](/user/Entry)
[@Component](/user/Component)
struct ButtonCase1 {
  build() {
    List({ space: 4 }) {
      ListItem() {
        Button("First").onClick(() => {
          router.pushUrl({ url: 'pages/first_page' })
        })
          .width('100%')
      }
      ListItem() {
        Button("Second").onClick(() => {
          router.pushUrl({ url: 'pages/second_page' })
        })
          .width('100%')
      }
      ListItem() {
        Button("Third").onClick(() => {
          router.pushUrl({ url: 'pages/third_page' })
        })
          .width('100%')
      }
    }
    .listDirection(Axis.Vertical)
    .backgroundColor(0xDCDCDC).padding(20)
  }
}
  • 用于表单的提交。

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

// xxx.ets
[@Entry](/user/Entry)
[@Component](/user/Component)
struct ButtonCase2 {
  build() {
    Column() {
      TextInput({ placeholder: 'input your username' }).margin({ top: 20 })
      TextInput({ placeholder: 'input your password' }).type(InputType.Password).margin({ top: 20 })
      Button('Register').width(300).margin({ top: 20 })
        .onClick(() => {
          // 需要执行的操作
        })
    }.padding(20)
  }
}

- 悬浮按钮

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

```javascript
// xxx.ets
[@Entry](/user/Entry)
[@Component](/user/Component)
struct HoverButtonExample {
  private arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  build() {
    Stack() {
      List({ space: 20, initialIndex: 0 }) {
        ForEach(this.arr, (item) => {
          ListItem() {
            Text('' + item)
              .width('100%').height(100).fontSize(16)
              .textAlign(TextAlign.Center).borderRadius(10).backgroundColor(0xFFFFFF)
          }
        }, item => item)
      }.width('90%')
      Button() {
        Image($r('app.media.ic_public_add'))
          .width(50)
          .height(50)
      }
      .width(60)
      .height(60)
      .position({x: '80%', y: 600})
      .shadow({radius: 10})
      .onClick(() => {
        // 需要执行的操作
      })
    }
    .width('100%')
    .height('100%')
    .backgroundColor(0xDCDCDC)
    .padding({ top: 5 })
  }
}

更多关于HarmonyOS 鸿蒙Next ArkTS语言教程入门学习第78天,知识点按钮(Button)内容。的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS鸿蒙Next中,ArkTS语言中的按钮(Button)组件是用于用户交互的常见UI元素。Button组件可以通过设置不同的属性来实现多样化的功能。以下是一些关键知识点:

  1. 基本用法:Button组件可以通过Button标签创建,并可以设置文本内容、点击事件等。例如:

    Button('点击我')
      .onClick(() => {
        console.log('按钮被点击');
      })
  2. 样式设置:Button组件支持多种样式设置,包括背景颜色、文字颜色、边框等。可以通过backgroundColorfontColorborderRadius等属性进行自定义。例如:

    Button('点击我')
      .backgroundColor(Color.Blue)
      .fontColor(Color.White)
      .borderRadius(10)
  3. 事件处理:Button组件支持多种事件,如点击事件(onClick)、长按事件(onLongPress)等。可以通过这些事件来实现用户交互逻辑。例如:

    Button('长按我')
      .onLongPress(() => {
        console.log('按钮被长按');
      })
  4. 状态管理:Button组件可以结合状态管理来实现动态变化。例如,通过[@State](/user/State)装饰器来管理按钮的启用或禁用状态。例如:

    [@State](/user/State) isEnabled: boolean = true;
    
    Button('点击我')
      .enabled(this.isEnabled)
      .onClick(() => {
        this.isEnabled = false;
      })
  5. 布局控制:Button组件可以与其他布局组件结合使用,如RowColumn等,以实现复杂的界面布局。例如:

    Column() {
      Button('按钮1')
      Button('按钮2')
    }

以上是ArkTS语言中Button组件的一些基本知识点,通过这些内容可以快速入门并掌握Button组件的使用。

更多关于HarmonyOS 鸿蒙Next ArkTS语言教程入门学习第78天,知识点按钮(Button)内容。的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在第78天的HarmonyOS鸿蒙Next ArkTS语言教程中,您将学习到按钮(Button)的相关知识。按钮是用户界面中常见的交互元素,用于触发特定操作。在ArkTS中,您可以通过Button组件创建按钮,并设置其文本、样式、点击事件等属性。例如,使用onClick事件处理按钮点击操作,通过text属性设置按钮显示的文本。此外,您还可以自定义按钮的样式,如背景颜色、边框、圆角等,以提升用户体验。掌握这些知识点将帮助您构建更加交互丰富的应用界面。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!