文档管理中心
我的

Checkbox更改样式

问题现象

Checkbox组件在交互的过程中有默认样式,但根据界面设计需求,往往需要更改Checkbox的样式使界面更统一或更具辨识度。

  • 场景一:如何自定义边框的宽度?

    效果如下:

  • 场景二:如何修改Checkbox未选择状态时的背景?

背景知识

解决方案

  • 场景一:通过contentModifier属性自定义圆形选择框样式,使用Circle组件重绘选择框,设置strokeWidth属性可修改边框宽度。

    示例代码如下:

    收起
    自动换行
    深色代码主题
    复制
    1. class MyCheckboxStyle implements ContentModifier<CheckBoxConfiguration> {
    2. selectedColor: ResourceColor = '#0A59F7';
    3. cx: number = 100;
    4. cy: number = 100;
    5. r: number = 100;
    6. r2: number = 90;
    7. constructor(selectedColor: ResourceColor, cx: number, cy: number, r: number, r2: number) {
    8. this.selectedColor = selectedColor;
    9. this.cx = cx;
    10. this.cy = cy;
    11. this.r = r;
    12. this.r2 = r2;
    13. }
    14. applyContent(): WrappedBuilder<[CheckBoxConfiguration]> {
    15. return wrapBuilder(buildCheckbox);
    16. }
    17. }
    18. @Entry
    19. @Component
    20. struct CheckboxDemo {
    21. @State checkSelect: boolean = false;
    22. build() {
    23. Column() {
    24. Text(`复选框状态${this.checkSelect ? '( 选中 )' : '( 非选中 )'}`).margin({ bottom: 10 });
    25. Checkbox({ name: '复选框状态', group: 'checkboxGroup' })
    26. .select($$this.checkSelect)
    27. .contentModifier(new MyCheckboxStyle('#0A59F7', 100, 120, 100, 80)) // 自定义选择框样式
    28. .onChange((value: boolean) => {
    29. console.info(`Checkbox change is${value}`);
    30. });
    31. }
    32. .alignItems(HorizontalAlign.Center)
    33. .justifyContent(FlexAlign.Center)
    34. .height('100%')
    35. .width('100%');
    36. }
    37. }
    38. @Builder
    39. function buildCheckbox(config: CheckBoxConfiguration) {
    40. Circle()
    41. .fill(config.selected ? (config.contentModifier as MyCheckboxStyle).selectedColor : Color.White)
    42. .width(50)
    43. .height(50)
    44. .strokeWidth(5) // 设置边框宽度
    45. .stroke('#000') // 设置边框颜色
    46. .onClick(() => {
    47. config.triggerChange(!config.selected);
    48. });
    49. }

    示例代码运行效果如下:

  • 场景二:通过contentModifier属性自定义圆形选择框样式,传入未选中时的默认背景图片,在点击事件中对当前背景图片进行取反,且使用CheckBoxConfiguration的triggerChange方法触发多选框选中状态变化从而实现背景的修改。

    示例代码如下:

    收起
    自动换行
    深色代码主题
    复制
    1. class MyCheckboxStyleTwo implements ContentModifier<CheckBoxConfiguration> {
    2. selectedColor: Resource;
    3. constructor(selectedColor: Resource) {
    4. this.selectedColor = selectedColor;
    5. }
    6. applyContent(): WrappedBuilder<[CheckBoxConfiguration]> {
    7. return wrapBuilder(buildCheckboxTwo);
    8. }
    9. }
    10. @Entry
    11. @Component
    12. struct ModifierCheckBoxTwo {
    13. build() {
    14. Row() {
    15. Column() {
    16. Checkbox({ name: '复选框状态', group: 'checkboxGroup' })
    17. .contentModifier(new MyCheckboxStyleTwo($r('app.media.green'))) // 自定义选择框样式,根据具体场景传入默认背景
    18. .onChange((value: boolean) => {
    19. console.info(`Checkbox change is${value}`);
    20. });
    21. }
    22. .width('100%');
    23. }
    24. .height('100%');
    25. }
    26. }
    27. @Builder
    28. function buildCheckboxTwo(config: CheckBoxConfiguration) {
    29. Column() {
    30. Text(config.name + (config.selected ? '(选中)' : '(非选中)')).margin({ bottom: 8 });
    31. Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
    32. }
    33. .onClick(() => {
    34. // 对当前背景图片进行取反操作
    35. (config.contentModifier as MyCheckboxStyleTwo).selectedColor =
    36. !config.selected ? $r('app.media.startIcon') : $r('app.media.background'); // 根据具体场景加载资源
    37. // 判断多选框的选择状态
    38. config.triggerChange(!config.selected);
    39. })
    40. .width(50)
    41. .height(50)
    42. .borderRadius(5)
    43. .backgroundImageSize({ width: '100%', height: '100%' })
    44. // 示例效果:未选中状态时的背景图片为绿色图片,经过自定义后未选中状态时的背景图片为蓝色图片
    45. .backgroundImage((config.contentModifier as MyCheckboxStyleTwo).selectedColor); // 设置背景,根据具体场景加载资源
    46. };
    47. }

    示例代码运行效果如下:

常见FAQ

Q:如何为Checkbox组件设置选中颜色?

A:可以使用selectedColor属性来设置Checkbox选中时的颜色,参考官方文档:示例2(设置多选框颜色)实现。

Q:已经通过shape(CheckBoxShape.ROUNDED_SQUARE)属性给Checkbox设置了圆角方形,如何改变圆角大小?

A:参考官方文档:示例3(自定义多选框样式)实现。