Skip to content

Input 输入框 ​

编辑单行文本。文本由应用中的 string 变量持有,Input 接收指针并在编辑时更新它。

在线体验 ​

点击输入框并输入文字,试试 Backspace、方向键、全选和清空按钮。 工具栏支持 Ant Design、Element Plus、shadcn 以及深浅色切换;重置会恢复初始状态。

Input 输入框 · 在线体验真实 Go 组件
在独立页面打开 ↗

基本用法 ​

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

go
import (
    "github.com/tangoui/tango/ui"
)

var name string

func page(c *ui.Context) {
    ui.Input(c, &name).
        Placeholder("Type here...").
        AllowClear()
    ui.Text(c, "Value: " + name)
}

常用参数与方法 ​

参数 / 方法说明
Placeholder(string)空值时的占位提示
AllowClear()显示清空按钮
MaxLength(int)限制输入长度
ReadOnly()允许查看但不可编辑
Disabled(bool)禁用输入框
Password().VisibilityToggle(false)密码输入,隐藏默认的可见性切换按钮
Changed() bool读取当前帧是否发生内容修改

使用说明 ​

值必须保存在页面函数外,或通过 ui.State 持有,避免每帧重置。网页输入通过浏览器输入法桥接,支持组合输入;预览已内置中文演示字形,更多字符可以导入本地字体。

更多场景 ​

官方 gallery 里这个组件的全部示例(基础、进阶与组合场景),可切换场景、主题和深浅色。

Input 输入框 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

实现与更多示例 ​

全部示例代码 ​

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

Input 输入框

gallery 演示 Input 的 Render 函数,源码 page_form.go#L58。

加载中…

Input 字数统计、外观变体与自定义底色

gallery 演示 InputVariants 的 Render 函数,源码 page_p2d_entry.go#L123。

加载中…

完整 API ​

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

入口函数 ​

ui.Input ​

go
func Input(c *Context, val *string) *InputW

Input adds a text input bound to *val.

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

InputW 方法 ​

ui.InputW:InputW is a single-line text input.

方法说明签名
A11yLabelA11yLabel sets the accessible name.A11yLabel(s string) *InputW
AllowClearAllowClear shows a clear button when the input has a value.AllowClear() *InputW
BgBg replaces the background color of the frame (a grey search field on a white bar).Bg(col theme.Color) *InputW
BorderlessBorderless drops the frame (background, border and focus halo), for inputs embedded in another surface such as the Command search row.Borderless() *InputW
ChangedChanged reports whether the value was edited this frame.Changed() bool
DisabledDisabled disables editing.Disabled(on bool) *InputW
EnterKeyEnterKey labels the soft keyboard's return key (input.EnterSearch, input.EnterNext, …).EnterKey(k input.EnterKey) *InputW
FocusFocus requests keyboard focus.Focus() *InputW
FocusedFocused reports keyboard focus.Focused() bool
IDID returns the editor's widget ID.ID() ID
KindKind tells soft keyboards what the field holds (input.TextEmail, input.TextNumber, …): the keyboard layout and whether it suggests words.Kind(k input.TextKind) *InputW
LayoutLayout applies layout options to the outer frame.Layout(opts ...layout.Option) *InputW
MaxLengthMaxLength limits the number of runes.MaxLength(n int) *InputW
NoBorderNoBorder drops the border and the focus halo but keeps the background (Bg or the variant's), e.g. a filled search bar.NoBorder() *InputW
PasswordPassword masks the value and adds a visibility toggle.Password() *InputW
PlaceholderPlaceholder(s string) *InputW
PrefixPrefix(ic *icon.Icon) *InputW
PrefixTextPrefixText / SuffixText show short static text (units, currency).PrefixText(s string) *InputW
ReadOnlyReadOnly prevents edits but allows selection.ReadOnly() *InputW
SelectAllSelectAll selects the full text of the input.SelectAll() *InputW
ShowCountShowCount shows the character count ("3 / 20" with MaxLength) at the end of the field (Ant showCount, Element show-word-limit).ShowCount() *InputW
SizeSize sets the control size.Size(s ControlSize) *InputW
StatusStatus sets the validation status: "error" or "warning".Status(s string) *InputW
SubmittedSubmitted reports an Enter key press.Submitted() bool
SuffixSuffix(ic *icon.Icon) *InputW
SuffixTextSuffixText(s string) *InputW
VariantVariant sets the look of the frame (Ant Input variant): "outlined" (default), "filled" (grey fill, border on focus), "borderless" (no frame) or "underlined" (bottom line only).Variant(v string) *InputW
VisibilityToggleVisibilityToggle shows (true, the default) or hides the eye button of a Password field (antd Input.Password visibilityToggle; Element type="password" without show-password).VisibilityToggle(on bool) *InputW
WidthWidth fixes the width.Width(px float32) *InputW

以 MIT 许可发布