input_page.lua 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. --[[
  2. @module input_page
  3. @summary 输入框组件演示页面
  4. @version 1.0
  5. @date 2026.02.05
  6. @author 江访
  7. @usage
  8. 本文件是输入框组件的演示页面,展示输入框的各种用法。
  9. ]]
  10. local input_page = {}
  11. -- 页面UI元素
  12. local main_container = nil
  13. -- 创建UI
  14. function input_page.create_ui()
  15. main_container = airui.container({
  16. x = 0,
  17. y = 0,
  18. w = 320,
  19. h = 480,
  20. color = 0xF5F5F5,
  21. })
  22. -- 标题栏
  23. local title_bar = airui.container({
  24. parent = main_container,
  25. x = 0,
  26. y = 0,
  27. w = 320,
  28. h = 50,
  29. color = 0x3F51B5,
  30. })
  31. -- 注册虚拟键盘,先创建再在 textarea 配置里复用
  32. local keyboard1 = airui.keyboard({
  33. x = 0,
  34. y = 0,
  35. w = 320,
  36. h = 220, -- x, y, 键盘默认打开ALIGN_BOTTOM_MID,位置从中下方开始计算
  37. mode = "text", -- 键盘模式,可选 "text"/"upper"/"lower"/"numeric"
  38. auto_hide = true, -- 自动隐藏键盘
  39. bg_color = 0xf1f1f1, -- 键盘背景颜色为灰色,可选,不设置则透明
  40. on_commit = function() -- 确认事件回调,只有在按下确认键时才会触发
  41. log.info("keyboard", "commit")
  42. end
  43. })
  44. local keyboard2 = airui.keyboard({
  45. x = 0,
  46. y = 0,
  47. w = 320,
  48. h = 220, -- x, y, 键盘默认打开ALIGN_BOTTOM_MID,位置从中下方开始计算
  49. mode = "numeric", -- 键盘模式,可选 "text"/"upper"/"lower"/"numeric"
  50. auto_hide = true, -- 自动隐藏键盘
  51. bg_color = 0xf1f1f1, -- 键盘背景颜色为灰色,可选,不设置则透明
  52. on_commit = function() -- 确认事件回调,只有在按下确认键时才会触发
  53. log.info("keyboard", "commit")
  54. end
  55. })
  56. local keyboard3 = airui.keyboard({
  57. x = 0,
  58. y = 0,
  59. w = 320,
  60. h = 220, -- x, y, 键盘默认打开ALIGN_BOTTOM_MID,位置从中下方开始计算
  61. mode = "lower", -- 键盘模式,可选 "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. airui.label({
  69. parent = title_bar,
  70. text = "输入框组件演示",
  71. x = 10,
  72. y = 15,
  73. w = 200,
  74. h = 20,
  75. font_size = 16,
  76. color = 0xFFFFFF,
  77. })
  78. -- 返回按钮
  79. local back_btn = airui.button({
  80. parent = title_bar,
  81. x = 250,
  82. y = 10,
  83. w = 60,
  84. h = 30,
  85. text = "返回",
  86. on_click = function(self)
  87. go_back()
  88. end
  89. })
  90. -- 滚动容器
  91. local scroll_container = airui.container({
  92. parent = main_container,
  93. x = 0,
  94. y = 60,
  95. w = 320,
  96. h = 370,
  97. color = 0xF5F5F5,
  98. })
  99. -- 示例1: 基本输入框
  100. airui.label({
  101. parent = scroll_container,
  102. text = "示例1: 基本输入框",
  103. x = 10,
  104. y = 10,
  105. w = 300,
  106. h = 20,
  107. font_size = 14,
  108. })
  109. -- 创建键盘并关联输入框(v1.0.3 推荐内嵌键盘配置)
  110. local basic_input = airui.textarea({
  111. parent = scroll_container,
  112. x = 20,
  113. y = 40,
  114. w = 280,
  115. h = 100,
  116. text = "",
  117. placeholder = "请输入文本...",
  118. max_len = 100,
  119. keyboard = keyboard1
  120. })
  121. -- 示例2: 带默认值的输入框
  122. airui.label({
  123. parent = scroll_container,
  124. text = "示例2: 带默认值",
  125. x = 10,
  126. y = 160,
  127. w = 300,
  128. h = 20,
  129. font_size = 14,
  130. })
  131. local default_input = airui.textarea({
  132. parent = scroll_container,
  133. x = 20,
  134. y = 190,
  135. w = 280,
  136. h = 60,
  137. text = "默认文本",
  138. placeholder = "请输入...",
  139. max_len = 50,
  140. keyboard = keyboard1
  141. })
  142. -- 示例3: 数字输入框
  143. airui.label({
  144. parent = scroll_container,
  145. text = "示例3: 数字输入框",
  146. x = 10,
  147. y = 270,
  148. w = 300,
  149. h = 20,
  150. font_size = 14,
  151. })
  152. local number_input = airui.textarea({
  153. parent = scroll_container,
  154. x = 20,
  155. y = 300,
  156. w = 280,
  157. h = 60,
  158. text = "",
  159. placeholder = "请输入数字...",
  160. max_len = 20,
  161. keyboard = keyboard2
  162. })
  163. airui.label({
  164. parent = scroll_container,
  165. text = "(使用数字键盘)",
  166. x = 20,
  167. y = 365,
  168. w = 280,
  169. h = 20,
  170. font_size = 12,
  171. color = 0x666666,
  172. })
  173. -- 示例4: 表单输入
  174. airui.label({
  175. parent = scroll_container,
  176. text = "示例4: 表单输入",
  177. x = 10,
  178. y = 400,
  179. w = 300,
  180. h = 20,
  181. font_size = 14,
  182. })
  183. local form_container = airui.container({
  184. parent = scroll_container,
  185. x = 20,
  186. y = 430,
  187. w = 280,
  188. h = 150,
  189. color = 0xE8EAF6,
  190. radius = 8,
  191. })
  192. -- 姓名输入
  193. airui.label({
  194. parent = form_container,
  195. text = "姓名:",
  196. x = 10,
  197. y = 20,
  198. w = 60,
  199. h = 20,
  200. font_size = 14,
  201. })
  202. local name_input = airui.textarea({
  203. parent = form_container,
  204. x = 80,
  205. y = 15,
  206. w = 180,
  207. h = 30,
  208. text = "",
  209. placeholder = "请输入姓名",
  210. max_len = 20,
  211. keyboard = keyboard1
  212. })
  213. -- 邮箱输入
  214. airui.label({
  215. parent = form_container,
  216. text = "邮箱:",
  217. x = 10,
  218. y = 60,
  219. w = 60,
  220. h = 20,
  221. font_size = 14,
  222. })
  223. local email_input = airui.textarea({
  224. parent = form_container,
  225. x = 80,
  226. y = 55,
  227. w = 180,
  228. h = 30,
  229. text = "",
  230. placeholder = "请输入邮箱",
  231. max_len = 50,
  232. keyboard = keyboard1
  233. })
  234. -- 提交按钮
  235. local submit_btn = airui.button({
  236. parent = form_container,
  237. x = 80,
  238. y = 100,
  239. w = 120,
  240. h = 35,
  241. text = "提交",
  242. on_click = function(self)
  243. local name = name_input:get_text()
  244. local email = email_input:get_text()
  245. if name == "" or email == "" then
  246. local msg = airui.msgbox({
  247. text = "请填写完整信息",
  248. buttons = { "确定" },
  249. on_action = function(self, label)
  250. if label == "确定" then
  251. self:hide()
  252. end
  253. end
  254. })
  255. msg:show()
  256. else
  257. local msg = airui.msgbox({
  258. text = "提交成功!\n姓名: " .. name .. "\n邮箱: " .. email,
  259. buttons = { "确定" },
  260. on_action = function(self, label)
  261. if label == "确定" then
  262. self:hide()
  263. end
  264. end
  265. })
  266. msg:show()
  267. end
  268. end
  269. })
  270. -- 示例5: 输入框控制
  271. airui.label({
  272. parent = scroll_container,
  273. text = "示例5: 输入框控制",
  274. x = 10,
  275. y = 600,
  276. w = 300,
  277. h = 20,
  278. font_size = 14,
  279. })
  280. local control_input = airui.textarea({
  281. parent = scroll_container,
  282. x = 20,
  283. y = 630,
  284. w = 200,
  285. h = 60,
  286. text = "可控制的输入框",
  287. placeholder = "请输入...",
  288. max_len = 50,
  289. keyboard = keyboard1
  290. })
  291. -- 控制按钮
  292. local clear_btn = airui.button({
  293. parent = scroll_container,
  294. x = 230,
  295. y = 630,
  296. w = 70,
  297. h = 30,
  298. text = "清空",
  299. on_click = function(self)
  300. control_input:set_text("")
  301. end
  302. })
  303. local get_btn = airui.button({
  304. parent = scroll_container,
  305. x = 230,
  306. y = 670,
  307. w = 70,
  308. h = 30,
  309. text = "获取",
  310. on_click = function(self)
  311. local text = control_input:get_text()
  312. local msg = airui.msgbox({
  313. text = "当前内容: " .. text,
  314. buttons = { "确定" },
  315. timeout = 2000,
  316. on_action = function(self, label)
  317. self:hide()
  318. end
  319. })
  320. msg:show()
  321. end
  322. })
  323. -- 设置光标位置
  324. local cursor_btn = airui.button({
  325. parent = scroll_container,
  326. x = 20,
  327. y = 700,
  328. w = 130,
  329. h = 35,
  330. text = "光标到开头",
  331. on_click = function(self)
  332. control_input:set_cursor(0)
  333. end
  334. })
  335. -- 底部信息
  336. airui.label({
  337. parent = main_container,
  338. text = "提示: 输入框支持文本监听和控制",
  339. x = 10,
  340. y = 440,
  341. w = 300,
  342. h = 20,
  343. font_size = 14,
  344. })
  345. end
  346. -- 初始化页面
  347. function input_page.init(params)
  348. input_page.create_ui()
  349. end
  350. -- 清理页面
  351. function input_page.cleanup()
  352. if main_container then
  353. main_container:destroy()
  354. main_container = nil
  355. end
  356. end
  357. return input_page