跳到主要内容
版本:v6

ion-radio

shadow

单选按钮(Radio)应在单选按钮组(radio group)内部使用。按下一个单选按钮将选中它并取消选中之前选中的单选按钮(如果有的话)。也可以通过将父级单选按钮组的 value 属性设置为该单选按钮的值,以编程方式选中它们。

当单选按钮在单选按钮组内时,任何时候只有一个单选按钮被选中。如果应该选择多个项目,则应使用复选框(checkbox)。可以禁用组内的单选按钮以防止与其交互。

基本用法

标签位置

开发者可以使用 labelPlacement 属性控制标签相对于控件的位置。此属性对应 flexbox 的 flex-direction 属性。

标签换行

无论标签位置如何,长文本默认不会换行。如果单选按钮的宽度受限,溢出的文本将用省略号截断。你可以通过向单选按钮文本周围的包装器添加 ion-text-wrap 类,或使用 ::part() 选择器设置 label 阴影部分的样式来启用文本换行。

对象值引用

默认情况下,单选按钮组使用严格相等(===)来确定是否选择了选项。可以通过为 compareWith 属性提供属性名称或函数来覆盖此行为。

控制台
控制台消息将在上方示例中调用 console.log 时显示在此处。

对齐

开发者可以使用 alignment 属性控制标签和控件在交叉轴上的对齐方式。此属性对应 flexbox 的 align-items 属性。

备注

堆叠的单选按钮可以使用 alignment 属性进行对齐。当标签和控件需要在水平方向上居中时,这很有用。

排列(Justify)

开发者可以使用 justify 属性控制标签和控件在一行上的排列方式。此属性对应 flexbox 的 justify-content 属性。

备注

ion-item 仅用于演示中强调 justify 的工作原理。justify 的正常运行不需要它。

取消选中单选按钮

默认情况下,单选按钮一旦选中就不能取消选中;再次按它会保持选中状态。可以通过在父级单选按钮组上使用 allowEmptySelection 属性来修改此行为,从而允许取消选中单选按钮。

主题

颜色

CSS 自定义属性

CSS 阴影部分

从旧版单选按钮语法迁移

Ionic 7.0 引入了更简单的单选按钮语法。这种新语法减少了设置单选按钮所需的样板代码,解决了无障碍访问问题,并改善了开发者体验。

开发者可以逐个迁移每个单选按钮。虽然开发者可以继续使用旧版语法,但我们建议尽快迁移。

使用现代语法

使用现代语法需要移除 ion-label 并将标签直接传递给 ion-radio。标签的位置可以使用 ion-radio 上的 labelPlacement 属性进行配置。标签和控件在一行上的排列方式可以使用 ion-radio 上的 justify 属性控制。

<!-- Basic -->

<!-- Before -->
<ion-item>
<ion-label>Radio Label</ion-label>
<ion-radio></ion-radio>
</ion-item>

<!-- After -->
<ion-item>
<ion-radio>Radio Label</ion-radio>
</ion-item>

<!-- Fixed Labels -->

<!-- Before -->
<ion-item>
<ion-label position="fixed">Radio Label</ion-label>
<ion-radio></ion-radio>
</ion-item>

<!-- After -->
<ion-item>
<ion-radio label-placement="fixed">Radio Label</ion-radio>
</ion-item>

<!-- Radio at the start of line, Label at the end of line -->

<!-- Before -->
<ion-item>
<ion-label slot="end">Radio Label</ion-label>
<ion-radio></ion-radio>
</ion-item>

<!-- After -->
<ion-item>
<ion-radio label-placement="end">Radio Label</ion-radio>
</ion-item>
备注

在之前的 Ionic 版本中,ion-itemion-radio 正常运行所必需的。从 Ionic 7.0 开始,仅当项目放置在 ion-list 中时,才应在 ion-item 中使用 ion-radio。此外,ion-radio 的正常运行不再需要 ion-item

使用旧版语法

Ionic 使用启发式方法检测应用是否在使用现代单选按钮语法。在某些情况下,继续使用旧版语法可能更可取。开发者可以将 ion-radio 上的 legacy 属性设置为 true,以强制该单选按钮实例使用旧版语法。

属性

alignment

说明How to control the alignment of the radio and label on the cross axis. "start": The label and control will appear on the left of the cross axis in LTR, and on the right side in RTL. "center": The label and control will appear at the center of the cross axis in both LTR and RTL.
属性alignment
类型"center" | "start"
默认值'center'

color

说明The color to use from your application's color palette. Default options are: "primary", "secondary", "tertiary", "success", "warning", "danger", "light", "medium", and "dark". For more information on colors, see theming.
属性color
类型"danger" | "dark" | "light" | "medium" | "primary" | "secondary" | "success" | "tertiary" | "warning" | string | undefined
默认值undefined

disabled

说明If true, the user cannot interact with the radio.
属性disabled
类型boolean
默认值false

justify

说明How to pack the label and radio within a line. "start": The label and radio will appear on the left in LTR and on the right in RTL. "end": The label and radio will appear on the right in LTR and on the left in RTL. "space-between": The label and radio will appear on opposite ends of the line with space between the two elements.
属性justify
类型"end" | "space-between" | "start"
默认值'space-between'

labelPlacement

说明Where to place the label relative to the radio. "start": The label will appear to the left of the radio in LTR and to the right in RTL. "end": The label will appear to the right of the radio in LTR and to the left in RTL. "fixed": The label has the same behavior as "start" except it also has a fixed width. Long text will be truncated with ellipses ("..."). "stacked": The label will appear above the radio regardless of the direction. The alignment of the label can be controlled with the alignment property.
属性label-placement
类型"end" | "fixed" | "stacked" | "start"
默认值'start'

legacy

说明Set the legacy property to true to forcibly use the legacy form control markup. Ionic will only opt components in to the modern form markup when they are using either the aria-label attribute or the default slot that contains the label text. As a result, the legacy property should only be used as an escape hatch when you want to avoid this automatic opt-in behavior. Note that this property will be removed in an upcoming major release of Ionic, and all form components will be opted-in to using the modern form markup.
属性legacy
类型boolean | undefined
默认值undefined

mode

说明The mode determines which platform styles to use.

这是一个虚拟属性,在初始化时设置一次,之后更改其值不会更新组件。
属性mode
类型"ios" | "md"
默认值undefined

name

说明The name of the control, which is submitted with the form data.
属性name
类型string
默认值this.inputId

value

说明the value of the radio.
属性value
类型any
默认值undefined

事件

Name说明冒泡
ionBlurEmitted when the radio button loses focus.true
ionFocusEmitted when the radio button has focus.true

方法

该组件没有可用的公共方法。

CSS 阴影部分

Name说明
containerThe container for the radio mark.
labelThe label text describing the radio.
markThe checkmark or dot used to indicate the checked state.

CSS 自定义属性

Name说明
--border-radiusBorder radius of the radio
--colorColor of the radio
--color-checkedColor of the checked radio
--inner-border-radiusBorder radius of the inner checked radio

插槽

Name说明
The label text to associate with the radio. Use the "labelPlacement" property to control where the label is placed relative to the radio.