在ArkUI框架的应用开发过程中,实现多选功能是常见的交互需求。开发过程中通常面临以下具体场景:
@ObservedV2
class Person {
@Trace public name: boolean;
public value: number;
constructor(name: boolean, value: number) {
this.name = name;
this.value = value;
}
}
@ObservedV2
class Info {
// 存储Person对象的数组
personList: Person[] = [];
constructor() {
this.personList = [new Person(false, 0), new Person(false, 1), new Person(false, 2), new Person(false, 3)
];
}
}
@Entry
@Component
struct CheckboxPage1 {
// 创建Info实例作为组件状态
info: Info = new Info();
build() {
Column() {
// 反选按钮
Button() {
Text('反选')
.fontColor(Color.White);
}
.width(60)
.height(30)
.margin(10)
.onClick(() => {
// 实现反选功能
for (let i = 0; i < this.info.personList.length; i++) {
this.info.personList[i].name = !this.info.personList[i].name;
}
});
// 使用List组件显示复选框列表
List({ space: 5, initialIndex: 0 }) {
ForEach(this.info.personList, (item: Person) => {
ListItem() {
Flex() {
// Checkbox组件,显示选择状态
Checkbox({ name: item.value.toString() })
.selectedColor('#027cff')
.shape(CheckBoxShape.ROUNDED_SQUARE)
.unselectedColor('#027cff')
.select(item.name)
.onChange((value: boolean) => {
// 复选框状态改变时更新Person对象的name属性
item.name = value;
})
.width(18)
.height(18);
Text('Checkbox' + item.value.toString())
.fontSize(15)
.margin({ top: 5 });
};
};
}, (item: Person) => JSON.stringify(item));
};
}
.width('100%')
.height('100%')
.margin({ top: 50 })
.padding({ left: 24, right: 24 });
}
} 效果预览:

方案 | 适用场景 | 优点 | 缺点 |
|---|---|---|---|
方案一:CheckboxGroup+LazyForEach | 适用于长列表,如商品列表、文件列表等。 | 代码结构清晰,易于维护。 |
|
方案二:CheckboxGroup+分组控制 | 适用于分组数据,如章节下的题目、分类下的子项等。 | 支持分组选择。 |
|
interface GroupData {
groupName: string;
items: string[];
isGlobalSelectedAll: boolean;
}
@Entry
@Component
struct CheckboxPage2 {
// 数据源
groupDataList: Array<GroupData> = [
{ groupName: '第一章', items: ['1.1', '1.2', '1.3', '1.4'], isGlobalSelectedAll: false },
{ groupName: '第二章', items: ['2.1', '2.2', '2.3'], isGlobalSelectedAll: false }
];
@State isGroupAllSelected: boolean[] = [false, false];
@State isGlobalSelectedAll: boolean = false;
@State selectedItems: Array<Array<string>> = [[], []];
@Builder
buildGroupSection(data: GroupData, index: number) {
Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
CheckboxGroup({ group: data.groupName })
.checkboxShape(CheckBoxShape.ROUNDED_SQUARE)
.selectedColor('#007DFF')
.selectAll(this.isGroupAllSelected[index])
.onChange((event: CheckboxGroupResult) => {
this.groupDataList[index].isGlobalSelectedAll = (event.status === SelectStatus.All);
this.isGlobalSelectedAll = true;
for (let item of this.groupDataList) {
this.isGlobalSelectedAll = (this.isGlobalSelectedAll && item.isGlobalSelectedAll);
}
// 保存当前分组所选数据
this.selectedItems[index] = event.name;
});
Text(data.groupName).fontSize(14).lineHeight(20).fontColor('#182431').fontWeight(500);
};
ForEach(data.items, (item: string) => {
Flex({ justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
Checkbox({ name: item, group: data.groupName })
.selectedColor('#007DFF')
.shape(CheckBoxShape.ROUNDED_SQUARE)
.onChange((value: boolean) => {
console.info(`Checkbox1 change is ${value}`);
});
Text(item).fontSize(14).lineHeight(20).fontColor('#182431').fontWeight(500);
}.margin({ left: 36 });
}, (item: string) => item);
}
build() {
Column() {
ForEach(this.groupDataList, (item: GroupData, index: number) => {
this.buildGroupSection(item, index);
}, (item: GroupData) => JSON.stringify(item));
Flex({ alignItems: ItemAlign.Center }) {
Checkbox({ name: '全选' })
.select(this.isGlobalSelectedAll)
.selectedColor('#007DFF')
.shape(CheckBoxShape.ROUNDED_SQUARE)
.onClick(() => {
// 点击全选按钮,改变每个CheckBoxGroup选择状态
this.isGlobalSelectedAll = !this.isGlobalSelectedAll;
for (let i = 0; i < this.isGroupAllSelected.length; i++) {
this.isGroupAllSelected[i] = !this.isGroupAllSelected[i];
this.isGroupAllSelected[i] = this.isGlobalSelectedAll;
}
});
Text('全选');
};
}
.padding(16);
}
} 效果预览:
