Lua 业务脚本 / UI 控件常用写法

7. UI 控件常用写法

7.1 改文本 / 数值 / 开关

ui.SetLabelText

  • 作用: 标签独有:设置/获取文本。控件须为 Label 类型。
  • 签名: ui.SetLabelText(widget_name, text); ui.GetLabelText(widget_name)
ui.SetLabelText("title", "Hello"); local s = ui.GetLabelText("title")

ui.SetSliderValue

  • 作用: 滑块独有:设置/获取数值。SetSliderValue(name, value [, anim]),anim 为 true 时带动画。
  • 签名: ui.SetSliderValue(widget_name, value [, anim]); ui.GetSliderValue(widget_name)
ui.SetSliderValue("vol", 50); local v = ui.GetSliderValue("vol")

ui.GetSwitchState

  • 作用: 开关独有:获取/设置开合状态。true=开(CHECKED),false=关。
  • 签名: ui.GetSwitchState(widget_name); ui.SetSwitchState(widget_name, true|false)
if ui.GetSwitchState("sw") then ... end; ui.SetSwitchState("sw", true)

ui.SetPos

  • 作用: 在当前页下按控件名查找控件并设置其位置(设置控件在页内的位置)
  • 签名: ui.SetPos(widget_name, x, y)
ui.SetPos("myButton", 100, 100)

ui.SetStyle

  • 作用: 按样式枚举设置控件本地样式。不暴露指针:控件用名字、图片用路径字符串(后端 resolve)。value 为数字(尺寸/透明度/0xRRGGBB 颜色)或图片路径。
  • 签名: ui.SetStyle(widget_name, ui.StyleProp.XXX, value [, selector])
ui.SetStyle("btn", ui.StyleProp.BG_COLOR, 0xFF0000); ui.SetStyle("img", ui.StyleProp.BG_IMAGE_SRC, "c_210.bin")

ui.AddFlag

  • 作用: 给控件添加标志(可 OR 组合),如 CLICKABLE、SCROLLABLE。
  • 签名: ui.AddFlag(widget_name, ui.Flag.XXX)
ui.AddFlag("list", ui.Flag.SCROLLABLE)

ui.SetAlign

  • 作用: 设置控件相对父对象的对齐方式,不改变偏移(偏移仍用 SetPos)。
  • 签名: ui.SetAlign(widget_name, ui.Align.XXX)
ui.SetAlign("btn", ui.Align.CENTER)

统一入口(模板;编辑器补全 可能缺失)

调用 作用 参数
ui.SetValue(name, v) 设 Slider/Bar/Arc/Spinbox 等数值 name 控件名;v 数值;动画等 待确认
ui.GetValue(name) 读数值 name
ui.SetChecked(name, b) 设 Switch/Checkbox 等 name;b bool
ui.GetChecked(name) 读开关 name
ui.SetLabelText("title", "Hello")
ui.SetLabelTextFmt("title", "v=%d", 3) -- 若工程启用 Fmt 绑定;参数细节待确认
ui.SetValue("vol", 50)
ui.SetChecked("sw_mute", false)
ui.SetSwitchState("sw_mute", true) -- 开关专用

7.2 跳转页面 / 退出

ui.GotoPage

  • 作用: 按页面名称跳转到指定页面(由设计器导出的页面名),可选指定动画类型、时间、延时和是否删除当前页。
  • 签名: ui.GotoPage(page_name [, anim, time_ms, delay_ms, delete_current])
ui.GotoPage("MainPage", 2, 300, 0, true) -- 2 对应 MOVE_LEFT 等枚举值

ui.ExitPage

  • 作用: 退出当前页,回到进入本页前的页面;会删除当前页。无法退回时返回 false(例如上一页已被删除或无导航历史)。
  • 签名: ui.ExitPage([anim, time_ms, delay_ms])
ui.ExitPage(2, 300, 0) -- 动画枚举与 GotoPage 相同:0 无,1 淡入,2 左移…

ui.GetCurrentPageName

  • 作用: 返回当前显示层对应的页面名(设计器页面名称字符串)。优先取运行时当前层页名;无有效显示层时回退为事件脚本上下文页名;仍不可用时返回空字符串。
  • 签名: ui.GetCurrentPageName()
local page = ui.GetCurrentPageName(); if page == "MainPage" then ... end
ui.GotoPage("MainPage")
ui.GotoPage("MainPage", 2, 300, 0, true) -- anim, time_ms, delay_ms, delete_current
if not ui.ExitPage() then
 hw.print("cannot exit page")
end

动画枚举取值以运行时 / 编辑器补全 示例为准(示例中 0 无、1 淡入、2 左移…)。

7.3 弹窗(模板口径)

RequestDialog / DismissDialog / GotoDialog / CloseDialog / GetActiveDialogName / IsDialogPending / IsDialogVisible / SetResidentVisible。

  • 作用: Top 弹窗调度与常驻层显隐。
  • 参数表: 多数 待确认(编辑器补全 常缺)。
  • 建议: 先在编辑器配弹窗与 事件流程;脚本侧只调已验证过的名字。
  • 概念: 页面层 / 弹窗层 / 常驻层的编辑入口与叠放关系见 上位机 · 三层。

7.4 在回调里读事件

ui.GetEventObjectName

  • 作用: 在事件回调内获取挂接该事件回调的控件名(当前处理节点,字符串)。冒泡到父控件时此处为父控件名。非事件上下文中返回空串。
  • 签名: ui.GetEventObjectName()
local handler = ui.GetEventObjectName()

ui.SendEvent

  • 作用: 对当前页上指定控件派发界面事件;第二参为 ui.Event.* 枚举值(与 ui.GetEventType() 一致)。由运行时派发;无附加参数。控件不存在时写日志且不派发。
  • 签名: ui.SendEvent(widget_name, ui.Event.事件名)
ui.SendEvent("my_btn", ui.Event.CLICKED)
return function(ud)
 local w = ui.GetEventObjectName()
 local t = ui.GetEventTypeName() -- 若运行时提供;编辑器补全 可能缺失 → 待确认
 hw.print("from", w, t)
end

7.5 Textarea / Dropdown / 其它

API 作用 签名来源
SetTextareaText / GetTextareaText 多行文本 模板;详参 待确认 / 见 05 清单
SetTextareaPlaceholderText / PasswordMode / I18nKey 占位与密码 同上
Get/SetDropdownSelected / GetDropdownSelectedStr 下拉选中 同上
Get/SetRollerSelected / GetRollerSelectedStr 滚轮 同上

Chart / Track / Keyboard / DigitalClock / Player / Table / List / QR 等:不在本章展开;查 05 全清单 + 编辑器补全 已有说明,勿臆造参数。

7.6 样式与标志(提醒)

ui.SetStyle

  • 作用: 按样式枚举设置控件本地样式。不暴露指针:控件用名字、图片用路径字符串(后端 resolve)。value 为数字(尺寸/透明度/0xRRGGBB 颜色)或图片路径。
  • 签名: ui.SetStyle(widget_name, ui.StyleProp.XXX, value [, selector])
ui.SetStyle("btn", ui.StyleProp.BG_COLOR, 0xFF0000); ui.SetStyle("img", ui.StyleProp.BG_IMAGE_SRC, "c_210.bin")

SetStyle 的 selector 可组合 ui.Part.* 与 ui.State.*(编辑器补全 说明)。具体枚举成员列表以运行时常量表 / meta 为准;若补全里没有某个枚举名,待确认是否未导出到 meta,不要凭桌面 GUI 库命名臆造。

下一章:硬件接口