harmony 鸿蒙Checkbox

2022-08-09 浏览 (1093)

Checkbox

The <Checkbox> component is used to enable or disable an option.

NOTE

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

Child Components

Not supported

APIs

Checkbox(options?: {name?: string, group?: string })

Since API version 9, this API is supported in ArkTS widgets.

Parameters

NameTypeMandatoryDescription
namestringNoName of the check box.
groupstringNoGroup name of the check box.
NOTE
If not used with the <CheckboxGroup> component, this parameter is invalid.

Attributes

In addition to the universal attributes, the following attributes are supported.

NameTypeDescription
selectbooleanWhether the check box is selected.
Default value: false
Since API version 9, this API is supported in ArkTS widgets.
Since API version 10, this attribute supports $$ for two-way binding of variables.
selectedColorResourceColorColor of the check box when it is selected.
Since API version 9, this API is supported in ArkTS widgets.
unselectedColor10+ResourceColorBorder color of the check box when it is not selected.
mark10+MarkStyleInternal icon style of the check box.

Events

In addition to the universal events, the following attributes are supported.

NameDescription
onChange(callback: (value: boolean) => void)Triggered when the selected status of the check box changes.
- The value true means that the check box is selected.
- The value false means that the check box is not selected.
Since API version 9, this API is supported in ArkTS widgets.

MarkStyle10+

NameTypeMandatoryDefault ValueDescription
strokeColorResourceColorNoColor.WhiteColor of the internal mark.
sizenumber |stringNo-Size of the internal mark, in vp. The default size is the same as the width of the check box.
This parameter cannot be set in percentage. If it is set to an invalid value, the default value is used.
strokeWidthnumber |stringNo2Stroke width of the internal mark, in vp. This parameter cannot be set in percentage. If it is set to an invalid value, the default value is used.

Example

Example 1

// xxx.ets
@Entry
@Component
struct CheckboxExample {

  build() {
    Row() {
      Checkbox({name: 'checkbox1',  group: 'checkboxGroup'})
        .select(true)
        .selectedColor(0xed6f21)
        .onChange((value: boolean) => {
          console.info('Checkbox1 change is'+ value)
        })
      Checkbox({name: 'checkbox2',  group: 'checkboxGroup'})
        .select(false)
        .selectedColor(0x39a2db)
        .onChange((value: boolean) => {
          console.info('Checkbox2 change is'+ value)
        })
    }
  }
}

Example 2

// xxx.ets
@Entry
@Component
struct Index {

  build() {
    Row() {
      Column() {
        Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
          Checkbox({ name: 'checkbox1', group: 'checkboxGroup' })
            .selectedColor(0x39a2db)
            .onChange((value: boolean) => {
              console.info('Checkbox1 change is'+ value)
            })
            .mark({
              strokeColor:Color.Black,
              size: 50,
              strokeWidth: 5
            })
            .unselectedColor(Color.Red)
            .width(30)
            .height(30)
          Text('Checkbox1').fontSize(20)
        }
        Flex({ justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center }) {
          Checkbox({ name: 'checkbox2', group: 'checkboxGroup' })
            .selectedColor(0x39a2db)
            .onChange((value: boolean) => {
              console.info('Checkbox2 change is' + value)
            })
            .width(30)
            .height(30)
          Text('Checkbox2').fontSize(20)
        }
      }
      .width('100%')
    }
    .height('100%')
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkTS-based Declarative Development Paradigm

harmony 鸿蒙@ohos.multimedia.avCastPicker (AVCastPicker)

harmony 鸿蒙Property Animation

harmony 鸿蒙Enums

harmony 鸿蒙Blank

harmony 鸿蒙Button

harmony 鸿蒙CalendarPicker

harmony 鸿蒙CheckboxGroup

harmony 鸿蒙DataPanel

harmony 鸿蒙DatePicker

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