You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.0 KiB

  1. -- 传入互斥的checkbox列表,格式如下
  2. --[[checkBoxs =
  3. {
  4. box_1 = callBack_1,
  5. box_1 = callBack_1,
  6. box_1 = callBack_1,
  7. }--]]
  8. local RadioCheckBox = class("RadioCheckBox")
  9. function RadioCheckBox:ctor()
  10. -- checkBox集合
  11. self.checkBoxs = {}
  12. self.lastSelectedBox = nil
  13. end
  14. -- 注册回调函数
  15. function RadioCheckBox:registerCheckBoxs(checkBoxs, defaultSelect)
  16. -- checkBox集合
  17. self.checkBoxs = checkBoxs
  18. self.lastSelectedBox = defaultSelect
  19. if self.lastSelectedBox then
  20. self.lastSelectedBox:setSelectedState(true)
  21. local callbackfun = self.checkBoxs[self.lastSelectedBox]
  22. if type(callbackfun) == "function" then
  23. callbackfun()
  24. end
  25. end
  26. for i, v in pairs(self.checkBoxs) do
  27. local function onCheckBoxClicked()
  28. if i:getSelectedState() then
  29. if self.lastSelectedBox then
  30. self.lastSelectedBox:setSelectedState(false)
  31. end
  32. -- 回调被选中的函数
  33. if type(v) == "function" then
  34. if v() == false then
  35. i:setSelectedState(false)
  36. if self.lastSelectedBox then
  37. self.lastSelectedBox:setSelectedState(true)
  38. end
  39. return
  40. end
  41. end
  42. else
  43. i:setSelectedState(true)
  44. end
  45. -- 替换上一次的选中
  46. self.lastSelectedBox = i
  47. end
  48. i:addEventListener(onCheckBoxClicked)
  49. end
  50. end
  51. -- 获取最后一次的选中box
  52. function RadioCheckBox:getLastSelectedCheckBox()
  53. return self.lastSelectedBox
  54. end
  55. -- 选中某个按钮
  56. function RadioCheckBox:selectCheckBox(checkBox)
  57. local callbackFunc = self.checkBoxs[checkBox]
  58. if callbackFunc then
  59. if self.lastSelectedBox then
  60. self.lastSelectedBox:setSelectedState(false)
  61. end
  62. checkBox:setSelectedState(true)
  63. -- 回调被选中的函数
  64. if type(callbackFunc) == "function" then
  65. if callbackFunc() == false then
  66. checkBox:setSelectedState(false)
  67. if self.lastSelectedBox then
  68. self.lastSelectedBox:setSelectedState(true)
  69. end
  70. return
  71. end
  72. end
  73. -- 替换上一次的选中
  74. self.lastSelectedBox = checkBox
  75. end
  76. end
  77. return RadioCheckBox