Skip to content

TimePicker 时间选择框 ​

输入或选择一天中的某个时间。下拉面板由时 / 分 / 秒滚动列组成,支持步长、12 小时制和禁用时间;还提供时间范围 TimeRangePicker 和可单独嵌入的 TimePanel。

何时使用 ​

  • 需要用户输入一个时刻(提醒时间、营业开始时间)时。
  • 需要同时选择日期和时间时,用 DatePicker 中的 DateTimePicker;选择一段日期用 RangePicker。
  • 只能从固定间隔的几个时间点中选择(例如每 30 分钟一个档位)时,用下拉列表风格的 TimeSelect 更直接。

在线体验 ​

TimePicker 时间选择框 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

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

go
import (
	"time"

	"github.com/tangoui/tango/ui"
)

var remindAt time.Time

func page(c *ui.Context) {
	ui.TimePicker(c, &remindAt).Placeholder("请选择时间")
	if !remindAt.IsZero() {
		ui.Text(c, "提醒时间:"+remindAt.Format("15:04:05"))
	}
}

值绑定到 *time.Time,零值表示未选择;新选出的值的日期部分为当天。既可以在面板里滚动选择,也可以直接在输入框中键入时间。Changed() 报告本帧是否提交了新值。

常见用法 ​

格式与步长 ​

Format 使用 Go 时间布局决定显示哪些列:"15:04:05"(默认)、"15:04"(隐藏秒列)。HourStep、MinuteStep、SecondStep 或一次性的 Steps(时, 分, 秒) 设置每列的步长。

go
import (
	"time"

	"github.com/tangoui/tango/ui"
)

var open, slot time.Time

func page(c *ui.Context) {
	ui.TimePicker(c, &open).Format("15:04").MinuteStep(15)
	ui.TimePicker(c, &slot).Steps(1, 5, 10)
}

12 小时制 ​

Use12Hours() 显示 12 小时制:小时列为 12、01 … 11,并增加上午 / 下午列;默认格式变为 "03:04:05 PM",上午 / 下午的文字取自当前语言包。

go
import (
	"time"

	"github.com/tangoui/tango/ui"
)

var alarm time.Time

func page(c *ui.Context) {
	ui.TimePicker(c, &alarm).Use12Hours()
	ui.TimePicker(c, &alarm).Use12Hours().Format("03:04 PM")
}

禁用时间 ​

DisabledHours、DisabledMinutes、DisabledSeconds 标记不可选择也不可键入的时间(小时为 0–23)。HideDisabledOptions(true) 直接从列中隐藏被禁用的项,而不是置灰。

go
import (
	"time"

	"github.com/tangoui/tango/ui"
)

var appointment time.Time

func page(c *ui.Context) {
	ui.TimePicker(c, &appointment).Format("15:04").
		DisabledHours(func(h int) bool { return h < 9 || h >= 18 }).
		DisabledMinutes(func(h, m int) bool { return h == 12 }). // 午休
		HideDisabledOptions(true)
}

时间范围 TimeRangePicker ​

TimeRangePicker 绑定 *[2]time.Time,开始和结束各有一组时间列;步长、12 小时制和禁用时间的用法与 TimePicker 相同,Placeholder(开始, 结束) 设置两端占位符。

go
import (
	"time"

	"github.com/tangoui/tango/ui"
)

var businessHours [2]time.Time

func page(c *ui.Context) {
	ui.TimeRangePicker(c, &businessHours).Format("15:04").MinuteStep(15).
		Placeholder("开门时间", "打烊时间").
		DisabledHours(func(h int) bool { return h < 6 })
}

内嵌时间面板 TimePanel ​

TimePanel 是 TimePicker 的下拉面板本身,可以直接放在页面或自定义弹层里。面板可聚焦:↑ / ↓ 改变当前列的值,← / → 切换列。

go
import (
	"time"

	"github.com/tangoui/tango/ui"
)

var at time.Time

func page(c *ui.Context) {
	ui.TimePanel(c, &at).Format("15:04").Steps(1, 15, 1).Height(200)
	ui.Text(c, at.Format("15:04"))
}

尺寸、状态与外观 ​

Size(ui.Small | ui.Large)、Status("error" | "warning")、Variant("filled" | "borderless" | "underlined")、Width、AllowClear(false)、Disabled(true) 调整外观与交互,用法与其他输入类组件一致。

go
import (
	"time"

	"github.com/tangoui/tango/ui"
)

var t time.Time

func page(c *ui.Context) {
	ui.TimePicker(c, &t).Size(ui.Small)
	ui.TimePicker(c, &t).Status("error").Placeholder("错误状态")
	ui.TimePicker(c, &t).Variant("filled").AllowClear(false).Width(160)
}

主题与样式 ​

  • 输入框外观沿用输入类组件的令牌;下拉面板的列宽、单元格高度和选中色与日期面板共用 theme.Components.DatePicker(DatePickerToken)中的 TimeColumnWidth、TimeColumnHeight、TimeCellHeight、TimeCellHoverBg、TimeCellActiveBg 等,三套风格切换无需改代码。
  • ConfigProvider.Defaults("TimePicker", ui.Props{...}) 支持 format、placeholder、use12Hours、hourStep、minuteStep、secondStep、allowClear、width、size、variant、hideDisabledOptions;其中格式、12 小时制、步长和 hideDisabledOptions 对 TimePanel 同样生效。TimeRangePicker 使用 "TimeRangePicker" 键。

注意事项 ​

  • 日期部分:TimePicker 只关心时分秒,新值的日期为当天。保存或比较时只取 Hour()、Minute()、Second(),不要依赖日期部分。
  • 状态:值就是你绑定的 time.Time;下拉是否展开、正在键入的文本由组件按调用位置保存,循环渲染时用 ui.Key 区分。
  • 表单校验:放进 Form 时,用 RulesFunc(func() string { ... }) 把时间格式化成字符串再配合 ui.Required,或用 ui.RulesOf 配合 ui.CheckValue 直接检查 time.Time。
  • 无障碍:在表单项里自动以表单项标签命名,否则依次使用 A11yLabel、占位符;TimePanel 默认名称取自语言包的“选择时间”。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

TimePicker 时间选择框

gallery 演示 TimePicker 的 Render 函数,源码 page_entry_date.go#L57。

加载中…

TimePanel 时间面板

gallery 演示 TimePanel 的 Render 函数,源码 page_fill_misc.go#L101。

加载中…

TimePicker 12 小时制与禁用时间

gallery 演示 TimePicker12Hours 的 Render 函数,源码 page_p2a_date.go#L99。

加载中…

TimePicker 免确认、跨夜范围与时间点

gallery 演示 TimePickerAlign 的 Render 函数,源码 page_b1d.go#L62。

加载中…

API ​

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

入口函数 ​

ui.TimePicker ​

go
func TimePicker(c *Context, val *time.Time) *TimePickerW

TimePicker adds a time input bound to *val (zero = empty). The date part of a new value is today's date.

ConfigProvider.Defaults("TimePicker", ...) keys: "format", "placeholder", "use12Hours", "hourStep", "minuteStep", "secondStep", "allowClear", "width", "size", "variant", "hideDisabledOptions", "placement", "showNow", "needConfirm".

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

ui.TimePanel ​

go
func TimePanel(c *Context, val *time.Time) *TimePanelW

TimePanel adds hour/minute(/second) scroll columns bound to *val. It is focusable: Up/Down change the active column, Left/Right switch columns.

ConfigProvider.Defaults("TimePicker", ...) keys "format", "use12Hours", "hourStep", "minuteStep", "secondStep" and "hideDisabledOptions" apply.

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

TimePickerW 方法 ​

ui.TimePickerW:TimePickerW is an input with a dropdown of time columns.

方法说明签名
A11yLabelA11yLabel sets the accessible name (inside a FormItem the item label is used; the placeholder is the last resort).A11yLabel(s string) *TimePickerW
AllowClearAllowClear toggles the clear button (on by default).AllowClear(on bool) *TimePickerW
ChangedChanged reports a committed value this frame.Changed() bool
DisabledDisabled disables the picker.Disabled(on bool) *TimePickerW
DisabledHoursDisabledHours marks hours (0-23) that cannot be picked or typed.DisabledHours(fn func(h int) bool) *TimePickerW
DisabledMinutesDisabledMinutes marks minutes of hour h that cannot be picked or typed.DisabledMinutes(fn func(h, m int) bool) *TimePickerW
DisabledSecondsDisabledSeconds marks seconds of h:m that cannot be picked or typed.DisabledSeconds(fn func(h, m, s int) bool) *TimePickerW
FormatFormat sets the Go time layout, e.g. "15:04:05" (default), "15:04" or with Use12Hours "03:04 PM" (AM / PM are shown with the Locale words).Format(f string) *TimePickerW
HideDisabledOptionsHideDisabledOptions leaves disabled hours, minutes and seconds out of the columns instead of greying them (Ant hideDisabledOptions).HideDisabledOptions(on bool) *TimePickerW
HourStepHourStep(n int) *TimePickerW
IsOpenIsOpen reports whether the dropdown is shown.IsOpen() bool
LayoutLayout applies layout options to the input frame.Layout(o ...layout.Option) *TimePickerW
MinuteStepMinuteStep(n int) *TimePickerW
NeedConfirmNeedConfirm keeps column picks as a draft until OK or Enter (Ant needConfirm; on by default for time pickers).NeedConfirm(on bool) *TimePickerW
OpenOpen forces the dropdown open (screenshots, tests).Open() *TimePickerW
PlaceholderPlaceholder(s string) *TimePickerW
PlacementPlacement sets where the columns open: BottomStart (default), BottomEnd, TopStart or TopEnd (Ant placement); they flip when there is no room.Placement(p Placement) *TimePickerW
SecondStepSecondStep(n int) *TimePickerW
ShowNowShowNow toggles the Now link in the footer (Ant showNow, on by default).ShowNow(on bool) *TimePickerW
SizeSize(s ControlSize) *TimePickerW
StatusStatus(s string) *TimePickerW
StepsSteps sets the hour, minute and second steps at once.Steps(h, m, s int) *TimePickerW
Use12HoursUse12Hours shows a 12-hour clock: hours 12, 01 … 11 and an AM / PM column; the default format becomes "03:04:05 PM".Use12Hours() *TimePickerW
VariantVariant sets the frame look: "outlined" (default), "filled", "borderless" or "underlined".Variant(v string) *TimePickerW
WidthWidth(px float32) *TimePickerW

TimePanelW 方法 ​

ui.TimePanelW:TimePanelW is a standalone set of hour/minute/second columns.

方法说明签名
A11yLabelA11yLabel sets the accessible name (default: the Locale's SelectTime).A11yLabel(s string) *TimePanelW
ChangedChanged reports a new value this frame.Changed() bool
DisabledHoursDisabledHours marks hours (0-23) that cannot be picked.DisabledHours(fn func(h int) bool) *TimePanelW
DisabledMinutesDisabledMinutes marks minutes of hour h that cannot be picked.DisabledMinutes(fn func(h, m int) bool) *TimePanelW
DisabledSecondsDisabledSeconds marks seconds of h:m that cannot be picked.DisabledSeconds(fn func(h, m, s int) bool) *TimePanelW
FormatFormat sets the Go layout that decides the columns ("15:04" or "15:04:05").Format(f string) *TimePanelW
HeightHeight sets the column height.Height(px float32) *TimePanelW
HideDisabledOptionsHideDisabledOptions leaves disabled options out of the columns.HideDisabledOptions(on bool) *TimePanelW
StepsSteps sets the hour, minute and second steps.Steps(h, m, s int) *TimePanelW
Use12HoursUse12Hours shows a 12-hour clock with an AM / PM column.Use12Hours() *TimePanelW

TimeRangePicker 的方法见 Go API 参考,与 TimePicker 同名方法含义一致。

相关链接 ​

以 MIT 许可发布