switch_page_demo.lua 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. --[[
  2. @module switch_page_demo
  3. @summary 页面切换演示
  4. @version 1.0
  5. @date 2026.02.05
  6. @author 江访
  7. @usage
  8. 本文件是页面切换的演示,展示多个子页面之间的切换。
  9. ]]
  10. local switch_page_demo = {}
  11. local tab_buttons = {}
  12. -- 页面UI元素
  13. local main_container = nil
  14. local current_subpage = nil
  15. local subpages = {}
  16. local update_timer
  17. local auto_update_timer
  18. -- 创建UI
  19. function switch_page_demo.create_ui()
  20. main_container = airui.container({
  21. x = 0,
  22. y = 0,
  23. w = 320,
  24. h = 480,
  25. color = 0xF5F5F5,
  26. })
  27. -- 标题栏
  28. local title_bar = airui.container({
  29. parent = main_container,
  30. x = 0,
  31. y = 0,
  32. w = 320,
  33. h = 50,
  34. color = 0x673AB7,
  35. })
  36. airui.label({
  37. parent = title_bar,
  38. text = "页面切换演示",
  39. x = 10,
  40. y = 15,
  41. w = 200,
  42. h = 20,
  43. font_size = 16,
  44. color = 0xFFFFFF,
  45. })
  46. -- 返回按钮
  47. local back_btn = airui.button({
  48. parent = title_bar,
  49. x = 250,
  50. y = 10,
  51. w = 60,
  52. h = 30,
  53. text = "返回",
  54. on_click = function(self)
  55. go_back()
  56. end
  57. })
  58. -- 选项卡容器
  59. local tab_container = airui.container({
  60. parent = main_container,
  61. x = 0,
  62. y = 60,
  63. w = 320,
  64. h = 50,
  65. color = 0xEDE7F6,
  66. })
  67. -- 子页面容器
  68. local content_container = airui.container({
  69. parent = main_container,
  70. x = 0,
  71. y = 120,
  72. w = 320,
  73. h = 280,
  74. color = 0xFFFFFF,
  75. })
  76. -- 创建子页面
  77. function switch_page_demo.create_subpages()
  78. -- 子页面1: 欢迎页面
  79. local page1 = airui.container({
  80. parent = content_container,
  81. x = 0,
  82. y = 0,
  83. w = 320,
  84. h = 280,
  85. color = 0xE3F2FD,
  86. })
  87. airui.label({
  88. parent = page1,
  89. text = "欢迎页面",
  90. x = 100,
  91. y = 50,
  92. w = 120,
  93. h = 40,
  94. font_size = 16,
  95. })
  96. airui.label({
  97. parent = page1,
  98. text = "这是第一个子页面",
  99. x = 60,
  100. y = 100,
  101. w = 200,
  102. h = 30,
  103. font_size = 14,
  104. })
  105. airui.label({
  106. parent = page1,
  107. text = "点击下方标签切换页面",
  108. x = 60,
  109. y = 140,
  110. w = 200,
  111. h = 30,
  112. font_size = 14,
  113. })
  114. -- 子页面2: 信息页面
  115. local page2 = airui.container({
  116. parent = content_container,
  117. x = 0,
  118. y = 0,
  119. w = 320,
  120. h = 280,
  121. color = 0xF1F8E9,
  122. })
  123. airui.label({
  124. parent = page2,
  125. text = "信息页面",
  126. x = 100,
  127. y = 50,
  128. w = 120,
  129. h = 40,
  130. font_size = 16,
  131. })
  132. airui.label({
  133. parent = page2,
  134. text = "系统信息:",
  135. x = 30,
  136. y = 100,
  137. w = 120,
  138. h = 30,
  139. font_size = 14,
  140. })
  141. airui.label({
  142. parent = page2,
  143. text = "当前时间: " .. os.date("%H:%M:%S"),
  144. x = 30,
  145. y = 140,
  146. w = 260,
  147. h = 20,
  148. font_size = 14,
  149. })
  150. airui.label({
  151. parent = page2,
  152. text = "页面演示系统",
  153. x = 30,
  154. y = 170,
  155. w = 260,
  156. h = 20,
  157. font_size = 14,
  158. })
  159. -- 子页面3: 设置页面
  160. local page3 = airui.container({
  161. parent = content_container,
  162. x = 0,
  163. y = 0,
  164. w = 320,
  165. h = 280,
  166. color = 0xFFF3E0,
  167. })
  168. airui.label({
  169. parent = page3,
  170. text = "设置页面",
  171. x = 100,
  172. y = 50,
  173. w = 120,
  174. h = 40,
  175. font_size = 16,
  176. })
  177. local setting1 = airui.switch({
  178. parent = page3,
  179. x = 30,
  180. y = 100,
  181. w = 60,
  182. h = 30,
  183. checked = true,
  184. on_change = function(self)
  185. log.info("switch_page", "设置1: " .. tostring(self:get_state()))
  186. end
  187. })
  188. airui.label({
  189. parent = page3,
  190. text = "启用通知",
  191. x = 100,
  192. y = 105,
  193. w = 100,
  194. h = 20,
  195. font_size = 14,
  196. })
  197. local setting2 = airui.switch({
  198. parent = page3,
  199. x = 30,
  200. y = 150,
  201. w = 60,
  202. h = 30,
  203. checked = false,
  204. on_change = function(self)
  205. log.info("switch_page", "设置2: " .. tostring(self:get_state()))
  206. end
  207. })
  208. airui.label({
  209. parent = page3,
  210. text = "自动更新",
  211. x = 100,
  212. y = 155,
  213. w = 100,
  214. h = 20,
  215. font_size = 14,
  216. })
  217. subpages = {
  218. { container = page1, name = "欢迎" },
  219. { container = page2, name = "信息" },
  220. { container = page3, name = "设置" }
  221. }
  222. current_subpage = 1
  223. for i, page in ipairs(subpages) do
  224. if i == 1 then
  225. page.container:open()
  226. else
  227. page.container:hide()
  228. end
  229. end
  230. end
  231. -- 创建选项卡按钮
  232. function switch_page_demo.create_tabs()
  233. local tab_width = 90
  234. local tab_height = 40
  235. local tab_margin = 10
  236. for i, page in ipairs(subpages) do
  237. local tab_x = tab_margin + (i - 1) * (tab_width + tab_margin)
  238. local tab = airui.button({
  239. parent = tab_container,
  240. x = tab_x,
  241. y = 5,
  242. w = tab_width,
  243. h = tab_height,
  244. text = page.name,
  245. on_click = function(self)
  246. switch_page_demo.switch_to_page(i)
  247. end
  248. })
  249. tab_buttons[i] = tab
  250. if i == current_subpage then
  251. tab:set_text("- " .. page.name .. " -")
  252. end
  253. end
  254. end
  255. -- 切换页面函数
  256. function switch_page_demo.switch_to_page(page_index)
  257. if page_index == current_subpage then
  258. return
  259. end
  260. if current_subpage and subpages[current_subpage] then
  261. subpages[current_subpage].container:hide()
  262. end
  263. if subpages[page_index] then
  264. subpages[page_index].container:open()
  265. current_subpage = page_index
  266. for i, btn in ipairs(tab_buttons) do
  267. if i == page_index then
  268. btn:set_text("- " .. subpages[i].name .. " -")
  269. else
  270. btn:set_text(subpages[i].name)
  271. end
  272. end
  273. log.info("switch_page", "切换到页面: " .. subpages[page_index].name)
  274. end
  275. end
  276. switch_page_demo.create_subpages()
  277. switch_page_demo.create_tabs()
  278. -- 页面指示器
  279. local indicator_label = airui.label({
  280. parent = main_container,
  281. text = "当前页面: 欢迎",
  282. x = 10,
  283. y = 410,
  284. w = 200,
  285. h = 20,
  286. font_size = 14,
  287. })
  288. update_timer = sys.timerLoopStart(function()
  289. if current_subpage and subpages[current_subpage] then
  290. indicator_label:set_text("当前页面: " .. subpages[current_subpage].name)
  291. end
  292. end, 100)
  293. -- 手动切换按钮
  294. local prev_btn = airui.button({
  295. parent = main_container,
  296. x = 20,
  297. y = 445,
  298. w = 80,
  299. h = 35,
  300. text = "上一页",
  301. on_click = function(self)
  302. local new_index = current_subpage - 1
  303. if new_index < 1 then
  304. new_index = #subpages
  305. end
  306. switch_page_demo.switch_to_page(new_index)
  307. end
  308. })
  309. local next_btn = airui.button({
  310. parent = main_container,
  311. x = 120,
  312. y = 445,
  313. w = 80,
  314. h = 35,
  315. text = "下一页",
  316. on_click = function(self)
  317. local new_index = current_subpage + 1
  318. if new_index > #subpages then
  319. new_index = 1
  320. end
  321. switch_page_demo.switch_to_page(new_index)
  322. end
  323. })
  324. -- 自动切换开关
  325. local auto_switch = airui.switch({
  326. parent = main_container,
  327. x = 220,
  328. y = 445,
  329. w = 50,
  330. h = 30,
  331. checked = false,
  332. on_change = function(self)
  333. if self:get_state() then
  334. log.info("switch_page", "启用自动切换")
  335. else
  336. log.info("switch_page", "禁用自动切换")
  337. end
  338. end
  339. })
  340. airui.label({
  341. parent = main_container,
  342. text = "自动",
  343. x = 275,
  344. y = 450,
  345. w = 40,
  346. h = 20,
  347. font_size = 14,
  348. })
  349. auto_update_timer = sys.timerLoopStart(function()
  350. if auto_switch:get_state() then
  351. local new_index = current_subpage + 1
  352. if new_index > #subpages then
  353. new_index = 1
  354. end
  355. switch_page_demo.switch_to_page(new_index)
  356. end
  357. end, 5000) -- 5秒自动切换
  358. end
  359. -- 初始化页面
  360. function switch_page_demo.init(params)
  361. current_subpage = 1
  362. subpages = {}
  363. switch_page_demo.create_ui()
  364. end
  365. -- 清理页面
  366. function switch_page_demo.cleanup()
  367. sys.timerStop(update_timer)
  368. sys.timerStop(auto_update_timer)
  369. if main_container then
  370. main_container:destroy()
  371. main_container = nil
  372. end
  373. current_subpage = nil
  374. subpages = {}
  375. tab_buttons = {}
  376. end
  377. return switch_page_demo