button_page.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. --[[
  2. @module button_page
  3. @summary 按钮组件演示页面
  4. @version 1.0
  5. @date 2026.02.05
  6. @author 江访
  7. @usage
  8. 本文件是按钮组件的演示页面,展示按钮的各种用法。
  9. ]]
  10. local button_page = {}
  11. -- 页面UI元素
  12. local main_container = nil
  13. -- 创建UI
  14. function button_page.create_ui()
  15. -- 创建主容器
  16. main_container = airui.container({
  17. x = 0,
  18. y = 0,
  19. w = 320,
  20. h = 480,
  21. color = 0xF5F5F5,
  22. })
  23. -- 标题栏
  24. local title_bar = airui.container({
  25. parent = main_container,
  26. x = 0,
  27. y = 0,
  28. w = 320,
  29. h = 50,
  30. color = 0xF44336,
  31. })
  32. airui.label({
  33. parent = title_bar,
  34. text = "按钮组件演示",
  35. x = 10,
  36. y = 15,
  37. w = 200,
  38. h = 20,
  39. font_size = 16,
  40. color = 0xFFFFFF,
  41. })
  42. -- 返回按钮
  43. local back_btn = airui.button({
  44. parent = title_bar,
  45. x = 250,
  46. y = 10,
  47. w = 60,
  48. h = 30,
  49. text = "返回",
  50. on_click = function(self)
  51. go_back()
  52. end
  53. })
  54. -- 滚动容器
  55. local scroll_container = airui.container({
  56. parent = main_container,
  57. x = 0,
  58. y = 60,
  59. w = 320,
  60. h = 370,
  61. color = 0xF5F5F5,
  62. })
  63. -- 示例1: 基本按钮
  64. airui.label({
  65. parent = scroll_container,
  66. text = "示例1: 基本按钮",
  67. x = 10,
  68. y = 10,
  69. w = 300,
  70. h = 20,
  71. font_size = 14,
  72. })
  73. local basic_btn = airui.button({
  74. parent = scroll_container,
  75. x = 20,
  76. y = 40,
  77. w = 120,
  78. h = 40,
  79. text = "点击我",
  80. on_click = function(self)
  81. log.info("button", "基本按钮被点击")
  82. end
  83. })
  84. -- 示例2: 不同大小的按钮
  85. airui.label({
  86. parent = scroll_container,
  87. text = "示例2: 不同大小的按钮",
  88. x = 10,
  89. y = 100,
  90. w = 300,
  91. h = 20,
  92. font_size = 14,
  93. })
  94. local small_btn = airui.button({
  95. parent = scroll_container,
  96. x = 20,
  97. y = 130,
  98. w = 80,
  99. h = 30,
  100. text = "小按钮",
  101. on_click = function(self)
  102. log.info("button", "小按钮被点击")
  103. end
  104. })
  105. local large_btn = airui.button({
  106. parent = scroll_container,
  107. x = 120,
  108. y = 125,
  109. w = 180,
  110. h = 40,
  111. text = "大按钮",
  112. on_click = function(self)
  113. log.info("button", "大按钮被点击")
  114. end
  115. })
  116. -- 示例3: 动态更新文本
  117. local dynamic_label = airui.label({
  118. parent = scroll_container,
  119. text = "示例3: 动态更新文本",
  120. x = 10,
  121. y = 190,
  122. w = 300,
  123. h = 20,
  124. font_size = 14,
  125. })
  126. local click_count = 0
  127. local dynamic_btn = airui.button({
  128. parent = scroll_container,
  129. x = 20,
  130. y = 220,
  131. w = 140,
  132. h = 40,
  133. text = "点击计数: 0",
  134. on_click = function(self)
  135. click_count = click_count + 1
  136. self:set_text("点击计数: " .. click_count) -- v1.0.3 使用 self
  137. dynamic_label:set_text("示例3: 动态更新文本")
  138. end
  139. })
  140. -- 示例4: 按钮组
  141. airui.label({
  142. parent = scroll_container,
  143. text = "示例4: 按钮组",
  144. x = 10,
  145. y = 280,
  146. w = 300,
  147. h = 20,
  148. font_size = 14,
  149. })
  150. local btn_container = airui.container({
  151. parent = scroll_container,
  152. x = 20,
  153. y = 310,
  154. w = 280,
  155. h = 60,
  156. color = 0xE0E0E0,
  157. radius = 5,
  158. })
  159. local btn1 = airui.button({
  160. parent = btn_container,
  161. x = 10,
  162. y = 10,
  163. w = 80,
  164. h = 40,
  165. text = "选项1",
  166. on_click = function(self)
  167. log.info("button", "选项1被选中")
  168. end
  169. })
  170. local btn2 = airui.button({
  171. parent = btn_container,
  172. x = 100,
  173. y = 10,
  174. w = 80,
  175. h = 40,
  176. text = "选项2",
  177. on_click = function(self)
  178. log.info("button", "选项2被选中")
  179. end
  180. })
  181. local btn3 = airui.button({
  182. parent = btn_container,
  183. x = 190,
  184. y = 10,
  185. w = 80,
  186. h = 40,
  187. text = "选项3",
  188. on_click = function(self)
  189. log.info("button", "选项3被选中")
  190. end
  191. })
  192. -- 示例5: 销毁按钮
  193. airui.label({
  194. parent = scroll_container,
  195. text = "示例5: 销毁按钮",
  196. x = 10,
  197. y = 390,
  198. w = 300,
  199. h = 20,
  200. font_size = 14,
  201. })
  202. local toggle_btn = airui.button({
  203. parent = scroll_container,
  204. x = 20,
  205. y = 420,
  206. w = 120,
  207. h = 40,
  208. text = "可点击",
  209. on_click = function(self)
  210. log.info("button", "按钮被点击")
  211. end
  212. })
  213. local disable_btn = airui.button({
  214. parent = scroll_container,
  215. x = 160,
  216. y = 420,
  217. w = 120,
  218. h = 40,
  219. text = "销毁可点击",
  220. on_click = function(self)
  221. toggle_btn:destroy()
  222. log.info("button", "已销毁可点击按钮")
  223. end
  224. })
  225. -- 底部信息
  226. airui.label({
  227. parent = main_container,
  228. text = "提示: 按钮支持点击事件和动态更新",
  229. x = 10,
  230. y = 440,
  231. w = 300,
  232. h = 20,
  233. font_size = 14,
  234. })
  235. end
  236. -- 初始化页面
  237. function button_page.init(params)
  238. button_page.create_ui()
  239. end
  240. -- 清理页面
  241. function button_page.cleanup()
  242. if main_container then
  243. main_container:destroy()
  244. main_container = nil
  245. end
  246. end
  247. return button_page