Reference version

This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 55).

复选框

一个用于选择控件的 Jetpack Compose 复选框组件。

Android

For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.

Expo UI Checkbox 与官方 Jetpack Compose Checkbox API 保持一致。

安装

Terminal
npx expo install @expo/ui

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

基础复选框

CheckboxExample.tsx
import { useState } from 'react'; import { Host, Checkbox } from '@expo/ui/jetpack-compose'; export default function CheckboxExample() { const [checked, setChecked] = useState(false); return ( <Host matchContents> <Checkbox value={checked} onCheckedChange={setChecked} /> </Host> ); }

自定义颜色

CustomColorsExample.tsx
import { useState } from 'react'; import { Host, Checkbox } from '@expo/ui/jetpack-compose'; export default function CustomColorsExample() { const [checked, setChecked] = useState(false); return ( <Host matchContents> <Checkbox value={checked} onCheckedChange={setChecked} colors={{ checkedColor: '#6200EE', checkmarkColor: '#FFFFFF', }} /> </Host> ); }

全选(TriStateCheckbox)

当父复选框需要反映其子项的状态时,请使用 TriStateCheckbox。它支持三种状态:'on''off''indeterminate'

toggleable 修饰符应用到每个 Row,使整行(复选框 + 标签)都可点击,并具备正确的可访问性语义。在行上使用 toggleable 时,请从复选框本身省略 onCheckedChange/onClick,以避免重复处理。

SelectAllExample.tsx
import { useState } from 'react'; import { Host, Checkbox, TriStateCheckbox, Row, Column, Text } from '@expo/ui/jetpack-compose'; import { toggleable } from '@expo/ui/jetpack-compose/modifiers'; export default function SelectAllExample() { const [child1, setChild1] = useState(false); const [child2, setChild2] = useState(false); const [child3, setChild3] = useState(false); const parentState = child1 && child2 && child3 ? 'on' : !child1 && !child2 && !child3 ? 'off' : 'indeterminate'; return ( <Host matchContents> <Column> <Row verticalAlignment="center" modifiers={[ toggleable( parentState === 'on', () => { const newState = parentState !== 'on'; setChild1(newState); setChild2(newState); setChild3(newState); }, { role: 'checkbox' } ), ]}> <TriStateCheckbox state={parentState} /> <Text>全选</Text> </Row> <Row verticalAlignment="center" modifiers={[toggleable(child1, () => setChild1(!child1), { role: 'checkbox' })]}> <Checkbox value={child1} /> <Text>选项 1</Text> </Row> <Row verticalAlignment="center" modifiers={[toggleable(child2, () => setChild2(!child2), { role: 'checkbox' })]}> <Checkbox value={child2} /> <Text>选项 2</Text> </Row> <Row verticalAlignment="center" modifiers={[toggleable(child3, () => setChild3(!child3), { role: 'checkbox' })]}> <Checkbox value={child3} /> <Text>选项 3</Text> </Row> </Column> </Host> ); }

API

import { Checkbox, TriStateCheckbox } from '@expo/ui/jetpack-compose';

Components

Checkbox

Android

Type: React.Element<CheckboxProps>

A checkbox component.

CheckboxProps

colors

Android
Optional • Type: CheckboxColors

Colors for checkbox core elements.

enabled

Android
Optional • Type: boolean • Default: true

Whether the checkbox is enabled.

modifiers

Android
Optional • Type: ModifierConfig[]

Modifiers for the component.

onCheckedChange

Android
Optional • Type: (value: boolean) => void

Callback function that is called when the checked state changes.

value

Android
Type: boolean

Indicates whether the checkbox is checked.

TriStateCheckbox

Android

Type: React.Element<TriStateCheckboxProps>

A tri-state checkbox component that supports 'on', 'off', and 'indeterminate' states. Useful for "select all" patterns where the parent checkbox reflects the state of its children.

TriStateCheckboxProps

colors

Android
Optional • Type: CheckboxColors

Colors for checkbox core elements.

enabled

Android
Optional • Type: boolean • Default: true

Whether the checkbox is enabled.

modifiers

Android
Optional • Type: ModifierConfig[]

Modifiers for the component.

onClick

Android
Optional • Type: () => void

Callback function that is called when the checkbox is clicked.

state

Android

The toggleable state of the checkbox: 'on', 'off', or 'indeterminate'.

Types

CheckboxColors

Android

Colors for checkbox core elements.

PropertyTypeDescription
checkedColor(optional)ColorValue
-
checkmarkColor(optional)ColorValue
-
disabledCheckedColor(optional)ColorValue
-
disabledIndeterminateColor(optional)ColorValue
-
disabledUncheckedColor(optional)ColorValue
-
uncheckedColor(optional)ColorValue
-

ToggleableState

Android

Literal Type: string

The toggleable state of a tri-state checkbox.

Acceptable values are: 'on' | 'off' | 'indeterminate'