RadioButton
用于单选控制的 Jetpack Compose RadioButton 组件。
For the complete documentation index, see llms.txt. Use this file to discover all available pages.
一个用于从一组选项中选择单个选项的单选按钮组件。映射到官方 Jetpack Compose RadioButton API。
🌐 A radio button component for selecting a single option from a set. Maps to the official Jetpack Compose RadioButton API.
安装
🌐 Installation
- npx expo install @expo/uiIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
🌐 Usage
基本单选按钮
🌐 Basic radio button
一个带有 onClick 处理程序的独立单选按钮。
🌐 A standalone radio button with an onClick handler.
import { useState } from 'react'; import { Host, RadioButton } from '@expo/ui/jetpack-compose'; export default function BasicRadioButton() { const [selected, setSelected] = useState(false); return ( <Host matchContents> <RadioButton selected={selected} onClick={() => setSelected(!selected)} /> </Host> ); }
单选框组(推荐)
🌐 Radio group (recommended)
推荐的单选按钮组模式遵循 Compose 可访问性指南:
🌐 The recommended pattern for a radio group follows the Compose accessibility guidelines:
- 使用带有
selectableGroup()修饰符的Column封装该组,这样屏幕阅读器会将选项视为一个组。 - 对每个
Row使用带有role: 'radioButton'的selectable修饰符,使整行(包括标签)都可以点击。 - 不要将
onClick传递给RadioButton本身,行会处理交互。这提供了更大的触控目标。
import { useState } from 'react'; import { Host, Column, Row, RadioButton, Text } from '@expo/ui/jetpack-compose'; import { selectable, selectableGroup, fillMaxWidth, height, padding, } from '@expo/ui/jetpack-compose/modifiers'; export default function RadioGroup() { const [selectedOption, setSelectedOption] = useState('Calls'); const options = ['Calls', 'Missed', 'Friends']; return ( <Host matchContents> <Column modifiers={[selectableGroup()]}> {options.map(label => ( <Row key={label} verticalAlignment="center" modifiers={[ fillMaxWidth(), height(56), selectable(label === selectedOption, () => setSelectedOption(label), 'radioButton'), padding(16, 0, 16, 0), ]}> <RadioButton selected={label === selectedOption} /> <Text modifiers={[padding(16, 0, 0, 0)]}>{label}</Text> </Row> ))} </Column> </Host> ); }
应用接口
🌐 API
import { RadioButton } from '@expo/ui/jetpack-compose';
Component
Type: React.Element<RadioButtonProps>
A Material Design radio button.