Skip to content

Radio 单选框 ​

在一组互斥的选项中选择一个。包含单个单选框 Radio 和单选框组 RadioGroup,组还支持按钮样式。

何时使用 ​

在线体验 ​

Radio 单个单选框 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。

go
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) 只负责显示和报告点击:传入当前是否选中,点击后由你更新状态。适合选项需要自定义排版(例如每项下方带说明)的场景。

go
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 对按钮和普通单选框都生效。

go
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 禁用单项。

go
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 入门。

Radio 单个单选框

gallery 演示 Radio 的 Render 函数,源码 page_fill_misc.go#L68。

加载中…

Radio 竖向与撑满

gallery 演示 RadioAlign 的 Render 函数,源码 page_b1b.go#L66。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Radio ​

go
func Radio(c *Context, label string, checked bool) *RadioW

Radio adds a radio button; it reports Clicked so the caller can select it.

返回 *RadioW,可链式调用的方法见 RadioW 方法。

ui.RadioGroup ​

go
func RadioGroup(c *Context, val *string, opts []Option) *RadioGroupW

RadioGroup adds radio options bound to *val.

返回 *RadioGroupW,可链式调用的方法见 RadioGroupW 方法。

RadioW 方法 ​

ui.RadioW:RadioW is a single radio button.

方法说明签名
BorderBorder draws a frame around the circle and the label (Element el-radio border); a checked one gets a brand-colored frame.Border() *RadioW
ClickedClicked reports activation.Clicked() bool
DisabledDisabled(on bool) *RadioW
SizeSize sets the radio size: the label font and the height of the Border style.Size(s ControlSize) *RadioW
StatusStatus sets the validation status ("error" marks the radio invalid).Status(s string) *RadioW

RadioGroupW 方法 ​

ui.RadioGroupW:RadioGroupW is a group of mutually exclusive options.

方法说明签名
A11yLabelA11yLabel names the group for screen readers (inside a FormItem the item label is used).A11yLabel(s string) *RadioGroupW
BlockBlock stretches the group to the full width and shares it equally among the options (antd block, typically with Buttons).Block() *RadioGroupW
BorderBorder draws each radio with a frame (see RadioW.Border); it has no effect on the button style.Border() *RadioGroupW
ButtonsButtons renders the group as segmented buttons.Buttons() *RadioGroupW
ChangedChanged reports a selection change this frame.Changed() bool
DisabledDisabled disables every option of the group (radios or buttons).Disabled(on bool) *RadioGroupW
OrientationOrientation sets the direction of the options: "horizontal" (default) or "vertical" (antd 6 orientation).Orientation(o string) *RadioGroupW
SizeSize sets the size of the buttons (and of the radios: label font and Border height).Size(s ControlSize) *RadioGroupW
SolidSolid fills the selected button with the primary color.Solid() *RadioGroupW
VerticalVertical 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.

字段说明类型
Labelstring
Valuestring
Disabledbool
Groupstring

相关链接 ​

以 MIT 许可发布