Gomaton
指南
API
GitHub
指南
API
GitHub
  • 前言

    • 快速开始
    • 能力速查
    • 工作原理
    • 硬件输入后端(ESP32 HID)
  • API 参考

    • motion - 触控 / 输入
    • images - 找图找色 / 截图
    • uiacc - 控件(UI 树)
    • device - 设备
    • app - 应用
    • ime - 输入法
    • ocr - 文字识别
    • yolo - 目标检测
    • files - 文件
    • storages - 键值存储
    • https - 网络
    • media - 媒体
    • system - 系统
    • utils - 工具

uiacc — 控件(UI 树)

import "gomaton.dev/sdk/uiacc"

Package uiacc 控件树(UiAutomation,不需要开无障碍)。API 形态对标 AutoGo:

if o := uiacc.New().TextContains("蓝牙").WaitFor(5000); o != nil { o.Click() }
uiacc.New().ClassName("android.widget.EditText").FindOnce().SetText("你好")
for _, o := range uiacc.New().Clickable(true).Find() { … }

实现是【胶水】:控件树、注入、performAction 的真身在 utils.dex(二进制); Go 发桥命令(单条件直接 performAction)或取树后本地匹配再点中心。

ChildrenOf

func ChildrenOf(nodes []Node, n Node) []Node

ChildrenOf 在一次 Dump 的数组里取 n 的全部直接子节点。

Copy

func Copy() bool

Copy 对当前焦点输入框执行复制。

Cut

func Cut() bool

Cut 对当前焦点输入框执行剪切。

Dump

func Dump() []Node

Dump 取当前整棵控件树(原始扁平数组)。失败返回 nil。

New

func New() *Uiacc

New 新建选择器(对标 AutoGo uiacc.New)。

Node.CenterX

func (n Node) CenterX() int

CenterX 控件中心的 X 像素坐标。

Node.CenterY

func (n Node) CenterY() int

CenterY 控件中心的 Y 像素坐标。

Node.Height

func (n Node) Height() int

Height 控件像素高。

Node.IsRoot

func (n Node) IsRoot() bool

IsRoot 是否为根节点(无父节点)。

Node.Width

func (n Node) Width() int

Width 控件像素宽。

ParentOf

func ParentOf(nodes []Node, n Node) (Node, bool)

ParentOf 在一次 Dump 的数组里取 n 的父节点;无父节点(根)返回 (Node{}, false)。

Paste

func Paste() bool

Paste 对当前焦点输入框执行粘贴。

SetSelection

func SetSelection(start, end int) bool

SetSelection 设置当前焦点输入框选区 [start, end)。

UiObject.CenterX

func (o *UiObject) CenterX() int

CenterX 控件中心的 X 像素坐标。

UiObject.CenterY

func (o *UiObject) CenterY() int

CenterY 控件中心的 Y 像素坐标。

UiObject.Click

func (o *UiObject) Click() bool

Click 点击控件(优先 performAction,退到点中心)。

UiObject.Collapse

func (o *UiObject) Collapse() bool

Collapse 折叠控件(如可展开的列表项)。

UiObject.Expand

func (o *UiObject) Expand() bool

Expand 展开控件(如可展开的列表项)。

UiObject.GetBounds

func (o *UiObject) GetBounds() Rect

GetBounds 控件包围盒。

UiObject.GetChecked

func (o *UiObject) GetChecked() bool

GetChecked 控件是否已勾选。

UiObject.GetChildCount

func (o *UiObject) GetChildCount() int

GetChildCount 直接子节点数。

UiObject.GetChildren

func (o *UiObject) GetChildren() []*UiObject

GetChildren 直接子控件句柄。

UiObject.GetClassName

func (o *UiObject) GetClassName() string

GetClassName 控件类名。

UiObject.GetClickable

func (o *UiObject) GetClickable() bool

GetClickable 控件是否可点击。

UiObject.GetDesc

func (o *UiObject) GetDesc() string

GetDesc 控件 contentDescription。

UiObject.GetEditable

func (o *UiObject) GetEditable() bool

GetEditable 控件是否可编辑。

UiObject.GetId

func (o *UiObject) GetId() string

GetId 控件资源 id。

UiObject.GetIndex

func (o *UiObject) GetIndex() int

GetIndex 控件在父容器中的 index。

UiObject.GetPackageName

func (o *UiObject) GetPackageName() string

GetPackageName 控件所属应用包名。

UiObject.GetParent

func (o *UiObject) GetParent() *UiObject

GetParent 父控件句柄(无则 nil)。

UiObject.GetScrollable

func (o *UiObject) GetScrollable() bool

GetScrollable 控件是否可滚动。

UiObject.GetSelected

func (o *UiObject) GetSelected() bool

GetSelected 控件是否被选中。

UiObject.GetText

func (o *UiObject) GetText() string

GetText 控件文本。

UiObject.LongClick

func (o *UiObject) LongClick() bool

LongClick 长按控件。

UiObject.ScrollBackward

func (o *UiObject) ScrollBackward() bool

ScrollBackward 对控件向后(向上/向左)滚一页。

UiObject.ScrollForward

func (o *UiObject) ScrollForward() bool

ScrollForward 对控件向前(向下/向右)滚一页。

UiObject.SetText

func (o *UiObject) SetText(text string) bool

SetText 往输入框写文字。

UiObject.ToString

func (o *UiObject) ToString() string

ToString 便于调试。

Uiacc.Bounds

func (a *Uiacc) Bounds(l, t, r, b int) *Uiacc

Bounds 限定控件包围盒精确等于 l,t,r,b。

Uiacc.BoundsContains

func (a *Uiacc) BoundsContains(l, t, r, b int) *Uiacc

BoundsContains 限定控件包围盒完全包住矩形 [l,t,r,b]。

Uiacc.BoundsInside

func (a *Uiacc) BoundsInside(l, t, r, b int) *Uiacc

BoundsInside 限定控件包围盒完全落在矩形 [l,t,r,b] 内部。

Uiacc.Checkable

func (a *Uiacc) Checkable(v bool) *Uiacc

Checkable 限定 checkable 属性为 v。

Uiacc.Checked

func (a *Uiacc) Checked(v bool) *Uiacc

Checked 限定 checked 属性为 v。

Uiacc.ClassName

func (a *Uiacc) ClassName(v string) *Uiacc

ClassName 限定类名(如 android.widget.EditText)精确等于 v。

Uiacc.ClassNameEndsWith

func (a *Uiacc) ClassNameEndsWith(v string) *Uiacc

ClassNameEndsWith 限定类名以 v 结尾。

Uiacc.ClassNameMatches

func (a *Uiacc) ClassNameMatches(v string) *Uiacc

ClassNameMatches 限定类名匹配正则 v。

Uiacc.Click

func (a *Uiacc) Click() bool

Click 找到并点击第一个命中控件(便捷;等价 FindOnce().Click())。

Uiacc.Clickable

func (a *Uiacc) Clickable(v bool) *Uiacc

Clickable 限定 clickable 属性为 v。

Uiacc.Desc

func (a *Uiacc) Desc(v string) *Uiacc

Desc 限定 contentDescription 精确等于 v。

Uiacc.DescContains

func (a *Uiacc) DescContains(v string) *Uiacc

DescContains 限定 contentDescription 包含子串 v。

Uiacc.DescEndsWith

func (a *Uiacc) DescEndsWith(v string) *Uiacc

DescEndsWith 限定 contentDescription 以 v 结尾。

Uiacc.DescMatches

func (a *Uiacc) DescMatches(v string) *Uiacc

DescMatches 限定 contentDescription 匹配正则 v。

Uiacc.Editable

func (a *Uiacc) Editable(v bool) *Uiacc

Editable 限定 editable 属性为 v。

Uiacc.Enabled

func (a *Uiacc) Enabled(v bool) *Uiacc

Enabled 限定 enabled 属性为 v。

Uiacc.Exists

func (a *Uiacc) Exists() bool

Exists 当前界面是否存在命中控件。

Uiacc.Find

func (a *Uiacc) Find() []*UiObject

Find 取树后返回全部命中控件句柄(对标 AutoGo Find)。

Uiacc.FindOnce

func (a *Uiacc) FindOnce() *UiObject

FindOnce 取树后返回第一个命中控件句柄;无则 nil(对标 AutoGo FindOnce)。

Uiacc.Focusable

func (a *Uiacc) Focusable(v bool) *Uiacc

Focusable 限定 focusable 属性为 v。

Uiacc.Focused

func (a *Uiacc) Focused(v bool) *Uiacc

Focused 限定 focused 属性为 v。

Uiacc.Id

func (a *Uiacc) Id(v string) *Uiacc

Id 限定资源 id(如 com.pkg:id/name)精确等于 v。

Uiacc.IdContains

func (a *Uiacc) IdContains(v string) *Uiacc

IdContains 限定资源 id 包含子串 v。

Uiacc.IdEndsWith

func (a *Uiacc) IdEndsWith(v string) *Uiacc

IdEndsWith 限定资源 id 以 v 结尾。

Uiacc.IdMatches

func (a *Uiacc) IdMatches(v string) *Uiacc

IdMatches 限定资源 id 匹配正则 v。

Uiacc.Index

func (a *Uiacc) Index(v int) *Uiacc

Index 限定节点在父容器中的 index 等于 v。

Uiacc.LongClickable

func (a *Uiacc) LongClickable(v bool) *Uiacc

LongClickable 限定 longClickable 属性为 v。

Uiacc.PackageName

func (a *Uiacc) PackageName(v string) *Uiacc

PackageName 限定所属应用包名精确等于 v。

Uiacc.Password

func (a *Uiacc) Password(v bool) *Uiacc

Password 限定 password 属性为 v。

Uiacc.Scrollable

func (a *Uiacc) Scrollable(v bool) *Uiacc

Scrollable 限定 scrollable 属性为 v。

Uiacc.Selected

func (a *Uiacc) Selected(v bool) *Uiacc

Selected 限定 selected 属性为 v。

Uiacc.Text

func (a *Uiacc) Text(v string) *Uiacc

Text 限定 text 精确等于 v。

Uiacc.TextContains

func (a *Uiacc) TextContains(v string) *Uiacc

TextContains 限定 text 包含子串 v。

Uiacc.TextEndsWith

func (a *Uiacc) TextEndsWith(v string) *Uiacc

TextEndsWith 限定 text 以 v 结尾。

Uiacc.TextMatches

func (a *Uiacc) TextMatches(v string) *Uiacc

TextMatches 限定 text 匹配正则 v。

Uiacc.TextStartsWith

func (a *Uiacc) TextStartsWith(v string) *Uiacc

TextStartsWith 限定 text 以 v 开头。

Uiacc.WaitFor

func (a *Uiacc) WaitFor(timeoutMs int) *UiObject

WaitFor 反复取树直到命中或超时(timeoutMs),返回句柄;超时 nil(对标 AutoGo WaitFor)。

上次更新: 2026/9/2 06:21
贡献者: hb
Prev
images - 找图找色 / 截图
Next
device - 设备