openharmony 鸿蒙 ohos-arkui-advanced-Counter

2025-06-12 浏览 (1)

advanced.Counter

A counter is a component used to accurately adjust values.

NOTE

This component is supported since API version 11. Updates will be marked with a superscript to indicate their earliest API version.

Modules to Import

import { CounterType, CounterComponent, CounterOptions, DateData } from '@kit.ArkUI';

Child Components

Not supported

CounterComponent

CounterComponent({ options: CounterOptions })

Defines a counter.

Decorator: @Component

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDecoratorDescription
optionsCounterOptionsYes@PropParameters of the counter.

CounterOptions

Defines the type and style parameters of the counter.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
typeCounterTypeYesType of the current counter.
direction12+DirectionNoLayout direction.
Default value: Direction.Auto
numberOptionsNumberStyleOptionsNoParameters of the number style counter.
inlineOptionsInlineStyleOptionsNoParameters of the inline number style counter.
dateOptionsDateStyleOptionsNoParameters of the inline date style counter.

A counter type must go with parameters of the matching counter style. Below is a mapping between the counter types and counter styles.

Counter TypeCounter Style
CounterType.LISTNumberStyleOptions
CounterType.COMPACTNumberStyleOptions
CounterType.INLINEInlineStyleOptions
CounterType.INLINE_DATEDateStyleOptions

CounterType

Enumerates the counter types.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameValueDescription
LIST0List counter.
COMPACT1Compact counter.
INLINE2Inline number counter.
INLINE_DATE3Inline date counter.

CommonOptions

Defines common attributes and events of counters.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
focusablebooleanNoWhether the counter is focusable.
NOTE
This attribute takes effect for the number style counter.
Default value: true
true: The counter is focusable.
false: The counter is not focusable.
stepnumberNoStep of the counter.
Value range: an integer greater than or equal to 1.
Default value: 1
onHoverIncrease(isHover: boolean) =>voidNoCallback invoked when the mouse pointer is moved over or away from the increase button of the counter.
isHover: whether the mouse pointer hovers over the component. The value true means that the mouse pointer enters the component, and the value false means that the mouse pointer leaves the component.
onHoverDecrease(isHover: boolean) =>voidNoCallback invoked when the mouse pointer is moved over or away from the decrease button of the counter.
isHover: whether the mouse pointer hovers over the component. The value true means that the mouse pointer enters the component, and the value false means that the mouse pointer leaves the component.

InlineStyleOptions

Defines the attributes and events of the inline number style counter.

Inherits from CommonOptions.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
valuenumberNoInitial value of the counter.
Default value: 0
minnumberNoMinimum value of the counter.
Default value: 0
maxnumberNoMaximum value of the counter.
Default value: 999
textWidthnumberNoText width of the counter.
Default value: 0
onChange(value: number) =>voidNoCallback invoked when the value changes. The current value is returned.
value: current value.

NumberStyleOptions

Defines the attributes and events of the number style counter.

Inherits from InlineStyleOptions.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
labelResourceStrNoLabel of the counter.
onFocusIncrease() =>voidNoCallback invoked when the increase button of the counter gains focus.
onFocusDecrease() =>voidNoCallback invoked when the decrease button of the counter gains focus.
onBlurIncrease() =>voidNoCallback invoked when the increase button of the counter loses focus.
onBlurDecrease() =>voidNoCallback invoked when the decrease button of the counter loses focus.

DateStyleOptions

Defines the attributes and events of the inline date style counter.

Inherits from CommonOptions.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
yearnumberNoInitial year of the counter.
Default value: 1
monthnumberNoInitial month of the counter.
Default value: 1
daynumberNoInitial day of the counter.
Default value: 1
onDateChange(date: DateData)=>voidNoCallback invoked when the date changes. The current date is returned.
date: current date.

DateData

Defines common date attributes and methods.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeRead-OnlyOptionalDescription
yearnumberNoNoInitial year of the counter.
monthnumberNoNoInitial month of the counter.
daynumberNoNoInitial day of the counter.

constructor

constructor(year: number, month: number, day: number)

A constructor used to create a DateData object.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeDescription
yearnumberInitial year of the counter.
monthnumberInitial month of the counter.
daynumberInitial day of the counter.

toString

toString(): string

Current date.

Atomic service API: This API can be used in atomic services since API version 12.

System capability: SystemCapability.ArkUI.ArkUI.Full

Return value

TypeDescription
stringCurrent date.

Example

Example 1: Implementing a List Counter

This example implements a list counter by setting type to CounterType.LIST and configuring numberOptions.

import { CounterType, CounterComponent } from '@kit.ArkUI';

@Entry
@Component
struct ListCounterExample {
  build() {
    Column() {
      // List counter
      CounterComponent({
        options: {
          type: CounterType.LIST,
          numberOptions: {
            label: "Price",
            min: 0,
            value: 5,
            max: 10
          }
        }
      })
    }
  }
}

listcounter

Example 2: Implementing a Compact Counter

This example implements a compact counter by setting type to CounterType.COMPACT and configuring numberOptions.

import { CounterType, CounterComponent } from '@kit.ArkUI';

@Entry
@Component
struct CompactCounterExample {
  build() {
    Column() {
      // Compact counter
      CounterComponent({
        options: {
          type: CounterType.COMPACT,
          numberOptions: {
            label: "Quantity",
            value: 10,
            min: 0,
            max: 100,
            step: 10
          }
        }
      })
    }
  }
}

compactcounter

Example 3: Implementing an Inline Number Counter

This example implements an inline number counter by setting type to CounterType.INLINE and configuring inlineOptions.

import { CounterType, CounterComponent } from '@kit.ArkUI';

@Entry
@Component
struct NumberStyleExample {
  build() {
    Column() {
      // Inline number counter
      CounterComponent({
        options: {
          type: CounterType.INLINE,
          inlineOptions: {
            value: 100,
            min: 10,
            step: 2,
            max: 1000,
            textWidth: 100,
            onChange: (value: number) => {
              console.log("onDateChange Date: " + value.toString());
            }
          }
        }
      })
    }
  }
}

numberstyle

Example 4: Implementing an Inline Date Counter

This example implements an inline date counter by setting type to CounterType.INLINE_DATE and configuring dateOptions, allowing for manual date input.

import { CounterType, CounterComponent, DateData } from '@kit.ArkUI';

@Entry
@Component
struct DataStyleExample {
  build() {
    Column() {
      // Inline date counter
      CounterComponent({
        options: {
          type: CounterType.INLINE_DATE,
          dateOptions: {
            year: 2016,
            onDateChange: (date: DateData) => {
              console.log("onDateChange Date: " + date.toString());
            }
          }
        }
      })
    }
  }
}

datestyle

Example 5: Implementing a Mirrored Layout

This example implements a mirrored layout for list, compact, inline number, and inline date counters by setting direction.

// xxx.ets
import { CounterType, CounterComponent, DateData } from '@kit.ArkUI';

@Entry
@Component
struct CounterPage {
  @State currentDirection: Direction = Direction.Rtl

  build() {
    Column({}) {

      // List counter
      CounterComponent({
        options: {
          direction: this.currentDirection,
          type: CounterType.LIST,
          numberOptions: {
            label: "Price",
            min: 0,
            value: 5,
            max: 10,
          }
        }
      })
        .width('80%')

      // Compact counter
      CounterComponent({
        options: {
          direction: this.currentDirection,
          type: CounterType.COMPACT,
          numberOptions: {
            label: "Quantity",
            value: 10,
            min: 0,
            max: 100,
            step: 10
          }
        }
      }).margin({ top: 20 })

      // Inline number counter
      CounterComponent({
        options: {
          type: CounterType.INLINE,
          direction: this.currentDirection,
          inlineOptions: {
            value: 100,
            min: 10,
            step: 2,
            max: 1000,
            textWidth: 100,
            onChange: (value: number) => {
              console.log("onDateChange Date: " + value.toString());
            }
          }
        }
      }).margin({ top: 20 })
      // Inline date counter
      CounterComponent({
        options: {
          direction: this.currentDirection,
          type: CounterType.INLINE_DATE,
          dateOptions: {
            year: 2024,
            onDateChange: (date: DateData) => {
              console.log("onDateChange Date: " + date.toString());
            }
          }
        }
      }).margin({ top: 20 })
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
  }
}

datestyle

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArcButton

harmony 鸿蒙ArcSlider

harmony 鸿蒙Chip

harmony 鸿蒙ChipGroup

harmony 鸿蒙ComposeListItem

harmony 鸿蒙ComposeTitleBar

harmony 鸿蒙Dialog Box (Dialog)

harmony 鸿蒙DialogV2

harmony 鸿蒙DownloadFileButton

harmony 鸿蒙EditableTitleBar

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