| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- --[[
- @module dropdown_page
- @summary 下拉框组件演示页面
- @version 1.0
- @date 2026.02.05
- @author 江访
- @usage
- 本文件是下拉框组件的演示页面,展示下拉框的各种用法。
- ]]
- local dropdown_page = {}
- -- 页面UI元素
- local main_container = nil
- -- 创建UI
- function dropdown_page.create_ui()
- -- 创建主容器
- main_container = airui.container({
- x = 0,
- y = 0,
- w = 320,
- h = 480,
- color = 0xF5F5F5,
- })
- -- 标题栏
- local title_bar = airui.container({
- parent = main_container,
- x = 0,
- y = 0,
- w = 320,
- h = 50,
- color = 0x795548,
- })
- airui.label({
- parent = title_bar,
- text = "下拉框组件演示",
- x = 10,
- y = 15,
- w = 200,
- h = 20,
- font_size = 16,
- color = 0xFFFFFF,
- })
- -- 返回按钮
- local back_btn = airui.button({
- parent = title_bar,
- x = 250,
- y = 10,
- w = 60,
- h = 30,
- text = "返回",
- on_click = function(self)
- go_back()
- end
- })
- -- 滚动容器
- local scroll_container = airui.container({
- parent = main_container,
- x = 0,
- y = 60,
- w = 320,
- h = 370,
- color = 0xF5F5F5,
- })
- -- 示例1: 基本下拉框
- airui.label({
- parent = scroll_container,
- text = "示例1: 基本下拉框",
- x = 10,
- y = 10,
- w = 300,
- h = 20,
- font_size = 14,
- })
- -- 显示选中项
- local selected_label1 = airui.label({
- parent = scroll_container,
- text = "当前选中: 选项1",
- x = 230,
- y = 45,
- w = 80,
- h = 50,
- font_size = 14,
- })
- local basic_dropdown = airui.dropdown({
- parent = scroll_container,
- x = 20,
- y = 40,
- w = 200,
- h = 40,
- options = {"选项1", "选项2", "选项3", "选项4", "选项5"},
- default_index = 0,
- on_change = function(self,idx)
- local texts = {"选项1", "选项2", "选项3", "选项4", "选项5"}
- selected_label1:set_text("当前选中: " .. texts[idx + 1])
- end
- })
- -- 示例2: 下拉框组
- airui.label({
- parent = scroll_container,
- text = "示例2: 下拉框组",
- x = 10,
- y = 110,
- w = 300,
- h = 20,
- font_size = 14,
- })
- local dropdown_container = airui.container({
- parent = scroll_container,
- x = 20,
- y = 140,
- w = 280,
- h = 150,
- color = 0xEFEBE9,
- radius = 8,
- })
- -- 城市选择
- airui.label({
- parent = dropdown_container,
- text = "城市:",
- x = 10,
- y = 20,
- w = 60,
- h = 20,
- font_size = 14,
- })
- local city_dropdown = airui.dropdown({
- parent = dropdown_container,
- x = 80,
- y = 15,
- w = 180,
- h = 30,
- options = {"北京", "上海", "广州", "深圳", "杭州", "南京", "成都"},
- default_index = 0,
- })
- -- 区县选择
- airui.label({
- parent = dropdown_container,
- text = "区县:",
- x = 10,
- y = 60,
- w = 60,
- h = 20,
- font_size = 14,
- })
- local district_dropdown = airui.dropdown({
- parent = dropdown_container,
- x = 80,
- y = 55,
- w = 180,
- h = 30,
- options = {"请先选择城市"},
- default_index = 0,
- })
- -- 联动选择按钮
- local select_btn = airui.button({
- parent = dropdown_container,
- x = 80,
- y = 100,
- w = 180,
- h = 35,
- text = "确认选择",
- on_click = function(self)
- local city_idx = city_dropdown:get_selected()
- local district_idx = district_dropdown:get_selected()
- local cities = {"北京", "上海", "广州", "深圳", "杭州", "南京", "成都"}
- log.info("dropdown", "选择了城市: " .. cities[city_idx + 1])
- end
- })
- -- 示例3: 获取选中值
- airui.label({
- parent = scroll_container,
- text = "示例3: 获取选中值",
- x = 10,
- y = 310,
- w = 300,
- h = 20,
- font_size = 14,
- })
- local value_dropdown = airui.dropdown({
- parent = scroll_container,
- x = 20,
- y = 340,
- w = 180,
- h = 40,
- options = {"红色", "绿色", "蓝色", "黄色", "紫色"},
- default_index = 2,
- })
- local get_btn = airui.button({
- parent = scroll_container,
- x = 210,
- y = 340,
- w = 80,
- h = 40,
- text = "获取",
- on_click = function(self)
- local idx = value_dropdown:get_selected()
- local colors = {"红色", "绿色", "蓝色", "黄色", "紫色"}
- local msg = airui.msgbox({
- text = "当前选中: " .. colors[idx + 1],
- buttons = {"确定"},
- timeout = 2000,
- on_action = function(self, label)
- self:hide()
- end
- })
- msg:show()
- end
- })
- -- 设置选中项
- local set_btn = airui.button({
- parent = scroll_container,
- x = 20,
- y = 390,
- w = 180,
- h = 40,
- text = "设置为黄色",
- on_click = function(self)
- value_dropdown:set_selected(3) -- 黄色是第4项,索引为3
- end
- })
- -- 底部信息
- airui.label({
- parent = main_container,
- text = "提示: 下拉框支持选项选择和联动",
- x = 10,
- y = 440,
- w = 300,
- h = 20,
- font_size = 14,
- })
- end
- -- 初始化页面
- function dropdown_page.init(params)
- dropdown_page.create_ui()
- end
- -- 清理页面
- function dropdown_page.cleanup()
- if main_container then
- main_container:destroy()
- main_container = nil
- end
- end
- return dropdown_page
|