# SegmentButton **SegmentButton** is a versatile component that organizes related options into visually grouped buttons. It supports three variants: tab-style, capsule-style single-select, and capsule-style multi-select. >**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 { SegmentButton, SegmentButtonOptions, SegmentButtonItemOptionsArray } from '@kit.ArkUI'; ``` ## Child Components Not supported ## SegmentButton SegmentButton({ options: SegmentButtonOptions, selectedIndexes: number[], onItemClicked: Callback\, maxFontScale: number \| Resource }) **Decorator**: @Component **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. | Name | Type | Mandatory| Decorator | Description | | --------------- | --------------------------------------------- | ---- | ----------- | ------------------------------------------------------------ | | options | [SegmentButtonOptions](#segmentbuttonoptions) | Yes | @ObjectLink | Options of the **SegmentButton** component.
**Atomic service API**: This API can be used in atomic services since API version 12.| | selectedIndexes | number[] | Yes | @Link | Indexes of selected items of the **SegmentButton**. The index is zero-based and increments by 1.
**NOTE**
**selectedIndexes** is decorated with [@Link](../../../ui/state-management/arkts-link.md) to implement parent-child two-way synchronization. If no items are selected, an empty array **[]** can be passed in.
**Atomic service API**: This API can be used in atomic services since API version 12.| | onItemClicked13+ | Callback\ | No| - | Callback invoked when an item in the **SegmentButton** is clicked. The index of the clicked item is passed in as the parameter.
**Atomic service API**: This API can be used in atomic services since API version 13.| | maxFontScale14+ | number \| [Resource](ts-types.md#resource) | Yes| @Prop | Maximum font scale for the text in the **SegmentButton**.
Default value: **1**
Value range: [1, 2]
**NOTE**
Values less than 1 are treated as 1, and values greater than 2 are treated as 2.
**Atomic service API**: This API can be used in atomic services since API version 14.| >**NOTE** > >The **SegmentButton** component does not support the universal attributes. The component takes up the maximum width allowed by the content area, and evenly allocates the width among its items. It adapts its height automatically to the content (text and image), the minimum height being 28 vp. ## SegmentButtonOptions >**NOTE** > > The component does not support custom font type settings. Provides initial data and custom properties for the **SegmentButton** component. **Decorator Type**: @Observed **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. ### Properties | Name | Type | Mandatory | Description | | ----------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | | type | 'tab' \| 'capsule' | Yes | Type of the **SegmentButton** component.
**NOTE**
**'tab'**: tab-style segmented buttons, designed for page or content section switching.
**'capsule'**: capsule-style segmented buttons, suitable for single or multiple selection scenarios.
**Atomic service API**: This API can be used in atomic services since API version 12.| | multiply | boolean | Yes | Whether multiple items can be selected.
**NOTE**
Default value: **false**.
**true**: Multiple items can be selected.
**false**: Multiple items cannot be selected. For the **SegmentButton** component consisting of tab-style buttons, only one item can be selected. In this case, setting **multiply** to **true** does not take effect.
**Atomic service API**: This API can be used in atomic services since API version 12.| | buttons | [SegmentButtonItemOptionsArray](#segmentbuttonitemoptionsarray) | Yes| Button information, including the icon and text.
**Atomic service API**: This API can be used in atomic services since API version 12.| | fontColor | [ResourceColor](ts-types.md#resourcecolor) | No | Font color of the unselected item.
Default value: **$r('sys.color.ohos_id_color_text_secondary')**
**Atomic service API**: This API can be used in atomic services since API version 12.| | selectedFontColor | [ResourceColor](ts-types.md#resourcecolor) | No | Font color of the selected item.
Default value: **$r('sys.color.ohos_id_color_text_primary')** when **type** is **"tab"**
and **$r('sys.color.ohos_id_color_foreground_contrary')** when **type** is **"capsule"**
**Atomic service API**: This API can be used in atomic services since API version 12.| | fontSize | [DimensionNoPercentage](#dimensionnopercentage) | No | Font size of the unselected item. It cannot be set in percentage.
Default value: **$r('sys.float.ohos_id_text_size_body2')**
**Atomic service API**: This API can be used in atomic services since API version 12.| | selectedFontSize | [DimensionNoPercentage](#dimensionnopercentage) | No | Font size of the selected item. It cannot be set in percentage.
Default value: **$r('sys.float.ohos_id_text_size_body2')**
**Atomic service API**: This API can be used in atomic services since API version 12.| | fontWeight | [FontWeight](ts-appendix-enums.md#fontweight) | No | Font weight of the unselected item.
Default value: **FontWeight.Regular**
**Atomic service API**: This API can be used in atomic services since API version 12.| | selectedFontWeight | [FontWeight](ts-appendix-enums.md#fontweight) | No | Font weight of the selected item.
Default value: **FontWeight.Medium**
**Atomic service API**: This API can be used in atomic services since API version 12.| | backgroundColor | [ResourceColor](ts-types.md#resourcecolor) | No | Background color of the unselected item.
Default value: **$r('sys.color.ohos_id_color_button_normal')**
**Atomic service API**: This API can be used in atomic services since API version 12.| | selectedBackgroundColor | [ResourceColor](ts-types.md#resourcecolor) | No | Background color of the selected item.
Default value: **$r('sys.color.ohos_id_color_foreground_contrary')** when **type** is **"tab"**
and **$r('sys.color.ohos_id_color_emphasize')** when **type** is **"capsule"**.
**Atomic service API**: This API can be used in atomic services since API version 12.| | imageSize | [SizeOptions](ts-types.md#sizeoptions) | No | Image size.
Default value: { width: 24, height: 24 }.
**NOTE**
This property is effective only for buttons that contain icons.
**Atomic service API**: This API can be used in atomic services since API version 12.| | buttonPadding | [Padding](ts-types.md#padding) \| [Dimension](ts-types.md#dimension10) | No| Button margin.
Default value: **{ top: 4, right: 8, bottom: 4, left: 8 }** for icon buttons and text buttons, and **{ top: 6, right: 8, bottom: 6, left: 8 }** for icon+text buttons.
**Atomic service API**: This API can be used in atomic services since API version 12.| | textPadding | [Padding](ts-types.md#padding) \| [Dimension](ts-types.md#dimension10) | No| Text padding.
Default value: **0**
**Atomic service API**: This API can be used in atomic services since API version 12.| | localizedButtonPadding12+ | [LocalizedPadding](ts-types.md#localizedpadding12) | No | Button padding.
Default value:
Text-only buttons and icon-only buttons: **{ top: LengthMetrics.vp(4), end: LengthMetrics.vp(8), bottom: LengthMetrics.vp(4), start: LengthMetrics.vp(8) }**
Buttons with both an icon and text: **{ top: LengthMetrics.vp(6), end: LengthMetrics.vp(8), bottom: LengthMetrics.vp(6), start: LengthMetrics.vp(8) }**
**Atomic service API**: This API can be used in atomic services since API version 12.| | localizedTextPadding12+ | [LocalizedPadding](ts-types.md#localizedpadding12) | No | Text padding.
Default value: **0**
**Atomic service API**: This API can be used in atomic services since API version 12.| | direction12+ | [Direction](ts-appendix-enums.md#direction) | No | Layout direction.
Default value: **Direction.Auto**
**Atomic service API**: This API can be used in atomic services since API version 12.| | backgroundBlurStyle | [BlurStyle](ts-universal-attributes-background.md#blurstyle9) | No | Background blur style applied between the content and the background.
Default value: **BlurStyle.NONE**
**Atomic service API**: This API can be used in atomic services since API version 12.| | borderRadiusMode20+ | [BorderRadiusMode](#borderradiusmode20) | No| Border radius mode, used to control the border radius calculation method.
Default value: **BorderRadiusMode.DEFAULT**.
**Atomic service API**: This API can be used in atomic services since API version 20.| | backgroundBorderRadius20+ | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | No| Container border radius.
**NOTE**
This attribute takes effect only when **borderRadiusMode** is set to **BorderRadiusMode.CUSTOM**.
For capsule-style multi-select segmented buttons (with **type** being **'capsule'** and **multiply** being **true**), this attribute does not take effect and **itemBorderRadius** must be used to set the border radius.
The maximum value for the border radius is half of the component's width or height, and percentage values are not supported.
Default value: **$r('sys.float.segmentbutton_container_shape')**.
**Atomic service API**: This API can be used in atomic services since API version 20.| | itemBorderRadius20+ | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | No| Individual button border radius.
**NOTE**
This attribute takes effect only when **borderRadiusMode** is set to **BorderRadiusMode.CUSTOM**.
For capsule-style multi-select segmented buttons (with **type** being **'capsule'** and **multiply** being **true**), this attribute only affects end items.
The maximum value for the border radius is half of the component's width or height, and percentage values are not supported.
Default value: **$r('sys.float.segmentbutton_selected_background_shape')**.
**Atomic service API**: This API can be used in atomic services since API version 20.| ### constructor constructor(options: TabSegmentButtonOptions | CapsuleSegmentButtonOptions) A constructor used to create a **SegmentButton** instance with specified configuration options. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. **Parameters** | Name | Type | Mandatory| Description | | ------- | ------------------------------------------------------------ | ---- | -------------------- | | options | [TabSegmentButtonOptions](#tabsegmentbuttonoptions) \| [CapsuleSegmentButtonOptions](#capsulesegmentbuttonoptions) | Yes| Configuration options for tab-style or capsule-style segmented buttons.| ### tab static tab(options: TabSegmentButtonConstructionOptions): SegmentButtonOptions Creates a **SegmentButtonOptions** object specifically for tab-style segmented buttons. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. **Parameters** | Name | Type | Mandatory| Description | | ------- | ------------------------------------------------------------ | ---- | -------------------- | | options | [TabSegmentButtonConstructionOptions](#tabsegmentbuttonconstructionoptions) | Yes | Configuration options for tab-style segmented buttons.| **Return value** | Type | Description | | ------ | ------------------------ | | [SegmentButtonOptions](#segmentbuttonoptions) | **SegmentButtonOptions** object.| ### capsule static capsule(options: CapsuleSegmentButtonConstructionOptions): SegmentButtonOptions Creates a **SegmentButtonOptions** object specifically for capsule-style segmented buttons. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. **Parameters** | Name | Type | Mandatory| Description | | ------- | ------------------------------------------------------------ | ---- | --------------------------- | | options | [CapsuleSegmentButtonConstructionOptions](#capsulesegmentbuttonconstructionoptions) | Yes | Configuration options for capsule-style segmented buttons.| **Return value** | Type | Description | | ------ | ------------------------ | | [SegmentButtonOptions](#segmentbuttonoptions) | **SegmentButtonOptions** object.| ## DimensionNoPercentage type DimensionNoPercentage = PX | VP | FP | LPX | Resource Represents the length union type that does not support values in percentage. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. | Type | Description | | -------------------------------- | --------------------------------------------- | | [PX](ts-types.md#px10) | Length in px. | | [VP](ts-types.md#vp10) | Length in vp. | | [FP](ts-types.md#fp10) | Length in fp. | | [LPX](ts-types.md#lpx10) | Length in lpx.| | [Resource](ts-types.md#resource) | Resource reference type, which is used to set the value of a component attribute. | ## CommonSegmentButtonOptions Defines the customizable properties for the **SegmentButton** component. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. ### Properties | Name | Type | Mandatory | Description | | ----------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | | fontColor | [ResourceColor](ts-types.md#resourcecolor) | No | Font color of the unselected item.
Default value: **$r('sys.color.ohos_id_color_text_secondary')**
**Atomic service API**: This API can be used in atomic services since API version 12.| | selectedFontColor | [ResourceColor](ts-types.md#resourcecolor) | No | Font color of the selected item.
Default value: **$r('sys.color.ohos_id_color_text_primary')** when **type** is **"tab"**
and **$r('sys.color.ohos_id_color_foreground_contrary')** when **type** is **"capsule"**
**Atomic service API**: This API can be used in atomic services since API version 12.| | fontSize | [DimensionNoPercentage](#dimensionnopercentage) | No | Font size of the unselected item. It cannot be set in percentage.
Default value: **$r('sys.float.ohos_id_text_size_body2')**.
**Atomic service API**: This API can be used in atomic services since API version 12.| | selectedFontSize | [DimensionNoPercentage](#dimensionnopercentage) | No | Font size of the selected item. It cannot be set in percentage.
Default value: **$r('sys.float.ohos_id_text_size_body2')**.
**Atomic service API**: This API can be used in atomic services since API version 12.| | fontWeight | [FontWeight](ts-appendix-enums.md#fontweight) | No | Font weight of the unselected item.
Default value: **FontWeight.Regular**
**Atomic service API**: This API can be used in atomic services since API version 12.| | selectedFontWeight | [FontWeight](ts-appendix-enums.md#fontweight) | No | Font weight of the selected item.
Default value: **FontWeight.Medium**
**Atomic service API**: This API can be used in atomic services since API version 12.| | backgroundColor | [ResourceColor](ts-types.md#resourcecolor) | No | Background color of the unselected item.
Default value: **$r('sys.color.ohos_id_color_button_normal')**
**Atomic service API**: This API can be used in atomic services since API version 12.| | selectedBackgroundColor | [ResourceColor](ts-types.md#resourcecolor) | No | Background color of the selected item.
Default value: **$r('sys.color.ohos_id_color_foreground_contrary')** when **type** is **"tab"**
and **$r('sys.color.ohos_id_color_emphasize')** when **type** is **"capsule"**.
**Atomic service API**: This API can be used in atomic services since API version 12.| | imageSize | [SizeOptions](ts-types.md#sizeoptions) | No | Image size.
Default value: { width: 24, height: 24 }.
**NOTE**
This property is effective only for buttons that contain icons.
**Atomic service API**: This API can be used in atomic services since API version 12.| | buttonPadding | [Padding](ts-types.md#padding) \| [Dimension](ts-types.md#dimension10) | No| Button margin.
Default value: **{ top: 4, right: 8, bottom: 4, left: 8 }** for icon buttons and text buttons, and **{ top: 6, right: 8, bottom: 6, left: 8 }** for icon+text buttons.
**Atomic service API**: This API can be used in atomic services since API version 12.| | textPadding | [Padding](ts-types.md#padding) \| [Dimension](ts-types.md#dimension10) | No| Text padding.
Default value: **0**
**Atomic service API**: This API can be used in atomic services since API version 12.| | localizedButtonPadding12+ | [LocalizedPadding](ts-types.md#localizedpadding12) | No | Button padding.
Default value:
Text-only buttons and icon-only buttons: **{ top: LengthMetrics.vp(4), end: LengthMetrics.vp(8), bottom: LengthMetrics.vp(4), start: LengthMetrics.vp(8) }**
Buttons with both an icon and text: **{ top: LengthMetrics.vp(6), end: LengthMetrics.vp(8), bottom: LengthMetrics.vp(6), start: LengthMetrics.vp(8) }**
**Atomic service API**: This API can be used in atomic services since API version 12.| | localizedTextPadding12+ | [LocalizedPadding](ts-types.md#localizedpadding12) | No | Text padding.
Default value: **0**
**Atomic service API**: This API can be used in atomic services since API version 12.| | direction12+ | [Direction](ts-appendix-enums.md#direction) | No | Layout direction.
Default value: **Direction.Auto**
**Atomic service API**: This API can be used in atomic services since API version 12.| | backgroundBlurStyle | [BlurStyle](ts-universal-attributes-background.md#blurstyle9) | No | Background blur style applied between the content and the background.
Default value: **BlurStyle.NONE**
**Atomic service API**: This API can be used in atomic services since API version 12.| | borderRadiusMode20+ | [BorderRadiusMode](#borderradiusmode20) | No| Border radius mode, used to control the border radius calculation method.
Default value: **BorderRadiusMode.DEFAULT**.
**Atomic service API**: This API can be used in atomic services since API version 20.| | backgroundBorderRadius20+ | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | No| Container border radius.
**NOTE**
This attribute takes effect only when **borderRadiusMode** is set to **BorderRadiusMode.CUSTOM**.
For capsule-style multi-select segmented buttons (with **type** being **'capsule'** and **multiply** being **true**), this attribute does not take effect and **itemBorderRadius** must be used to set the border radius.
The maximum value for the border radius is half of the component's width or height, and percentage values are not supported.
Default value: **$r('sys.float.segmentbutton_container_shape')**.
**Atomic service API**: This API can be used in atomic services since API version 20.| | itemBorderRadius20+ | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | No| Individual button border radius.
**NOTE**
This attribute takes effect only when **borderRadiusMode** is set to **BorderRadiusMode.CUSTOM**.
For capsule-style multi-select segmented buttons (with **type** being **'capsule'** and **multiply** being **true**), this attribute only affects end items.
The maximum value for the border radius is half of the component's width or height, and percentage values are not supported.
Default value: **$r('sys.float.segmentbutton_selected_background_shape')**.
**Atomic service API**: This API can be used in atomic services since API version 20.| ## BorderRadiusMode20+ Enumerates the border radius modes for the **SegmentButton** component, which are used to control the border radius calculation method. **Atomic service API**: This API can be used in atomic services since API version 20. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. | Name |Value| Description | | ------- | - |-------------------------------- | | DEFAULT | 0 |Default mode, where the framework automatically calculates the border radius.| | CUSTOM | 1 |Custom mode, where the border radius is set by the developer.| ## TabSegmentButtonConstructionOptions Represents configuration options for creating a **SegmentButton** component consisting of tab-style segmented buttons. Inherits from [CommonSegmentButtonOptions](#commonsegmentbuttonoptions). **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. ### Properties | Name | Type | Mandatory| Description | | ------- | ------------------------------------------------------------ | ---- | ---------- | | buttons | [ItemRestriction](#itemrestriction)\<[SegmentButtonTextItem](#segmentbuttontextitem)> | Yes | Button information.| ## CapsuleSegmentButtonConstructionOptions Represents configuration options for creating a **SegmentButton** component consisting of capsule-style segmented buttons. Inherits from [CommonSegmentButtonOptions](#commonsegmentbuttonoptions). **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. ### Properties | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------- | ---- | ----------------------------- | | buttons | [SegmentButtonItemTuple](#segmentbuttonitemtuple) | Yes | Button information. | | multiply | boolean | No | Whether multi-selection is allowed.
**true**: Multi-selection is allowed.
**false**: Multi-selection is not allowed. Default value: **false**.| ## ItemRestriction type ItemRestriction\ = [T, T, T?, T?, T?] Represents a tuple used to store button information. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. | Type | Description | | ----------------------------------------- | --------------------------------- | |[T, T, T?, T?, T?] | A tuple that contains two to five elements of the same type.| >**NOTE** > >A **SegmentButton** component supports two to five buttons. ## SegmentButtonItemTuple type SegmentButtonItemTuple = ItemRestriction\ | ItemRestriction\ | ItemRestriction\ Represents the tuple union type used to store button information. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. | Type | Description | | ------------------------------------------------------------ | ------------------------- | | [ItemRestriction](#itemrestriction)\<[SegmentButtonTextItem](#segmentbuttontextitem)\> | A tuple of text-only button information. | | [ItemRestriction](#itemrestriction)\<[SegmentButtonIconItem](#segmentbuttoniconitem)\> | A tuple of icon-only button information. | | [ItemRestriction](#itemrestriction)\<[SegmentButtonIconTextItem](#segmentbuttonicontextitem)\> | A tuple of icon and text button information.| ## SegmentButtonItemArray type SegmentButtonItemArray = Array\ | Array\ | Array\ Represents the array union type used to store button information. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. | Type | Description | | ------------------------------------------------------------ | ------------------------- | | Array\<[SegmentButtonTextItem](#segmentbuttontextitem)\> | An array of text-only button information. | | Array\<[SegmentButtonIconItem](#segmentbuttoniconitem)\> | An array of icon-only button information. | | Array\<[SegmentButtonIconTextItem](#segmentbuttonicontextitem)\> | An array of icon and text button information.| ## SegmentButtonItemOptionsArray Represents an array for storing button information. **Decorator Type**: @Observed >**NOTE** > >A **SegmentButton** component supports two to five buttons. As such, a **SegmentButtonItemOptionsArray** object stores information about only two to five buttons. ### constructor constructor(elements: SegmentButtonItemTuple) A constructor used to create a **SegmentButtonItemOptionsArray** instance. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------- | ---- | ---------- | | elements | [SegmentButtonItemTuple](#segmentbuttonitemtuple) | Yes | Button information.| ### push push(...items: SegmentButtonItemArray): number Adds the specified elements to the end of this array and returns the new length of the array. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. **Parameters** | Name| Type | Mandatory| Description | | ------ | ------------------------------------------------- | ---- | ---------------------- | | items | [SegmentButtonItemArray](#segmentbuttonitemarray) | Yes | Elements to add to the end of the array.| **Return value** | Type | Description | | ------ | ------------------------ | | number | Length of the array after the elements are added.| >**NOTE** > >A **SegmentButton** component supports two to five buttons. As such, a **SegmentButtonItemOptionsArray** object stores information about only two to five buttons. If the number limit is reached, this API will not work. ### pop pop(): SegmentButtonItemOptions | undefined Removes the last element from this array and returns that element. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. **Return value** | Type | Description | | ------------------------------------------------------------ | -------------- | | [SegmentButtonItemOptions](#segmentbuttonitemoptions) \| undefined | Element removed from the array.| >**NOTE** > >A **SegmentButton** component supports two to five buttons. As such, a **SegmentButtonItemOptionsArray** object stores information about only two to five buttons. If the number limit is reached, this API will not work. ### shift shift(): SegmentButtonItemOptions | undefined Removes the first element from this array and returns that element. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. **Return value** | Type | Description | | ------------------------------------------------------------ | -------------- | | [SegmentButtonItemOptions](#segmentbuttonitemoptions) \| undefined | Element removed from the array.| >**NOTE** > >A **SegmentButton** component supports two to five buttons. As such, a **SegmentButtonItemOptionsArray** object stores information about only two to five buttons. If the number limit is reached, this API will not work. ### unshift unshift(...items: SegmentButtonItemArray): number Adds the specified elements to the beginning of this array and returns the new length of the array. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. **Parameters** | Name | Type | Mandatory| Description | | ----- | ------------------------------------------------- | ---- | -------------------- | | items | [SegmentButtonItemArray](#segmentbuttonitemarray) | Yes | Elements to add to the beginning of the array.| **Return value** | Type | Description | | ------ | ---------------------- | | number | Length of the array after the elements are added.| >**NOTE** > >A **SegmentButton** component supports two to five buttons. As such, a **SegmentButtonItemOptionsArray** object stores information about only two to five buttons. If the number limit is reached, this API will not work. ### splice splice(start: number, deleteCount: number, ...items: SegmentButtonItemOptions[]): SegmentButtonItemOptions[] Changes the contents of this array by removing the specified number of elements from the specified position and adding new elements in place. This API returns an array containing the removed elements. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. **Parameters** | Name | Type | Mandatory| Description | | ----------- | ------------------------------------------------------- | ---- | -------------------- | | start | number | Yes | Index of the position starting from which elements are to be removed.| | deleteCount | number | Yes | Number of elements to remove. | | items | [SegmentButtonItemOptions](#segmentbuttonitemoptions)[] | No | Elements to add to the array. | **Return value** | Type | Description | | ------------------------------------------------------- | ------------------------------ | | [SegmentButtonItemOptions](#segmentbuttonitemoptions)[] | An array containing the removed elements.| >**NOTE** > >A **SegmentButton** component supports two to five buttons. As such, a **SegmentButtonItemOptionsArray** object stores information about only two to five buttons. If the number limit is reached, this API will not work. ### create static create(elements: SegmentButtonItemTuple): SegmentButtonItemOptionsArray Creates a **SegmentButtonItemOptionsArray** object. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. **Parameters** | Name | Type | Mandatory| Description | | -------- | ------------------------------------------------- | ---- | ---------- | | elements | [SegmentButtonItemTuple](#segmentbuttonitemtuple) | Yes | Button information.| **Return value** | Type | Description | | ------------------------------------------------------------ | ----------------------------------------- | | [SegmentButtonItemOptionsArray](#segmentbuttonitemoptionsarray) | Created **SegmentButtonItemOptionsArray** object.| ## TabSegmentButtonOptions Provides configuration options for tab-style segmented buttons. Inherits from [TabSegmentButtonConstructionOptions](#tabsegmentbuttonconstructionoptions). **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. | Name| Type | Mandatory| Description | | ---- | ----- | ---- | ---------------------- | | type | 'tab' | Yes | Type of the segmented buttons, which is **'tab'** in this case.| ## CapsuleSegmentButtonOptions Provides configuration options for capsule-style segmented buttons. Inherits from [CapsuleSegmentButtonConstructionOptions](#capsulesegmentbuttonconstructionoptions). **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. | Name| Type | Mandatory| Description | | ---- | --------- | ---- | ----------------------------- | | type | 'capsule' | Yes | Type of the segmented buttons, which is **'capsule'** in this case.| ## SegmentButtonTextItem Provides text button information. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. | Name| Type | Mandatory| Description | | ---- | -------------------------------------- | ---- | ---------- | | text | [ResourceStr](ts-types.md#resourcestr) | Yes | Button text.
**Atomic service API**: This API can be used in atomic services since API version 12.| | accessibilityLevel13+ | string | No | Accessibility level, which is used to set whether the current component can be recognized by accessibility services.
The options are as follows:
**"auto"**: The component can be recognized by accessibility services.
**"yes"**: The component can be recognized by accessibility services.
**"no"**: The component cannot be recognized by accessibility services.
**"no-hide-descendants"**: Neither the component nor its child components can be recognized by accessibility services.
Default value: **"auto"**
**Atomic service API**: This API can be used in atomic services since API version 13.| | accessibilityDescription13+ | [ResourceStr](ts-types.md#resourcestr) | No | Accessibility description. You can specify further explanation of the current component, for example, possible operation consequences, especially those that cannot be learned from component attributes and accessibility text. If a component contains both text information and the accessibility description, the text is read first and then the accessibility description, when the component is selected.
**Atomic service API**: This API can be used in atomic services since API version 13.| ## SegmentButtonIconItem Provides icon button information. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. |Name | Type | Mandatory| Description | | ------------ | -------------------------------------- | ---- | -------------------- | | icon | [ResourceStr](ts-types.md#resourcestr) | Yes | Icon of the unselected item.
**Atomic service API**: This API can be used in atomic services since API version 12.| | iconAccessibilityText13+ | [ResourceStr](ts-types.md#resourcestr) | No | Accessibility text of the unselected item.
**Atomic service API**: This API can be used in atomic services since API version 13.| | selectedIcon | [ResourceStr](ts-types.md#resourcestr) | Yes | Icon of the selected item.
**Atomic service API**: This API can be used in atomic services since API version 12. | | selectedIconAccessibilityText13+ | [ResourceStr](ts-types.md#resourcestr) | No | Accessibility text of the selected item.
**Atomic service API**: This API can be used in atomic services since API version 13. | | accessibilityLevel13+ | string | No | Accessibility level, which is used to set whether the current component can be recognized by accessibility services.
The options are as follows:
**"auto"**: The component can be recognized by accessibility services.
**"yes"**: The component can be recognized by accessibility services.
**"no"**: The component cannot be recognized by accessibility services.
**"no-hide-descendants"**: Neither the component nor its child components can be recognized by accessibility services.
Default value: **"auto"**
**Atomic service API**: This API can be used in atomic services since API version 13. | | accessibilityDescription13+ | [ResourceStr](ts-types.md#resourcestr) | No | Accessibility description. You can specify further explanation of the current component, for example, possible operation consequences, especially those that cannot be learned from component attributes and accessibility text. If a component contains both text information and the accessibility description, the text is read first and then the accessibility description, when the component is selected.
**Atomic service API**: This API can be used in atomic services since API version 13. | >**NOTE** > >For the settings to take effect, both **icon** and **selectedIcon** must be set. ## SegmentButtonIconTextItem Provides icon+text button information. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. ### Properties | Name | Type | Mandatory| Description | | ------------ | -------------------------------------- | ---- | -------------------- | | icon | [ResourceStr](ts-types.md#resourcestr) | Yes | Icon of the unselected item.
**Atomic service API**: This API can be used in atomic services since API version 12.| | iconAccessibilityText13+ | [ResourceStr](ts-types.md#resourcestr) | No | Accessibility text of the unselected item.
**Atomic service API**: This API can be used in atomic services since API version 13.| | selectedIcon | [ResourceStr](ts-types.md#resourcestr) | Yes | Icon of the selected item.
**Atomic service API**: This API can be used in atomic services since API version 12. | | selectedIconAccessibilityText13+ | [ResourceStr](ts-types.md#resourcestr) | No | Accessibility text of the selected item.
**Atomic service API**: This API can be used in atomic services since API version 13. | | text | [ResourceStr](ts-types.md#resourcestr) | Yes | Button text.
**Atomic service API**: This API can be used in atomic services since API version 12. | | accessibilityLevel13+ | string | No | Accessibility level, which is used to set whether the current component can be recognized by accessibility services.
The options are as follows:
**"auto"**: The component can be recognized by accessibility services.
**"yes"**: The component can be recognized by accessibility services.
**"no"**: The component cannot be recognized by accessibility services.
**"no-hide-descendants"**: Neither the component nor its child components can be recognized by accessibility services.
Default value: **"auto"**
**Atomic service API**: This API can be used in atomic services since API version 13. | | accessibilityDescription13+ | [ResourceStr](ts-types.md#resourcestr) | No | Accessibility description. You can specify further explanation of the current component, for example, possible operation consequences, especially those that cannot be learned from component attributes and accessibility text. If a component contains both text information and the accessibility description, the text is read first and then the accessibility description, when the component is selected.
**Atomic service API**: This API can be used in atomic services since API version 13. | >**NOTE** > >For the settings to take effect, both **icon** and **selectedIcon** must be set. ## SegmentButtonItemOptions Describes options of the items in the **SegmentButton** component. **Decorator Type**: @Observed **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. ### Properties | Name | Type | Mandatory| Description | | ------------ | -------------------------------------- | ---- | -------------------- | | icon | [ResourceStr](ts-types.md#resourcestr) | No | Icon of the unselected item.
**Atomic service API**: This API can be used in atomic services since API version 12.| | iconAccessibilityText13+ | [ResourceStr](ts-types.md#resourcestr) | No | Accessibility text of the unselected item.
**Atomic service API**: This API can be used in atomic services since API version 13.| | selectedIcon | [ResourceStr](ts-types.md#resourcestr) | No | Icon of the selected item.
**Atomic service API**: This API can be used in atomic services since API version 12. | | selectedIconAccessibilityText13+ | [ResourceStr](ts-types.md#resourcestr) | No | Accessibility text of the selected item.
**Atomic service API**: This API can be used in atomic services since API version 13. | | text | [ResourceStr](ts-types.md#resourcestr) | No | Button text.
**Atomic service API**: This API can be used in atomic services since API version 12. | | accessibilityLevel13+ | string | No | Accessibility level, which is used to set whether the current component can be recognized by accessibility services.
The options are as follows:
**"auto"**: The component can be recognized by accessibility services.
**"yes"**: The component can be recognized by accessibility services.
**"no"**: The component cannot be recognized by accessibility services.
**"no-hide-descendants"**: Neither the component nor its child components can be recognized by accessibility services.
Default value: **"auto"**
**Atomic service API**: This API can be used in atomic services since API version 13. | | accessibilityDescription13+ | [ResourceStr](ts-types.md#resourcestr) | No | Accessibility description. You can specify further explanation of the current component, for example, possible operation consequences, especially those that cannot be learned from component attributes and accessibility text. If a component contains both text information and the accessibility description, the text is read first and then the accessibility description, when the component is selected.
**Atomic service API**: This API can be used in atomic services since API version 13. | ### constructor constructor(options: SegmentButtonItemOptionsConstructorOptions) A constructor used to create a **SegmentButtonItemOptions** instance. **Atomic service API**: This API can be used in atomic services since API version 12. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. **Parameters** | Name | Type | Mandatory| Description | | ------- | ------------------------------------------------------------ | ---- | ------------------ | | options | [SegmentButtonItemOptionsConstructorOptions](#segmentbuttonitemoptionsconstructoroptions) | Yes | Configuration options for a single segmented button, including the icon, text, and accessibility attributes.| ## SegmentButtonItemOptionsConstructorOptions Describes the constructor parameters of **SegmentButtonItemOptions**. **System capability**: SystemCapability.ArkUI.ArkUI.Full **Device behavior differences**: On wearables, calling this API results in a runtime exception indicating that the API is undefined. On other devices, the API works correctly. ### Properties | Name | Type | Mandatory| Description | | ------------ | -------------------------------------- | ---- | -------------------- | | icon | [ResourceStr](ts-types.md#resourcestr) | No | Icon of the unselected item.
**Atomic service API**: This API can be used in atomic services since API version 12.| | iconAccessibilityText13+ | [ResourceStr](ts-types.md#resourcestr) | No | Accessibility text of the unselected item.
**Atomic service API**: This API can be used in atomic services since API version 13.| | selectedIcon | [ResourceStr](ts-types.md#resourcestr) | No | Icon of the selected item.
**Atomic service API**: This API can be used in atomic services since API version 12. | | selectedIconAccessibilityText13+ | [ResourceStr](ts-types.md#resourcestr) | No | Accessibility text of the selected item.
**Atomic service API**: This API can be used in atomic services since API version 13. | | text | [ResourceStr](ts-types.md#resourcestr) | No | Button text.
**Atomic service API**: This API can be used in atomic services since API version 12. | | accessibilityLevel13+ | string | No | Accessibility level, which is used to set whether the current component can be recognized by accessibility services.
The options are as follows:
**"auto"**: The component can be recognized by accessibility services.
**"yes"**: The component can be recognized by accessibility services.
**"no"**: The component cannot be recognized by accessibility services.
**"no-hide-descendants"**: Neither the component nor its child components can be recognized by accessibility services.
Default value: **"auto"**
**Atomic service API**: This API can be used in atomic services since API version 13. | | accessibilityDescription13+ | [ResourceStr](ts-types.md#resourcestr) | No | Accessibility description. You can specify further explanation of the current component, for example, possible operation consequences, especially those that cannot be learned from component attributes and accessibility text. If a component contains both text information and the accessibility description, the text is read first and then the accessibility description, when the component is selected.
**Atomic service API**: This API can be used in atomic services since API version 13. | ## Example ### Example 1: Setting the Type of the SegmentButton component This example demonstrates how to create two different types of **SegmentButton** components by configuring **SegmentButtonOptions** with **tab** and **capsule** types. ```ts // xxx.ets import { ItemRestriction, SegmentButton, SegmentButtonItemTuple, SegmentButtonOptions, SegmentButtonTextItem } from '@kit.ArkUI'; @Entry @Component struct Index { @State tabOptions: SegmentButtonOptions = SegmentButtonOptions.tab({ buttons: [{ text: 'Tab 1' }, { text: 'Tab 2' }, { text: 'Tab 3' }] as ItemRestriction, backgroundBlurStyle: BlurStyle.BACKGROUND_THICK }) @State singleSelectCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [{ text: 'Single-select 1' }, { text: 'Single-select 2' }, { text: 'Single-select 3' }] as SegmentButtonItemTuple, multiply: false, backgroundBlurStyle: BlurStyle.BACKGROUND_THICK }) @State multiplySelectCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [{ text: 'Multi-select 1' }, { text: 'Multi-select 2' }, { text: 'Multi-select 3' }] as SegmentButtonItemTuple, multiply: true }) @State iconCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [ { icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') } ] as SegmentButtonItemTuple, multiply: false, backgroundBlurStyle: BlurStyle.BACKGROUND_THICK }) @State iconTextCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [ { text: 'Icon 1', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { text: 'Icon 2', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { text: 'Icon 3', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { text: 'Icon 4', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { text: 'Icon 5', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') } ] as SegmentButtonItemTuple, multiply: true }) @State tabSelectedIndexes: number[] = [1] @State singleSelectCapsuleSelectedIndexes: number[] = [0] @State multiplySelectCapsuleSelectedIndexes: number[] = [0, 1] @State singleSelectIconCapsuleSelectedIndexes: number[] = [3] @State multiplySelectIconTextCapsuleSelectedIndexes: number[] = [1, 2] build() { Row() { Column() { Column({ space: 25 }) { SegmentButton({ options: this.tabOptions, selectedIndexes: $tabSelectedIndexes }) SegmentButton({ options: this.singleSelectCapsuleOptions, selectedIndexes: $singleSelectCapsuleSelectedIndexes }) SegmentButton({ options: this.multiplySelectCapsuleOptions, selectedIndexes: $multiplySelectCapsuleSelectedIndexes }) SegmentButton({ options: this.iconCapsuleOptions, selectedIndexes: $singleSelectIconCapsuleSelectedIndexes }) SegmentButton({ options: this.iconTextCapsuleOptions, selectedIndexes: $multiplySelectIconTextCapsuleSelectedIndexes }) }.width('90%') }.width('100%') }.height('100%') } } ``` ![segmentbutton-sample1](figures/segmentbutton-sample1.png) ### Example 2: Setting the Style of the SegmentButton component This example demonstrates how to use **CommonSegmentButtonOptions** to customize the text and background style of the **SegmentButton** component. ```ts // xxx.ets import { ItemRestriction, SegmentButton, SegmentButtonItemTuple, SegmentButtonOptions, SegmentButtonTextItem } from '@kit.ArkUI'; @Entry @Component struct Index { @State tabOptions: SegmentButtonOptions = SegmentButtonOptions.tab({ buttons: [{ text: 'Tab 1' }, { text: 'Tab 2' }, { text: 'Tab 3' }] as ItemRestriction, backgroundColor: 'rgb(213,213,213)', selectedBackgroundColor: 'rgb(112,112,112)', textPadding: { top: 10, right: 10, bottom: 10, left: 10 }, }) @State singleSelectCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [{ text: 'Single-select 1' }, { text: 'Single-select 2' }, { text: 'Single-select 3' }] as SegmentButtonItemTuple, multiply: false, fontColor: 'rgb(0,74,175)', selectedFontColor: 'rgb(247,247,247)', backgroundBlurStyle: BlurStyle.BACKGROUND_THICK }) @State multiplySelectCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [{ text: 'Multi-select 1' }, { text: 'Multi-select 2' }, { text: 'Multi-select 3' }] as SegmentButtonItemTuple, multiply: true, fontSize: 18, selectedFontSize: 18, fontWeight: FontWeight.Bolder, selectedFontWeight: FontWeight.Lighter, }) @State iconCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [ { icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') } ] as SegmentButtonItemTuple, multiply: false, imageSize: { width: 40, height: 40 }, buttonPadding: { top: 6, right: 10, bottom: 6, left: 10 }, backgroundBlurStyle: BlurStyle.BACKGROUND_THICK }) @State iconTextCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [ { text: 'Icon 1', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { text: 'Icon 2', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { text: 'Icon 3', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { text: 'Icon 4', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { text: 'Icon 5', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') } ] as SegmentButtonItemTuple, multiply: true, imageSize: { width: 10, height: 10 }, }) @State tabSelectedIndexes: number[] = [0] @State singleSelectCapsuleSelectedIndexes: number[] = [0] @State multiplySelectCapsuleSelectedIndexes: number[] = [0, 1] @State singleSelectIconCapsuleSelectedIndexes: number[] = [3] @State multiplySelectIconTextCapsuleSelectedIndexes: number[] = [1, 2] build() { Row() { Column() { Column({ space: 20 }) { SegmentButton({ options: this.tabOptions, selectedIndexes: $tabSelectedIndexes }) SegmentButton({ options: this.singleSelectCapsuleOptions, selectedIndexes: $singleSelectCapsuleSelectedIndexes }) SegmentButton({ options: this.multiplySelectCapsuleOptions, selectedIndexes: $multiplySelectCapsuleSelectedIndexes }) SegmentButton({ options: this.iconCapsuleOptions, selectedIndexes: $singleSelectIconCapsuleSelectedIndexes }) SegmentButton({ options: this.iconTextCapsuleOptions, selectedIndexes: $multiplySelectIconTextCapsuleSelectedIndexes }) }.width('90%') }.width('100%') }.height('100%') } } ``` ![segmentbutton-sample2](figures/segmentbutton-sample2.png) ### Example 3: Performing Array Operations on the SegmentButton Component This example shows how to perform operations such as adding and removing segmented buttons using array functions like **pop**, **shift**, and **unshift**. ```ts import { SegmentButton, SegmentButtonOptions, SegmentButtonItemOptionsArray, SegmentButtonItemTuple, SegmentButtonItemOptions } from '@kit.ArkUI'; @Entry @Component struct Index { @State singleSelectCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [{ text: '1' }, { text: '2' }, { text: '3' }, { text: '4' }, { text: '5' }] as SegmentButtonItemTuple, multiply: false, backgroundBlurStyle: BlurStyle.BACKGROUND_THICK }) @State capsuleSelectedIndexes: number[] = [0] build() { Row() { Column() { Column({ space: 10 }) { SegmentButton({ options: this.singleSelectCapsuleOptions, selectedIndexes: $capsuleSelectedIndexes }) Button("Delete First Item") .onClick(() => { this.singleSelectCapsuleOptions.buttons.shift() }) Button("Delete Last Item") .onClick(() => { this.singleSelectCapsuleOptions.buttons.pop() }) Button("Add to End") .onClick(() => { this.singleSelectCapsuleOptions.buttons.push({ text: 'push' }) }) Button("Add to Beginning") .onClick(() => { this.singleSelectCapsuleOptions.buttons.unshift(({ text: 'unshift' })) }) Button("Replace Items 2 and 3 with splice1 and splice2") .onClick(() => { this.singleSelectCapsuleOptions.buttons.splice(1, 2, new SegmentButtonItemOptions({ text: 'splice1' }), new SegmentButtonItemOptions({ text: 'splice2' })) }) Button("Change All Button Text") .onClick(() => { this.singleSelectCapsuleOptions.buttons = SegmentButtonItemOptionsArray.create([{ text: 'a' }, { text: 'b' }, { text: 'c' }, { text: 'd' }, { text: 'e' }]) }) }.width('90%') }.width('100%') }.height('100%') } } ``` ![segmentbutton-sample3](figures/segmentbutton-sample3.gif) ### Example 4: Implementing a Mirror Effect This example shows how to implement a mirror effect by configuring the **direction** property of the **SegmentButton** component. ```ts // xxx.ets import { LengthMetrics, SegmentButton, SegmentButtonOptions } from '@kit.ArkUI'; @Entry @Component struct Index { @State tabOptions: SegmentButtonOptions = SegmentButtonOptions.tab({ buttons: [{ text: 'Tab 1' }, { text: 'Tab 2' }, { text: 'Tab 3' }], direction: Direction.Rtl, backgroundColor: Color.Green, selectedBackgroundColor: Color.Orange, localizedTextPadding: { end: LengthMetrics.vp(10), start: LengthMetrics.vp(10) }, }) @State singleSelectCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [{ text: 'Single-select 1' }, { text: 'Single-select 2' }, { text: 'Single-select 3' }], multiply: false, direction: Direction.Rtl, fontColor: Color.Black, selectedFontColor: Color.Yellow, backgroundBlurStyle: BlurStyle.BACKGROUND_THICK }) @State multiplySelectCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [{ text: 'Multi-select 1' }, { text: 'Multi-select 2' }, { text: 'Multi-select 3' }], multiply: true, direction: Direction.Rtl, fontSize: 18, selectedFontSize: 18, fontWeight: FontWeight.Bolder, selectedFontWeight: FontWeight.Lighter, }) @State iconCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [ { icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') } ], multiply: false, direction: Direction.Rtl, imageSize: { width: 40, height: 40 }, localizedButtonPadding: { end: LengthMetrics.vp(10), start: LengthMetrics.vp(10) }, backgroundBlurStyle: BlurStyle.BACKGROUND_THICK }) @State iconTextCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [ { text: 'Icon 1', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { text: 'Icon 2', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { text: 'Icon 3', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { text: 'Icon 4', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') }, { text: 'Icon 5', icon: $r('sys.media.ohos_ic_public_email'), selectedIcon: $r('sys.media.ohos_ic_public_clock') } ], multiply: true, direction: Direction.Rtl, imageSize: { width: 10, height: 10 }, }) @State tabSelectedIndexes: number[] = [0] @State singleSelectCapsuleSelectedIndexes: number[] = [0] @State multiplySelectCapsuleSelectedIndexes: number[] = [0, 1] @State singleSelectIconCapsuleSelectedIndexes: number[] = [3] @State multiplySelectIconTextCapsuleSelectedIndexes: number[] = [1, 2] build() { Row() { Column() { Column({ space: 20 }) { SegmentButton({ options: this.tabOptions, selectedIndexes: $tabSelectedIndexes }) SegmentButton({ options: this.singleSelectCapsuleOptions, selectedIndexes: $singleSelectCapsuleSelectedIndexes }) SegmentButton({ options: this.multiplySelectCapsuleOptions, selectedIndexes: $multiplySelectCapsuleSelectedIndexes }) SegmentButton({ options: this.iconCapsuleOptions, selectedIndexes: $singleSelectIconCapsuleSelectedIndexes }) SegmentButton({ options: this.iconTextCapsuleOptions, selectedIndexes: $multiplySelectIconTextCapsuleSelectedIndexes }) }.width('90%') }.width('100%') }.height('100%') } } ``` ![segmentbutton-sample4](figures/segmentbutton-sample4.png) ### Example 5: Setting Accessibility This example showcases how to implement accessibility features for the **SegmentButton** component by configuring properties such as **accessibilityLevel** and **selectedIconAccessibilityText**. ```ts // xxx.ets import { ItemRestriction, SegmentButton, SegmentButtonItemTuple, SegmentButtonOptions, SegmentButtonTextItem, SegmentButtonItemOptions } from '@kit.ArkUI'; @Entry @Component struct Index { @State tabOptions: SegmentButtonOptions = SegmentButtonOptions.tab({ buttons: [{ text: 'Tab 1', accessibilityLevel: 'yes', accessibilityDescription: 'Tab 1 usage hints' }, { text: 'Tab 2', accessibilityLevel: 'yes', accessibilityDescription: 'Tab 2 usage hints' }, { text: 'Tab 3 ', accessibilityLevel: 'yes', accessibilityDescription: 'Tab 3 usage hints' }] as ItemRestriction, backgroundBlurStyle: BlurStyle.BACKGROUND_THICK }) @State iconCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [ { icon: $r('sys.media.ohos_ic_public_email'), iconAccessibilityText: 'Unselected icon accessibility text', selectedIcon: $r('sys.media.ohos_ic_public_clock'), selectedIconAccessibilityText: 'Selected icon accessibility text', accessibilityLevel: 'yes', accessibilityDescription: 'SegmentButtonIconItem usage hints' }, { icon: $r('sys.media.ohos_ic_public_email'), iconAccessibilityText: 'Unselected icon accessibility text', selectedIcon: $r('sys.media.ohos_ic_public_clock'), selectedIconAccessibilityText: 'Selected icon accessibility text', accessibilityLevel: 'yes', accessibilityDescription: 'SegmentButtonIconItem usage hints' }, { icon: $r('sys.media.ohos_ic_public_email'), iconAccessibilityText: 'Unselected icon accessibility text', selectedIcon: $r('sys.media.ohos_ic_public_clock'), selectedIconAccessibilityText: 'Selected icon accessibility text', accessibilityLevel: 'yes', accessibilityDescription: 'SegmentButtonIconItem usage hints' }, { icon: $r('sys.media.ohos_ic_public_email'), iconAccessibilityText: 'Unselected icon accessibility text', selectedIcon: $r('sys.media.ohos_ic_public_clock'), selectedIconAccessibilityText: 'Selected icon accessibility text', accessibilityLevel: 'yes', accessibilityDescription: 'SegmentButtonIconItem usage hints' } ] as SegmentButtonItemTuple, multiply: false, backgroundBlurStyle: BlurStyle.BACKGROUND_THICK }) @State iconTextCapsuleOptions: SegmentButtonOptions = SegmentButtonOptions.capsule({ buttons: [ { text: 'Icon 1', icon: $r('sys.media.ohos_ic_public_email'), iconAccessibilityText: 'Unselected icon accessibility text', selectedIcon: $r('sys.media.ohos_ic_public_clock'), selectedIconAccessibilityText: 'Selected icon accessibility text', accessibilityLevel: 'yes', accessibilityDescription: 'SegmentButtonIconTextItem usage hints' }, { text: 'Icon 1', icon: $r('sys.media.ohos_ic_public_email'), iconAccessibilityText: 'Unselected icon accessibility text', selectedIcon: $r('sys.media.ohos_ic_public_clock'), selectedIconAccessibilityText: 'Selected icon accessibility text', accessibilityLevel: 'yes', accessibilityDescription: 'SegmentButtonIconTextItem usage hints' }, { text: 'Icon 1', icon: $r('sys.media.ohos_ic_public_email'), iconAccessibilityText: 'Unselected icon accessibility text', selectedIcon: $r('sys.media.ohos_ic_public_clock'), selectedIconAccessibilityText: 'Selected icon accessibility text', accessibilityLevel: 'yes', accessibilityDescription: 'SegmentButtonIconTextItem usage hints' }, { text: 'Icon 1', icon: $r('sys.media.ohos_ic_public_email'), iconAccessibilityText: 'Unselected icon accessibility text', selectedIcon: $r('sys.media.ohos_ic_public_clock'), selectedIconAccessibilityText: 'Selected icon accessibility text', accessibilityLevel: 'yes', accessibilityDescription: 'SegmentButtonIconTextItem usage hints' } ] as SegmentButtonItemTuple, multiply: true }) @State tabSelectedIndexes: number[] = [1] @State singleSelectIconCapsuleSelectedIndexes: number[] = [3] @State multiplySelectIconTextCapsuleSelectedIndexes: number[] = [1, 2] build() { Row() { Column() { Column({ space: 25 }) { SegmentButton({ options: this.tabOptions, selectedIndexes: $tabSelectedIndexes }) SegmentButton({ options: this.iconCapsuleOptions, selectedIndexes: $singleSelectIconCapsuleSelectedIndexes }) SegmentButton({ options: this.iconTextCapsuleOptions, selectedIndexes: $multiplySelectIconTextCapsuleSelectedIndexes }) Button("Replace Items 2 and 3 with splice1 and splice2") .onClick(() => { this.iconTextCapsuleOptions.buttons.splice(1, 2, new SegmentButtonItemOptions({ text: 'splice1', accessibilityLevel: 'yes', accessibilityDescription: 'SegmentButtonItemOptions usage hints' }), new SegmentButtonItemOptions({ text: 'splice2', icon: $r('sys.media.ohos_ic_public_email'), iconAccessibilityText: 'Unselected icon accessibility text', selectedIcon: $r('sys.media.ohos_ic_public_clock'), selectedIconAccessibilityText: 'Selected icon accessibility text', accessibilityLevel: 'yes', accessibilityDescription: 'SegmentButtonIconTextItem usage hints' })) }) }.width('90%') }.width('100%') }.height('100%') } } ``` ### Example 6: Setting Custom Border Radius This example demonstrates how to set a custom border radius for the **SegmentButton** component. ```ts // xxx.ets import { BorderRadiusMode, ItemRestriction, LengthMetrics, SegmentButton, SegmentButtonOptions, SegmentButtonTextItem } from '@kit.ArkUI'; @Entry @Component struct Index { @State tabOptions: SegmentButtonOptions = SegmentButtonOptions.tab({ buttons: [{ text: 'Tab 1' }, { text: 'Tab 2' }, { text: 'Tab 3' }] as ItemRestriction, backgroundBlurStyle: BlurStyle.BACKGROUND_THICK, borderRadiusMode: BorderRadiusMode.CUSTOM, backgroundBorderRadius: LengthMetrics.vp(8), itemBorderRadius: LengthMetrics.vp(6) }) @State tabSelectedIndexes: number[] = [1] build() { Row() { Column() { Column({ space: 25 }) { SegmentButton({ options: this.tabOptions, selectedIndexes: $tabSelectedIndexes, }) }.width('90%') }.width('100%') }.height('100%') } } ``` ![segmentbutton-sample6](figures/segmentbutton-sample6.png)