win_number_input.lua 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. --[[
  2. @module win_number_input
  3. @summary 数字输入框演示模块
  4. @version 1.0.0
  5. @date 2025.12.9
  6. @author 江访
  7. @usage
  8. 本文件为数字输入框演示模块,核心业务逻辑为:
  9. 1、创建窗口容器并设置白色背景;
  10. 2、添加数字输入框组件;
  11. 3、添加增减按钮控制数字输入;
  12. 4、实现数字范围限制功能;
  13. 5、启动UI渲染循环持续刷新显示;
  14. 本文件没有对外接口;
  15. ]]
  16. local function ui_main()
  17. -- 显示触摸初始化
  18. hw_font_drv.init()
  19. -- 设置主题
  20. ui.sw_init({ theme = "light" })
  21. -- 创建窗口容器
  22. local page1 = ui.window({ background_color = ui.COLOR_WHITE })
  23. -- 计算居中位置
  24. local page_w, page_h = lcd.getSize()
  25. local container_width = 200
  26. local container_x = (page_w - container_width) / 2
  27. -- 创建数字输入框组件
  28. local number_input = ui.input({
  29. x = container_x, y = 100,
  30. w = 120, h = 40,
  31. placeholder = "0-100",
  32. input_type = "number",
  33. max_length = 3
  34. })
  35. -- 创建减少按钮
  36. local btn_number_minus = ui.button({
  37. x = container_x + 130, y = 100,
  38. w = 40, h = 40,
  39. text = "-",
  40. on_click = function()
  41. local value = tonumber(number_input:get_text()) or 0
  42. if value > 0 then
  43. number_input:set_text(tostring(value - 1))
  44. end
  45. end
  46. })
  47. -- 创建增加按钮
  48. local btn_number_plus = ui.button({
  49. x = container_x + 180, y = 100,
  50. w = 40, h = 40,
  51. text = "+",
  52. on_click = function()
  53. local value = tonumber(number_input:get_text()) or 0
  54. if value < 100 then
  55. number_input:set_text(tostring(value + 1))
  56. end
  57. end
  58. })
  59. -- 添加说明标签
  60. local title_label = ui.label({
  61. x = container_x, y = 50,
  62. text = "数字输入框演示",
  63. color = ui.COLOR_BLACK,
  64. size = 20
  65. })
  66. -- 添加组件到窗口
  67. page1:add(title_label)
  68. page1:add(number_input)
  69. page1:add(btn_number_minus)
  70. page1:add(btn_number_plus)
  71. -- 注册窗口到UI系统
  72. ui.add(page1)
  73. end
  74. sys.taskInit(ui_main)