Skip to content

Mobile Button 移动端按钮 ​

提供适合手指触控的移动端按钮,支持主要、危险、加载、尺寸、块级和圆角样式。

何时使用 ​

  • 需要提交、确认、打开页面等明确操作时使用 mobile.Button。
  • 表单底部的主提交按钮通常使用 Primary().Block()。
  • 行内切换、导航或列表操作应使用对应的 Cell、Tabs 或 NavBar,不要全部改成按钮。
  • 正在等待异步结果时使用 Loading(true),避免重复提交。

在线体验 ​

真实 gallery 展示默认、主要、成功、危险、加载、尺寸、圆角和禁用按钮。

Mobile · 表单:Form / Field / Stepper / Button · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

按钮是否被点击由当前帧的 Clicked 返回;页面状态需要由调用方保存。

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

var saved bool

func page(c *ui.Context) {
    if mobile.Button(c, "保存").Primary().Clicked() {
        saved = true
    }
    if saved {
        ui.Text(c, "已保存")
    }
}

常见用法 ​

主要和块级按钮 ​

Primary 表示主要操作,Block 让按钮占据可用宽度。

go
func submitButton(c *ui.Context) {
    mobile.Button(c, "提交").Primary().Block().Size(mobile.Large)
}

颜色和填充 ​

Color 使用移动组件约定的颜色名,Fill("outline") 生成描边按钮。

go
func variants(c *ui.Context) {
    mobile.Button(c, "成功").Color("success")
    mobile.Button(c, "危险").Danger().Fill("outline")
}

尺寸和圆角 ​

尺寸使用 Mini、Small、Middle、Large;Round 适合短标签或胶囊式操作。

go
func sizes(c *ui.Context) {
    mobile.Button(c, "迷你").Size(mobile.Mini)
    mobile.Button(c, "小号").Size(mobile.Small)
    mobile.Button(c, "圆角").Round().Primary()
}

加载和禁用 ​

加载态会阻止重复点击;禁用态应由业务状态决定。

go
var submitting bool

func states(c *ui.Context) {
    if mobile.Button(c, "发送").Primary().Loading(submitting).Clicked() {
        submitting = true
    }
    mobile.Button(c, "不可用").Disabled(true)
}

图标和无障碍标签 ​

图标按钮或文案不完整时用 Icon 和 A11yLabel 补充语义。

go
import "github.com/tangoui/tango/ui/icon"

func iconButton(c *ui.Context) {
    mobile.Button(c, "").Icon(icon.Plus).A11yLabel("新建消息")
}

主题与样式 ​

Button 的主色、危险色、圆角、尺寸和加载态读取 Provider 的移动主题。Ant Mobile、Vant、shadcn 的填充和边框配方不同;ConfigProvider.Defaults("mobile.Button", ui.Props{"size": mobile.Large}) 可设置默认属性,显式方法优先。

注意事项 ​

Clicked 是当前帧事件,提交状态应放在稳定变量中。异步提交期间设置 Loading,完成或失败后恢复。按钮文案要描述动作,图标按钮必须设置 A11yLabel;确保触控区域足够大并支持 Enter、Space 和屏幕阅读器 Press。危险按钮应配合确认,而不是只依赖红色。

示例代码 ​

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

Mobile · 表单:Form / Field / Stepper / Button

gallery 演示 MobileForm 的 Render 函数,源码 page_mobile_gallery.go#L134。

加载中…

Mobile · 展示:Swiper / Skeleton / Empty / Result / Toast

gallery 演示 MobileDisplay 的 Render 函数,源码 page_mobile_gallery.go#L134。

加载中…

Mobile · 导航:NavBar / SearchBar / Tabs / NoticeBar / Grid / TabBar

gallery 演示 MobileNav 的 Render 函数,源码 page_mobile_gallery.go#L134。

加载中…

API ​

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

入口函数 ​

mobile.Button ​

go
func Button(c *ui.Context, label string) *ButtonW

Button adds a button labelled label. In the shadcn style the default color is the outline variant, "primary" the near-black default variant and "danger" the destructive one, with medium-weight labels and h-11 (Middle) touch heights.

ConfigProvider defaults ("mobile.Button"): "size" ("mini", "small", "middle", "large", a mobile.Size or a ui.ControlSize; otherwise the ConfigProvider size), "shape" ("round" or "default"), "fill" ("solid", "outline", "none"), "color" (string), "block" (bool).

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

mobile.Badge ​

go
func Badge(c *ui.Context, content string, fn func()) *ui.Box

Badge wraps content with a badge at its top-end corner (top right; top left in RTL): content is the text ("99+"), "dot" for a dot, or "" for none.

返回通用容器 *ui.Box,可用的布局与样式方法见 Box 的 API。

mobile.Tag ​

go
func Tag(c *ui.Context, label string) *TagW

Tag adds a tag (a shadcn Badge in the shadcn style: rounded-md, text-xs medium; "default" is the secondary variant).

ConfigProvider defaults ("mobile.Tag"): "color" (string), "fill" ("solid", "outline", "light"), "round" (bool).

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

mobile.Grid ​

go
func Grid(c *ui.Context, cols int, items []GridItem) *GridW

Grid adds a grid of items in cols columns. In the shadcn style the icons sit on muted rounded-xl tiles and cells have no separators.

ConfigProvider defaults ("mobile.Grid"): "bordered" (bool).

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

mobile.NoticeBar ​

go
func NoticeBar(c *ui.Context, msg string) *NoticeBarW

NoticeBar adds an announcement bar showing msg (a neutral shadcn Alert strip in the shadcn style).

ConfigProvider defaults ("mobile.NoticeBar"): "color" (string), "closeable", "scroll" (bool).

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

mobile.Skeleton ​

go
func Skeleton(c *ui.Context) *SkeletonW

Skeleton adds a placeholder; choose parts with Title, Paragraph, Avatar. The shadcn style pulses rounded-md muted bars instead of a shimmer.

ConfigProvider defaults ("mobile.Skeleton"): "title", "avatar", "animated" (bool), "rows" (int).

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

mobile.Empty ​

go
func Empty(c *ui.Context) *EmptyW

Empty adds an empty state with the localized "暂无数据" (shadcn: an icon on a muted tile above the muted caption).

ConfigProvider defaults ("mobile.Empty"): "description" (string).

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

mobile.Result ​

go
func Result(c *ui.Context, status, title string) *ResultW

Result adds an outcome block: status "success", "error", "info", "waiting" or "warning".

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

ButtonW 方法 ​

mobile.ButtonW:ButtonW is a touch button sized for fingers.

方法说明签名
A11yLabelA11yLabel sets the accessible name (icon-only buttons).A11yLabel(s string) *ButtonW
BlockBlock stretches the button to the full width.Block() *ButtonW
ClickedClicked reports a tap this frame.Clicked() bool
ColorColor sets the color: "default", "primary", "success", "warning", "danger".Color(s string) *ButtonW
DangerDanger is Color("danger").Danger() *ButtonW
DisabledDisabled greys the button out.Disabled(on bool) *ButtonW
FillFill sets "solid" (default), "outline" or "none".Fill(s string) *ButtonW
IDID returns the button's ID.ID() ui.ID
IconIcon adds a leading icon.Icon(ic *icon.Icon) *ButtonW
LayoutLayout applies layout options.Layout(opts ...layout.Option) *ButtonW
LoadingLoading shows a spinner and ignores taps.Loading(on bool) *ButtonW
PrimaryPrimary is Color("primary").Primary() *ButtonW
RoundRound makes the ends fully rounded.Round() *ButtonW
SizeSize sets the size.Size(s Size) *ButtonW

TagW 方法 ​

mobile.TagW:TagW is a small label.

方法说明签名
ColorColor sets "default", "primary", "success", "warning", "danger".Color(s string) *TagW
LightLight uses a tinted background with colored text.Light() *TagW
OutlineOutline draws a border instead of a solid fill.Outline() *TagW
RoundRound makes the tag pill-shaped.Round() *TagW

GridW 方法 ​

mobile.GridW:GridW is a grid of icon + text entries.

方法说明签名
BorderBorder toggles cell separators.Border(on bool) *GridW
ClickedClicked returns the index tapped this frame.Clicked() (int, bool)

NoticeBarW 方法 ​

mobile.NoticeBarW:NoticeBarW is a one-line announcement bar, optionally scrolling.

方法说明签名
CloseableCloseable shows a close button.Closeable() *NoticeBarW
ClosedClosed reports a tap on the close button this frame.Closed() bool
ColorColor sets "default" (orange), "alert", "error" or "info".Color(s string) *NoticeBarW
ScrollScroll makes long text scroll (marquee; towards the start, so to the right in RTL).Scroll() *NoticeBarW

SkeletonW 方法 ​

mobile.SkeletonW:SkeletonW is a loading placeholder of grey bars.

方法说明签名
AnimatedAnimated toggles the shimmer (on by default when animations run).Animated(on bool) *SkeletonW
AvatarAvatar shows a round avatar placeholder.Avatar() *SkeletonW
ParagraphParagraph sets the number of text rows.Paragraph(n int) *SkeletonW
TitleTitle shows a title bar.Title() *SkeletonW

EmptyW 方法 ​

mobile.EmptyW:EmptyW is an empty-state placeholder.

方法说明签名
DescriptionDescription replaces the caption.Description(s string) *EmptyW

ResultW 方法 ​

mobile.ResultW:ResultW is a full-width outcome page block.

方法说明签名
DescriptionDescription adds an explanation under the title.Description(s string) *ResultW

GridItem 参数类型 ​

mobile.GridItem:GridItem is one cell of a Grid.

字段说明类型
Textstring
Icon*icon.Icon
Badge"", "dot" or count textstring

相关链接 ​

以 MIT 许可发布