main.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. -- Dropdown 组件测试脚本
  2. PROJECT = "easylvgl"
  3. VERSION = "1.0.0"
  4. sys.taskInit(function()
  5. local ret = easylvgl.init(800, 600, easylvgl.COLOR_FORMAT_ARGB8888)
  6. if not ret then
  7. log.error("easylvgl", "dropdown init failed")
  8. return
  9. end
  10. -- 下拉框组件
  11. local dropdown = easylvgl.dropdown({
  12. parent = easylvgl.screen, -- 父对象,可选,默认当前屏幕
  13. options = {"Option A", "Option B", "Option C"}, -- 选项列表(字符串数组)
  14. default_index = 2, -- 默认选中项索引,默认 -1
  15. x = 40, y = 60, w = 180, h = 50,
  16. on_change = function(self, index) -- 选中项变化回调
  17. log.info("dropdown", "selected index", index)
  18. end
  19. })
  20. local btn = easylvgl.button({
  21. parent = easylvgl.screen, -- 父对象,可选,默认当前屏幕
  22. text = "set selected to A",
  23. x = 240, y = 60, w = 160, h = 48,
  24. on_click = function(self)
  25. local selected = dropdown:get_selected() -- 获取当前选中项索引
  26. log.info("dropdown", "previous selected index is", selected)
  27. dropdown:set_selected(0) -- 设置选中项为1
  28. log.info("dropdown", "now set selected to A")
  29. end
  30. })
  31. while true do
  32. easylvgl.refresh()
  33. sys.wait(10)
  34. end
  35. end)
  36. sys.run()