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 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。
完整 API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Input
go
func Input(c *Context, val *string) *InputWInput adds a text input bound to *val.
返回 *InputW,可链式调用的方法见 InputW 方法。
InputW 方法
ui.InputW:InputW is a single-line text input.
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name. | A11yLabel(s string) *InputW |
AllowClear | AllowClear shows a clear button when the input has a value. | AllowClear() *InputW |
Bg | Bg replaces the background color of the frame (a grey search field on a white bar). | Bg(col theme.Color) *InputW |
Borderless | Borderless drops the frame (background, border and focus halo), for inputs embedded in another surface such as the Command search row. | Borderless() *InputW |
Changed | Changed reports whether the value was edited this frame. | Changed() bool |
Disabled | Disabled disables editing. | Disabled(on bool) *InputW |
EnterKey | EnterKey labels the soft keyboard's return key (input.EnterSearch, input.EnterNext, …). | EnterKey(k input.EnterKey) *InputW |
Focus | Focus requests keyboard focus. | Focus() *InputW |
Focused | Focused reports keyboard focus. | Focused() bool |
ID | ID returns the editor's widget ID. | ID() ID |
Kind | Kind tells soft keyboards what the field holds (input.TextEmail, input.TextNumber, …): the keyboard layout and whether it suggests words. | Kind(k input.TextKind) *InputW |
Layout | Layout applies layout options to the outer frame. | Layout(opts ...layout.Option) *InputW |
MaxLength | MaxLength limits the number of runes. | MaxLength(n int) *InputW |
NoBorder | NoBorder drops the border and the focus halo but keeps the background (Bg or the variant's), e.g. a filled search bar. | NoBorder() *InputW |
Password | Password masks the value and adds a visibility toggle. | Password() *InputW |
Placeholder | Placeholder(s string) *InputW | |
Prefix | Prefix(ic *icon.Icon) *InputW | |
PrefixText | PrefixText / SuffixText show short static text (units, currency). | PrefixText(s string) *InputW |
ReadOnly | ReadOnly prevents edits but allows selection. | ReadOnly() *InputW |
SelectAll | SelectAll selects the full text of the input. | SelectAll() *InputW |
ShowCount | ShowCount shows the character count ("3 / 20" with MaxLength) at the end of the field (Ant showCount, Element show-word-limit). | ShowCount() *InputW |
Size | Size sets the control size. | Size(s ControlSize) *InputW |
Status | Status sets the validation status: "error" or "warning". | Status(s string) *InputW |
Submitted | Submitted reports an Enter key press. | Submitted() bool |
Suffix | Suffix(ic *icon.Icon) *InputW | |
SuffixText | SuffixText(s string) *InputW | |
Variant | Variant 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 |
VisibilityToggle | VisibilityToggle 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 |
Width | Width fixes the width. | Width(px float32) *InputW |