Radio 单选框
在一组互斥的选项中选择一个。包含单个单选框 Radio 和单选框组 RadioGroup,组还支持按钮样式。
何时使用
- 选项不多(一般不超过 5 个)、需要把所有选项平铺展示以便比较时。
- 只能选一个值;需要多选时用 Checkbox 多选框。
- 选项较多或空间有限时用 Select 选择器;作为视图切换(列表 / 看板)时用 Segmented 分段控制器。
在线体验
基本用法
下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。
import (
"github.com/tangoui/tango/ui"
)
var plan = "A"
func page(c *ui.Context) {
ui.RadioGroup(c, &plan, []ui.Option{
{Label: "选项 A", Value: "A"},
{Label: "选项 B", Value: "B"},
{Label: "选项 C", Value: "C"},
{Label: "禁用", Value: "D", Disabled: true},
}).A11yLabel("方案")
ui.Text(c, "当前:"+plan)
}RadioGroup 把选中项的 Value 绑定到 *string;Changed() 报告本帧是否切换了选项。ui.Opts("北京", "上海") 可以快速生成值与标签相同的选项。
常见用法
单个单选框
Radio(c, label, checked) 只负责显示和报告点击:传入当前是否选中,点击后由你更新状态。适合选项需要自定义排版(例如每项下方带说明)的场景。
import (
"github.com/tangoui/tango/ui"
)
var tier = "free"
func page(c *ui.Context) {
for _, p := range []struct{ v, label, desc string }{
{"free", "免费版", "适合个人试用"},
{"pro", "专业版", "适合小团队"},
} {
if ui.Radio(c, p.label, tier == p.v).Clicked() {
tier = p.v
}
ui.Text(c, p.desc).Secondary()
}
}按钮样式
Buttons() 把单选框组渲染成相连的按钮;Solid() 让选中按钮用主色填充(隐含 Buttons())。Size 对按钮和普通单选框都生效。
import (
"github.com/tangoui/tango/ui"
)
var city = "上海"
func page(c *ui.Context) {
ui.RadioGroup(c, &city, ui.Opts("北京", "上海", "广州", "深圳")).Buttons().A11yLabel("城市")
ui.RadioGroup(c, &city, ui.Opts("北京", "上海", "广州")).Solid().Size(ui.Small).A11yLabel("城市(实心)")
}边框与禁用
Border() 给每个单选框加边框(Element Plus el-radio border,对按钮样式无效);Disabled(true) 禁用整组,Option.Disabled 禁用单项。
import (
"github.com/tangoui/tango/ui"
)
var size = "M"
func page(c *ui.Context) {
ui.RadioGroup(c, &size, ui.Opts("S", "M", "L")).Border().A11yLabel("尺码")
ui.RadioGroup(c, &size, ui.Opts("S", "M", "L")).Buttons().Disabled(true).A11yLabel("尺码(禁用)")
}主题与样式
- Ant Design / shadcn 下默认为圆点加文字;按钮样式对应 antd
optionType="button",Solid对应buttonStyle="solid";Border对应 Element Plus 的带边框单选框。 - 组件令牌:
theme.Components.Radio(RadioToken)。 ConfigProvider.Defaults("Radio", ui.Props{...})支持size、optionType("button")、buttonStyle("solid"),可以把一个范围内的单选框组统一切换成按钮样式。
注意事项
- 状态:
RadioGroup的值就是你绑定的字符串;单个Radio不保存任何状态,选中与否完全由你传入的checked决定。 - 表单校验:放进 Form 时用
f.Item("类型").Rules(&kind, ui.Required("请选择类型"));字段联动时可用RulesFunc让表单通过f.Value(name)读取当前选项。 - 无障碍:单选框组在表单项里默认以表单项标签命名,单独使用时用
A11yLabel命名整组;Tab 聚焦后可用键盘激活选项。 - RTL:在 RTL 范围内圆点位于文字右侧,按钮组的顺序也随之镜像。
示例代码
以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Radio
func Radio(c *Context, label string, checked bool) *RadioWRadio adds a radio button; it reports Clicked so the caller can select it.
返回 *RadioW,可链式调用的方法见 RadioW 方法。
ui.RadioGroup
func RadioGroup(c *Context, val *string, opts []Option) *RadioGroupWRadioGroup adds radio options bound to *val.
返回 *RadioGroupW,可链式调用的方法见 RadioGroupW 方法。
RadioW 方法
ui.RadioW:RadioW is a single radio button.
| 方法 | 说明 | 签名 |
|---|---|---|
Border | Border draws a frame around the circle and the label (Element el-radio border); a checked one gets a brand-colored frame. | Border() *RadioW |
Clicked | Clicked reports activation. | Clicked() bool |
Disabled | Disabled(on bool) *RadioW | |
Size | Size sets the radio size: the label font and the height of the Border style. | Size(s ControlSize) *RadioW |
Status | Status sets the validation status ("error" marks the radio invalid). | Status(s string) *RadioW |
RadioGroupW 方法
ui.RadioGroupW:RadioGroupW is a group of mutually exclusive options.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel names the group for screen readers (inside a FormItem the item label is used). | A11yLabel(s string) *RadioGroupW |
Block | Block stretches the group to the full width and shares it equally among the options (antd block, typically with Buttons). | Block() *RadioGroupW |
Border | Border draws each radio with a frame (see RadioW.Border); it has no effect on the button style. | Border() *RadioGroupW |
Buttons | Buttons renders the group as segmented buttons. | Buttons() *RadioGroupW |
Changed | Changed reports a selection change this frame. | Changed() bool |
Disabled | Disabled disables every option of the group (radios or buttons). | Disabled(on bool) *RadioGroupW |
Orientation | Orientation sets the direction of the options: "horizontal" (default) or "vertical" (antd 6 orientation). | Orientation(o string) *RadioGroupW |
Size | Size sets the size of the buttons (and of the radios: label font and Border height). | Size(s ControlSize) *RadioGroupW |
Solid | Solid fills the selected button with the primary color. | Solid() *RadioGroupW |
Vertical | Vertical stacks the options in a column (antd 6 vertical); buttons join top to bottom. | Vertical() *RadioGroupW |
Option 参数类型
ui.Option:Option is an item of a Select / RadioGroup / CheckboxGroup. Group puts the option under a titled group in Select / MultiSelect / VirtualSelect dropdowns (Ant OptGroup, Element el-option-group); options of one group are listed together in order of the group's first appearance.
| 字段 | 说明 | 类型 |
|---|---|---|
Label | string | |
Value | string | |
Disabled | bool | |
Group | string |