
https://www.bilibili.com/video/BV1jomdBBE4H/

ButtonGroup 是控件库中的按钮组组件,将多个按钮组合在一起,支持单选和多选两种模式,适用于选项选择、筛选、状态切换等场景。
按钮组采用统一视觉设计,具有以下特点:
import { ButtonGroup, ButtonGroupItem } from '../components/base'
@Entry
@Component
struct MyPage {
@State selectedValue: string | number = 'option1'
private items: ButtonGroupItem[] = [
{ label: '选项1', value: 'option1' },
{ label: '选项2', value: 'option2' },
{ label: '选项3', value: 'option3' }
]
build() {
Column({ space: 20 }) {
// 单选模式
ButtonGroup({
items: this.items,
mode: 'single'
})
// 多选模式
ButtonGroup({
items: this.items,
mode: 'multiple'
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}ButtonGroup 使用内部状态管理选中值。如果需要外部控制选中值,可以通过监听点击事件或使用状态管理来实现。
属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
items | ButtonGroupItem[] | [] | 按钮项列表(必需) |
mode | 'single' | 'multiple' | 'single' | 选择模式:单选或多选 |
buttonSize | 'small' | 'medium' | 'large' | 'medium' | 按钮尺寸 |
showBrand | boolean | true | 是否显示品牌标识 |
disabled | boolean | false | 是否禁用整个按钮组 |
属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
label | string | - | 按钮文字(必需) |
value | string | number | - | 按钮值(必需) |
disabled | boolean? | undefined | 是否禁用该项 |
icon | ResourceStr? | undefined | 图片图标(可选) |
textIcon | string? | undefined | 文字图标(可选,优先级高于 icon) |
尺寸 | 按钮高度 | 字体大小 | 图标大小 | 内边距 |
|---|---|---|---|---|
small | 28vp | 12vp | 14vp | 12vp(左右) |
medium | 36vp | 14vp | 16vp | 16vp(左右) |
large | 44vp | 16vp | 20vp | 20vp(左右) |
@Entry
@Component
struct ButtonGroupExample1 {
private items: ButtonGroupItem[] = [
{ label: '选项1', value: 'option1' },
{ label: '选项2', value: 'option2' },
{ label: '选项3', value: 'option3' }
]
build() {
Column({ space: 15 }) {
ButtonGroup({
items: this.items,
mode: 'single'
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct ButtonGroupExample2 {
private items: ButtonGroupItem[] = [
{ label: '选项1', value: 'option1' },
{ label: '选项2', value: 'option2' },
{ label: '选项3', value: 'option3' }
]
build() {
Column({ space: 15 }) {
ButtonGroup({
items: this.items,
mode: 'multiple'
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct ButtonGroupExample3 {
private items: ButtonGroupItem[] = [
{ label: '全部', value: 'all', textIcon: 'A' },
{ label: '待处理', value: 'pending', textIcon: 'P' },
{ label: '已完成', value: 'completed', textIcon: '√' }
]
build() {
Column({ space: 15 }) {
ButtonGroup({
items: this.items,
mode: 'single'
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct ButtonGroupExample4 {
private items: ButtonGroupItem[] = [
{ label: '小', value: 'small' },
{ label: '中', value: 'medium' },
{ label: '大', value: 'large' }
]
build() {
Column({ space: 20 }) {
ButtonGroup({
items: this.items,
mode: 'single',
buttonSize: 'small'
})
ButtonGroup({
items: this.items,
mode: 'single',
buttonSize: 'medium'
})
ButtonGroup({
items: this.items,
mode: 'single',
buttonSize: 'large'
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct ButtonGroupExample5 {
private items: ButtonGroupItem[] = [
{ label: '可用', value: 'available' },
{ label: '不可用', value: 'unavailable', disabled: true },
{ label: '维护中', value: 'maintenance' }
]
build() {
Column({ space: 20 }) {
// 整个按钮组禁用
ButtonGroup({
items: this.items,
mode: 'single',
disabled: true
})
Divider()
.margin({ top: 20, bottom: 20 })
// 部分按钮禁用
ButtonGroup({
items: this.items,
mode: 'single'
})
}
.width('100%')
.height('100%')
.padding(20)
.justifyContent(FlexAlign.Center)
}
}@Entry
@Component
struct FilterExample {
build() {
Column({ space: 20 }) {
// 时间筛选
Column({ space: 10 }) {
Text('时间筛选:')
.fontSize(16)
.fontColor('#111827')
.width('100%')
ButtonGroup({
items: [
{ label: '全部', value: 'all' },
{ label: '今天', value: 'today' },
{ label: '本周', value: 'week' },
{ label: '本月', value: 'month' }
],
mode: 'single',
buttonSize: 'medium'
})
}
.width('100%')
}
.width('100%')
.height('100%')
.padding(20)
}
}@Entry
@Component
struct StatusExample {
build() {
Column({ space: 20 }) {
// 订单状态(多选)
Column({ space: 10 }) {
Text('订单状态(多选):')
.fontSize(16)
.fontColor('#111827')
.width('100%')
ButtonGroup({
items: [
{ label: '待付款', value: 'unpaid', textIcon: 'P' },
{ label: '待发货', value: 'unshipped', textIcon: 'S' },
{ label: '已发货', value: 'shipped', textIcon: '√' },
{ label: '已完成', value: 'completed', textIcon: 'C' }
],
mode: 'multiple',
buttonSize: 'medium'
})
}
.width('100%')
}
.width('100%')
.height('100%')
.padding(20)
}
}ButtonGroup 的所有样式都通过 ComponentTheme 配置,所有配置都在代码中,不依赖JSON文件。
import { ComponentTheme } from '../theme/ComponentTheme'
// 修改主色(影响选中状态的背景色)
ComponentTheme.PRIMARY_COLOR = '#007AFF'
// 修改次要背景色(影响未选中状态的背景色)
ComponentTheme.BACKGROUND_SECONDARY = '#F5F5F5'
// 修改边框颜色
ComponentTheme.BORDER_COLOR = '#E5E5E5'
// 修改圆角
ComponentTheme.BORDER_RADIUS = 8import { ComponentTheme } from '../theme/ComponentTheme'
// 使用 setTheme 方法批量配置
ComponentTheme.setTheme({
primaryColor: '#007AFF',
borderRadius: 8,
spacing: 16
})推荐:根据使用场景选择模式
A: 主要区别在于使用场景:
ButtonGroup 更适合需要从多个选项中选择的场景。
A: 根据业务需求选择:
A: ButtonGroup 使用内部状态管理。如果需要外部获取选中值,可以通过以下方式:
A: 可以。通过更新 items 数组来动态更新选项:
@State items: ButtonGroupItem[] = [
{ label: '选项1', value: 'option1' }
]
// 动态添加选项
this.items.push({ label: '选项2', value: 'option2' })
this.items = [...this.items] // 触发更新A: 可以通过 ComponentTheme 自定义全局样式:
ComponentTheme.PRIMARY_COLOR = '#34C759' // 选中状态背景色
ComponentTheme.BACKGROUND_SECONDARY = '#F0F0F0' // 未选中状态背景色
ComponentTheme.BORDER_RADIUS = 12 // 圆角大小A: 当前版本支持水平排列。如果需要垂直排列,可以:
ButtonGroup 垂直排列ButtonGroup 是控件库中的按钮组组件,具有以下核心特性:
mode 属性选择单选或多选模式buttonSize 属性选择合适尺寸items 属性配置按钮项disabled 属性控制按钮组状态textIcon 或 icon 添加图标ComponentTheme 自定义全局样式下一篇预告:TextInput(文本输入框)详解
本文档属于《鸿蒙PC UI控件库开发系列文章》第6篇