all_component_page.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. --[[
  2. @module all_component_page
  3. @summary 所有组件演示页面
  4. @version 1.0
  5. @date 2026.02.05
  6. @author 江访
  7. @usage
  8. 本文件是所有组件的综合演示页面,展示AirUI所有组件的用法。
  9. ]]
  10. local all_component_page = {}
  11. -- 页面UI元素
  12. local main_container = nil
  13. -- 创建UI
  14. function all_component_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. color_opacity = 255, -- v1.0.3 新增
  23. })
  24. -- 标题栏
  25. local title_bar = airui.container({
  26. parent = main_container,
  27. x = 0,
  28. y = 0,
  29. w = 320,
  30. h = 50,
  31. color = 0x007AFF,
  32. })
  33. airui.label({
  34. parent = title_bar,
  35. text = "所有组件演示",
  36. x = 10,
  37. y = 15,
  38. w = 200,
  39. h = 20,
  40. font_size = 16, -- v1.0.1 改为 font_size
  41. color = 0xFFFFFF,
  42. })
  43. -- 返回按钮
  44. local back_btn = airui.button({
  45. parent = title_bar,
  46. x = 250,
  47. y = 10,
  48. w = 60,
  49. h = 30,
  50. text = "返回",
  51. on_click = function(self) -- v1.0.3 支持 self 参数
  52. go_back()
  53. end
  54. })
  55. -- 注册虚拟键盘,先创建再在 textarea 配置里复用
  56. local keyboard1 = airui.keyboard({
  57. x = 0,
  58. y = 0,
  59. w = 320,
  60. h = 220, -- x, y, 键盘默认打开ALIGN_BOTTOM_MID,位置从中下方开始计算
  61. mode = "text", -- 键盘模式,可选 "text"/"upper"/"lower"/"numeric"
  62. auto_hide = true, -- 自动隐藏键盘
  63. bg_color = 0xf1f1f1, -- 键盘背景颜色为灰色,可选,不设置则透明
  64. on_commit = function() -- 确认事件回调,只有在按下确认键时才会触发
  65. log.info("keyboard", "commit")
  66. end
  67. })
  68. -- 滚动容器
  69. local scroll_container = airui.container({
  70. parent = main_container,
  71. x = 0,
  72. y = 60,
  73. w = 320,
  74. h = 370,
  75. color = 0xF5F5F5,
  76. })
  77. -- 组件展示区域
  78. local y_offset = 10
  79. -- 1. 标签组件
  80. airui.label({
  81. parent = scroll_container,
  82. text = "1. 标签组件 (Label)",
  83. x = 10,
  84. y = y_offset,
  85. w = 300,
  86. h = 20,
  87. font_size = 14,
  88. })
  89. local demo_label = airui.label({
  90. parent = scroll_container,
  91. text = "这是一个标签",
  92. x = 20,
  93. y = y_offset + 30,
  94. w = 120,
  95. h = 30,
  96. font_size = 14,
  97. on_click = function(self)
  98. self:set_text("标签被点击") -- v1.0.3 使用 self
  99. end
  100. })
  101. y_offset = y_offset + 70
  102. -- 2. 按钮组件
  103. airui.label({
  104. parent = scroll_container,
  105. text = "2. 按钮组件 (Button)",
  106. x = 10,
  107. y = y_offset,
  108. w = 300,
  109. h = 20,
  110. font_size = 14,
  111. })
  112. local demo_button = airui.button({
  113. parent = scroll_container,
  114. x = 20,
  115. y = y_offset + 30,
  116. w = 100,
  117. h = 40,
  118. text = "点击我",
  119. on_click = function(self)
  120. log.info("all_component", "按钮被点击")
  121. end
  122. })
  123. y_offset = y_offset + 80
  124. -- 3. 容器组件
  125. airui.label({
  126. parent = scroll_container,
  127. text = "3. 容器组件 (Container)",
  128. x = 10,
  129. y = y_offset,
  130. w = 300,
  131. h = 20,
  132. font_size = 14,
  133. })
  134. local demo_container = airui.container({
  135. parent = scroll_container,
  136. x = 20,
  137. y = y_offset + 30,
  138. w = 280,
  139. h = 60,
  140. color = 0xE3F2FD,
  141. radius = 8,
  142. })
  143. airui.label({
  144. parent = demo_container,
  145. text = "容器内的标签",
  146. x = 10,
  147. y = 20,
  148. w = 120,
  149. h = 20,
  150. font_size = 14,
  151. })
  152. y_offset = y_offset + 100
  153. -- 4. 进度条组件
  154. airui.label({
  155. parent = scroll_container,
  156. text = "4. 进度条组件 (Progress Bar)",
  157. x = 10,
  158. y = y_offset,
  159. w = 300,
  160. h = 20,
  161. font_size = 14,
  162. })
  163. local demo_bar = airui.bar({
  164. parent = scroll_container,
  165. x = 20,
  166. y = y_offset + 30,
  167. w = 200,
  168. h = 20,
  169. value = 65,
  170. indicator_color = 0x4CAF50,
  171. show_progress_text = true, -- v1.0.3 新增
  172. progress_text_format = "%d%%", -- v1.0.3 新增
  173. })
  174. y_offset = y_offset + 60
  175. -- 5. 开关组件
  176. airui.label({
  177. parent = scroll_container,
  178. text = "5. 开关组件 (Switch)",
  179. x = 10,
  180. y = y_offset,
  181. w = 300,
  182. h = 20,
  183. font_size = 14,
  184. })
  185. local demo_switch = airui.switch({
  186. parent = scroll_container,
  187. x = 20,
  188. y = y_offset + 30,
  189. w = 60,
  190. h = 30,
  191. checked = true,
  192. on_change = function(self) -- v1.0.3 回调参数为 self
  193. log.info("all_component", "开关状态: " .. tostring(self:get_state()))
  194. end
  195. })
  196. y_offset = y_offset + 70
  197. -- 6. 下拉框组件
  198. airui.label({
  199. parent = scroll_container,
  200. text = "6. 下拉框组件 (Dropdown)",
  201. x = 10,
  202. y = y_offset,
  203. w = 300,
  204. h = 20,
  205. font_size = 14,
  206. })
  207. local demo_dropdown = airui.dropdown({
  208. parent = scroll_container,
  209. x = 20,
  210. y = y_offset + 30,
  211. w = 150,
  212. h = 35,
  213. options = { "选项1", "选项2", "选项3" },
  214. default_index = 0,
  215. on_change = function(idx) -- 回调参数为索引(0起始)
  216. log.info("all_component", "选择了选项: " .. (idx + 1))
  217. end
  218. })
  219. y_offset = y_offset + 75
  220. -- 7. 表格组件
  221. airui.label({
  222. parent = scroll_container,
  223. text = "7. 表格组件 (Table)",
  224. x = 10,
  225. y = y_offset,
  226. w = 300,
  227. h = 20,
  228. font_size = 14,
  229. })
  230. local demo_table = airui.table({
  231. parent = scroll_container,
  232. x = 20,
  233. y = y_offset + 30,
  234. w = 280,
  235. h = 80,
  236. rows = 3,
  237. cols = 3,
  238. col_width = { 80, 80, 80 },
  239. border_color = 0x607D8B,
  240. })
  241. demo_table:set_cell_text(0, 0, "姓名")
  242. demo_table:set_cell_text(0, 1, "年龄")
  243. demo_table:set_cell_text(0, 2, "城市")
  244. demo_table:set_cell_text(1, 0, "张三")
  245. demo_table:set_cell_text(1, 1, "25")
  246. demo_table:set_cell_text(1, 2, "北京")
  247. y_offset = y_offset + 120
  248. -- 8. 输入框组件
  249. airui.label({
  250. parent = scroll_container,
  251. text = "8. 输入框组件 (Textarea)",
  252. x = 10,
  253. y = y_offset,
  254. w = 300,
  255. h = 20,
  256. font_size = 14,
  257. })
  258. local demo_textarea = airui.textarea({
  259. parent = scroll_container,
  260. x = 20,
  261. y = y_offset + 30,
  262. w = 150,
  263. h = 60,
  264. placeholder = "请输入文本...",
  265. max_len = 50,
  266. keyboard = keyboard1
  267. })
  268. y_offset = y_offset + 100
  269. -- 9. 消息框组件(按钮演示)
  270. airui.label({
  271. parent = scroll_container,
  272. text = "9. 消息框组件 (Msgbox)",
  273. x = 10,
  274. y = y_offset,
  275. w = 300,
  276. h = 20,
  277. font_size = 14,
  278. })
  279. local demo_msgbox_btn = airui.button({
  280. parent = scroll_container,
  281. x = 20,
  282. y = y_offset + 30,
  283. w = 120,
  284. h = 40,
  285. text = "显示消息",
  286. on_click = function(self)
  287. local msg = airui.msgbox({
  288. text = "这是一个消息框",
  289. buttons = { "确定" },
  290. on_action = function(self, label)
  291. if label == "确定" then
  292. self:hide()
  293. end
  294. end
  295. })
  296. msg:show()
  297. end
  298. })
  299. y_offset = y_offset + 80
  300. -- 10. 图片组件(需要图片文件)
  301. airui.label({
  302. parent = scroll_container,
  303. text = "10. 图片组件 (Image)",
  304. x = 10,
  305. y = y_offset,
  306. w = 300,
  307. h = 20,
  308. font_size = 14,
  309. })
  310. -- 假设有图片文件
  311. local demo_image = airui.image({
  312. parent = scroll_container,
  313. x = 20,
  314. y = y_offset + 30,
  315. w = 80,
  316. h = 80,
  317. src = "/luadb/logo.jpg",
  318. on_click = function(self)
  319. log.info("all_component", "图片被点击")
  320. end
  321. })
  322. y_offset = y_offset + 120
  323. -- 11. 选项卡组件
  324. airui.label({
  325. parent = scroll_container,
  326. text = "11. 选项卡组件 (Tabview)",
  327. x = 10,
  328. y = y_offset,
  329. w = 300,
  330. h = 20,
  331. font_size = 14,
  332. })
  333. local demo_tabview = airui.tabview({
  334. parent = scroll_container,
  335. x = 20,
  336. y = y_offset + 30,
  337. w = 280,
  338. h = 150,
  339. tabs = { "标签1", "标签2" },
  340. active = 0,
  341. })
  342. -- 获取页面内容并添加标签
  343. local tab1_content = demo_tabview:get_content(0)
  344. if tab1_content then
  345. airui.label({
  346. parent = tab1_content,
  347. text = "标签1内容",
  348. x = 20,
  349. y = 20,
  350. w = 240,
  351. h = 30,
  352. font_size = 14,
  353. })
  354. end
  355. y_offset = y_offset + 190
  356. -- 12. 窗口组件
  357. airui.label({
  358. parent = scroll_container,
  359. text = "12. 窗口组件 (Window)",
  360. x = 10,
  361. y = y_offset,
  362. w = 300,
  363. h = 20,
  364. font_size = 14,
  365. })
  366. local demo_window_btn = airui.button({
  367. parent = scroll_container,
  368. x = 20,
  369. y = y_offset + 30,
  370. w = 120,
  371. h = 40,
  372. text = "打开窗口",
  373. on_click = function(self)
  374. local win = airui.win({
  375. parent = airui.screen,
  376. title = "演示窗口",
  377. x = 60,
  378. y = 120,
  379. w = 200,
  380. h = 250,
  381. close_btn = true,
  382. auto_center = false,
  383. -- on_close 在 v1.0.3 无效,v1.0.4 修复
  384. })
  385. win:add_content(airui.label({
  386. text = "窗口内容演示",
  387. x = 20,
  388. y = 20,
  389. w = 160,
  390. h = 30,
  391. font_size = 14,
  392. }))
  393. win:add_content(airui.button({
  394. text = "关闭",
  395. x = 60,
  396. y = 70,
  397. w = 80,
  398. h = 40,
  399. on_click = function(self)
  400. win:close()
  401. end
  402. }))
  403. end
  404. })
  405. y_offset = y_offset + 80
  406. -- 组件总数显示
  407. airui.label({
  408. parent = scroll_container,
  409. text = "共展示12个AirUI组件",
  410. x = 10,
  411. y = y_offset + 20,
  412. w = 300,
  413. h = 20,
  414. font_size = 14,
  415. })
  416. -- 交互提示
  417. local interact_label = airui.label({
  418. parent = scroll_container,
  419. text = "提示: 点击各个组件查看交互效果",
  420. x = 10,
  421. y = y_offset + 50,
  422. w = 300,
  423. h = 20,
  424. font_size = 14,
  425. })
  426. -- 组件计数
  427. local component_count = 12
  428. local count_label = airui.label({
  429. parent = scroll_container,
  430. text = "组件总数: " .. component_count,
  431. x = 10,
  432. y = y_offset + 80,
  433. w = 300,
  434. h = 20,
  435. font_size = 14,
  436. })
  437. -- 底部信息
  438. airui.label({
  439. parent = main_container,
  440. text = "AirUI组件综合演示",
  441. x = 10,
  442. y = 440,
  443. w = 300,
  444. h = 20,
  445. font_size = 14,
  446. })
  447. end
  448. -- 初始化页面
  449. function all_component_page.init(params)
  450. all_component_page.create_ui()
  451. end
  452. -- 清理页面
  453. function all_component_page.cleanup()
  454. if main_container then
  455. main_container:destroy()
  456. main_container = nil
  457. end
  458. end
  459. return all_component_page