dropdown_page.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. --[[
  2. @module dropdown_page
  3. @summary 下拉框组件演示页面
  4. @version 1.0
  5. @date 2026.02.05
  6. @author 江访
  7. @usage
  8. 本文件是下拉框组件的演示页面,展示下拉框的各种用法。
  9. ]]
  10. local dropdown_page = {}
  11. -- 页面UI元素
  12. local main_container = nil
  13. -- 创建UI
  14. function dropdown_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 = 0x795548,
  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. -- 显示选中项
  74. local selected_label1 = airui.label({
  75. parent = scroll_container,
  76. text = "当前选中: 选项1",
  77. x = 230,
  78. y = 45,
  79. w = 80,
  80. h = 50,
  81. font_size = 14,
  82. })
  83. local basic_dropdown = airui.dropdown({
  84. parent = scroll_container,
  85. x = 20,
  86. y = 40,
  87. w = 200,
  88. h = 40,
  89. options = {"选项1", "选项2", "选项3", "选项4", "选项5"},
  90. default_index = 0,
  91. on_change = function(self,idx)
  92. local texts = {"选项1", "选项2", "选项3", "选项4", "选项5"}
  93. selected_label1:set_text("当前选中: " .. texts[idx + 1])
  94. end
  95. })
  96. -- 示例2: 下拉框组
  97. airui.label({
  98. parent = scroll_container,
  99. text = "示例2: 下拉框组",
  100. x = 10,
  101. y = 110,
  102. w = 300,
  103. h = 20,
  104. font_size = 14,
  105. })
  106. local dropdown_container = airui.container({
  107. parent = scroll_container,
  108. x = 20,
  109. y = 140,
  110. w = 280,
  111. h = 150,
  112. color = 0xEFEBE9,
  113. radius = 8,
  114. })
  115. -- 城市选择
  116. airui.label({
  117. parent = dropdown_container,
  118. text = "城市:",
  119. x = 10,
  120. y = 20,
  121. w = 60,
  122. h = 20,
  123. font_size = 14,
  124. })
  125. local city_dropdown = airui.dropdown({
  126. parent = dropdown_container,
  127. x = 80,
  128. y = 15,
  129. w = 180,
  130. h = 30,
  131. options = {"北京", "上海", "广州", "深圳", "杭州", "南京", "成都"},
  132. default_index = 0,
  133. })
  134. -- 区县选择
  135. airui.label({
  136. parent = dropdown_container,
  137. text = "区县:",
  138. x = 10,
  139. y = 60,
  140. w = 60,
  141. h = 20,
  142. font_size = 14,
  143. })
  144. local district_dropdown = airui.dropdown({
  145. parent = dropdown_container,
  146. x = 80,
  147. y = 55,
  148. w = 180,
  149. h = 30,
  150. options = {"请先选择城市"},
  151. default_index = 0,
  152. })
  153. -- 联动选择按钮
  154. local select_btn = airui.button({
  155. parent = dropdown_container,
  156. x = 80,
  157. y = 100,
  158. w = 180,
  159. h = 35,
  160. text = "确认选择",
  161. on_click = function(self)
  162. local city_idx = city_dropdown:get_selected()
  163. local district_idx = district_dropdown:get_selected()
  164. local cities = {"北京", "上海", "广州", "深圳", "杭州", "南京", "成都"}
  165. log.info("dropdown", "选择了城市: " .. cities[city_idx + 1])
  166. end
  167. })
  168. -- 示例3: 获取选中值
  169. airui.label({
  170. parent = scroll_container,
  171. text = "示例3: 获取选中值",
  172. x = 10,
  173. y = 310,
  174. w = 300,
  175. h = 20,
  176. font_size = 14,
  177. })
  178. local value_dropdown = airui.dropdown({
  179. parent = scroll_container,
  180. x = 20,
  181. y = 340,
  182. w = 180,
  183. h = 40,
  184. options = {"红色", "绿色", "蓝色", "黄色", "紫色"},
  185. default_index = 2,
  186. })
  187. local get_btn = airui.button({
  188. parent = scroll_container,
  189. x = 210,
  190. y = 340,
  191. w = 80,
  192. h = 40,
  193. text = "获取",
  194. on_click = function(self)
  195. local idx = value_dropdown:get_selected()
  196. local colors = {"红色", "绿色", "蓝色", "黄色", "紫色"}
  197. local msg = airui.msgbox({
  198. text = "当前选中: " .. colors[idx + 1],
  199. buttons = {"确定"},
  200. timeout = 2000,
  201. on_action = function(self, label)
  202. self:hide()
  203. end
  204. })
  205. msg:show()
  206. end
  207. })
  208. -- 设置选中项
  209. local set_btn = airui.button({
  210. parent = scroll_container,
  211. x = 20,
  212. y = 390,
  213. w = 180,
  214. h = 40,
  215. text = "设置为黄色",
  216. on_click = function(self)
  217. value_dropdown:set_selected(3) -- 黄色是第4项,索引为3
  218. end
  219. })
  220. -- 底部信息
  221. airui.label({
  222. parent = main_container,
  223. text = "提示: 下拉框支持选项选择和联动",
  224. x = 10,
  225. y = 440,
  226. w = 300,
  227. h = 20,
  228. font_size = 14,
  229. })
  230. end
  231. -- 初始化页面
  232. function dropdown_page.init(params)
  233. dropdown_page.create_ui()
  234. end
  235. -- 清理页面
  236. function dropdown_page.cleanup()
  237. if main_container then
  238. main_container:destroy()
  239. main_container = nil
  240. end
  241. end
  242. return dropdown_page