当前位置:   article > 正文

鸿蒙HarmonyOS NEXT开发:简易随机抽人功能,动态展示

鸿蒙HarmonyOS NEXT开发:简易随机抽人功能,动态展示

效果图:

状态变量定义模块

  • @State name:string[]:这是一个存储所有拟参与抽奖人员姓名的数组,初始化时包含了一系列的姓名。
  • @State selectedName:string[]:用于记录已经被抽中的人员姓名列表,初始为空。
  • @State random:string:保存每次随机抽取到的具体人员姓名,初始为空字符串。
  • @State compile:boolean:决定是否处于编辑状态,初始为 false,表示非编辑状态。
  • @State addName:string:用于存储在编辑状态下输入的新姓名。
  • @State randomList:number[]:存储随机生成的数字列表,用于辅助确定选中状态。
  • @State timeId:number:用于存储延迟函数的时间 ID,执行延迟函数用到
    1. //拟 姓名数组
    2. @State name:string[]=['张三','李四','王五','汐月',
    3. '映雪','俊翔','沐晨','羽灵','梦璃','逸风',
    4. '嘉悦','静婉','梵音','睿泽宇轩','诗韵雅琪','楚瑶慧敏','子昂思涵','锦程妙晴',
    5. '雨昕晨熙','明轩紫涵','梓萱靖瑶','宇澄语琴','泽翰悦萱']
    6. //已抽中的名单列表
    7. @State selectedName:string[] = []
    8. //随机抽取到的人
    9. @State random:string = ''
    10. //是否为编辑状态
    11. @State compile:boolean = false
    12. //添加姓名
    13. @State addName:string = ''
    14. //随机抽取到的数字列表
    15. @State randomList:number[] = []
    16. //延迟函数的时间id
    17. @State timeId:number = -1

抽奖按钮模块

当点击抽奖按钮时,首先将上一次抽取的结果 random 置为空。然后清除可能存在的未结束的延迟事件。通过设定 num 为 15,决定生成随机数的次数。在延迟函数中,每 200 毫秒执行一次,先清空 randomList,然后生成 num/3 个随机数。当 num 小于等于 0 时,通过循环确保抽取的姓名不在已抽中的名单中,最后将抽中的姓名加入 selectedName 列表,并清除延迟事件。

  1. // 编辑状态或已将人抽完时 为禁用状态
  2. Button('抽奖')
  3. .enabled(this.selectedName.length < this.name.length && !this.compile)
  4. .onClick(()=>{
  5. this.random = '' //先删除上一次抽中的
  6. clearInterval(this.timeId) //为了防止连续点击导致上一个延迟事件未结束 先将上一个杀死
  7. let num:number=15 //为了生成num/3(取整)个随机数
  8. this.timeId =setInterval(()=>{ //200ms执行一次
  9. this.randomList = [] //先清空
  10. for(let i=0;i<num/3;i++) //生成num/3(取整)个随机数
  11. this.randomList.push(Math.floor(Math.random()*this.name.length))
  12. //最后一次时为确定抽中的姓名
  13. if(num<=0) {
  14. //防止抽到重复的人
  15. let f:boolean = true
  16. while (f){
  17. this.random = this.name[Math.floor(Math.random()*this.name.length)]
  18. //抽到的不在已抽中的名单列表中 则跳出循环
  19. if(this.selectedName.indexOf(this.random)==-1) f=false
  20. }
  21. //将抽到的进行统计 加入列表
  22. this.selectedName.push(this.random)
  23. clearInterval(this.timeId)
  24. }
  25. num--
  26. },200)
  27. })

编辑名单按钮模块

点击编辑名单按钮时,通过 this.compile =!this.compile 切换编辑状态。为编辑状态时渲染不同的视图,可以点击右上角的x删除该姓名,也可以通过输入框添加姓名。

重置按钮模块

点击重置按钮,将已抽中的名单 selectedName 清空,同时将随机抽取的结果 random 和随机数字列表 randomList 也都重置为空。

姓名展示视图模块

在编辑状态下,会显示添加姓名的输入框和确定按钮。对于每个姓名,通过判断其在随机数字列表中的索引或是否与随机抽取的结果相同,来设置不同的字体颜色和边框颜色。在编辑状态下,还会显示删除符号,点击可删除对应的姓名。

比如,在显示姓名时,如果某个姓名处于被抽取的状态,其颜色和边框会相应变化,以突出显示。

通过合理的状态管理和逻辑控制,实现了一个简单但实用的随机抽人功能,并提供了编辑和重置的功能,具有一定的灵活性和可用性。

  1. // 所有姓名的展示视图
  2. Flex({wrap:FlexWrap.Wrap}){
  3. // 编辑状态时 可添加姓名
  4. if(this.compile)
  5. Row({space:10}){
  6. TextInput({placeholder:'添加姓名'})
  7. .onChange((value:string)=>{this.addName = value})
  8. .layoutWeight(1)
  9. Button('确定')
  10. .onClick(()=>{this.name.push(this.addName)})
  11. }
  12. ForEach(this.name,(item:string, index:number)=>{
  13. // 设置选中状态和为选中状态的样式
  14. Row(){
  15. Row(){
  16. Text(item)
  17. .borderWidth(2)
  18. .padding(5)
  19. .fontColor((this.randomList.indexOf(index)!=-1||this.random==item)?Color.Orange:Color.Black)
  20. .borderColor((this.randomList.indexOf(index)!=-1||this.random==item)?Color.Red:Color.Black)
  21. }
  22. .margin(5)
  23. .border({width:3,color:this.random==item?Color.Red:Color.Transparent})
  24. // 编辑状态 显示x删除符号
  25. if(this.compile)
  26. Text('+')
  27. .fontSize(21)
  28. .fontWeight(800)
  29. .rotate({angle:45}) //旋转45
  30. .position({right:2,top:-2})
  31. .onClick(()=>{
  32. this.name.splice(index,1) //删除
  33. })
  34. }
  35. })
  36. }
  37. .width('100%')

完整代码:

  1. @Entry
  2. @Component
  3. struct IndexPage {
  4. //拟 姓名数组
  5. @State name:string[]=['张三','李四','王五','汐月',
  6. '映雪','俊翔','沐晨','羽灵','梦璃','逸风',
  7. '嘉悦','静婉','梵音','睿泽宇轩','诗韵雅琪','楚瑶慧敏','子昂思涵','锦程妙晴',
  8. '雨昕晨熙','明轩紫涵','梓萱靖瑶','宇澄语琴','泽翰悦萱']
  9. //已抽中的名单列表
  10. @State selectedName:string[] = []
  11. //随机抽取到的人
  12. @State random:string = ''
  13. //是否为编辑状态
  14. @State compile:boolean = false
  15. //添加姓名
  16. @State addName:string = ''
  17. //随机抽取到的数字列表
  18. @State randomList:number[] = []
  19. //延迟函数的时间id
  20. @State timeId:number = -1
  21. build() {
  22. Column({space:10}) {
  23. // 已抽中的名单
  24. Column(){
  25. Text('已抽中的名单:')
  26. Flex({wrap:FlexWrap.Wrap}){
  27. ForEach(this.selectedName,(item:string)=>{
  28. Text(item)
  29. .padding(5)
  30. .height(32)
  31. .margin(5)
  32. })
  33. }
  34. .width('100%')
  35. }
  36. .alignItems(HorizontalAlign.Start)
  37. Row({space:10}){
  38. // 编辑状态或已将人抽完时 为禁用状态
  39. Button('抽奖')
  40. .enabled(this.selectedName.length < this.name.length && !this.compile)
  41. .onClick(()=>{
  42. this.random = '' //先删除上一次抽中的
  43. clearInterval(this.timeId) //为了防止连续点击导致上一个延迟事件未结束 先将上一个杀死
  44. let num:number=15 //为了生成num/3(取整)个随机数
  45. this.timeId =setInterval(()=>{ //200ms执行一次
  46. this.randomList = [] //先清空
  47. for(let i=0;i<num/3;i++) //生成num/3(取整)个随机数
  48. this.randomList.push(Math.floor(Math.random()*this.name.length))
  49. //最后一次时为确定抽中的姓名
  50. if(num<=0) {
  51. //防止抽到重复的人
  52. let f:boolean = true
  53. while (f){
  54. this.random = this.name[Math.floor(Math.random()*this.name.length)]
  55. //抽到的不在已抽中的名单列表中 则跳出循环
  56. if(this.selectedName.indexOf(this.random)==-1) f=false
  57. }
  58. //将抽到的进行统计 加入列表
  59. this.selectedName.push(this.random)
  60. clearInterval(this.timeId)
  61. }
  62. num--
  63. },200)
  64. })
  65. Button('编辑名单')
  66. .onClick(()=>{this.compile=!this.compile})
  67. Button('重置')
  68. .onClick(()=>{this.selectedName=[]
  69. this.random = ''
  70. this.randomList=[]})
  71. }
  72. // 所有姓名的展示视图
  73. Flex({wrap:FlexWrap.Wrap}){
  74. // 编辑状态时 可添加姓名
  75. if(this.compile)
  76. Row({space:10}){
  77. TextInput({placeholder:'添加姓名'})
  78. .onChange((value:string)=>{this.addName = value})
  79. .layoutWeight(1)
  80. Button('确定')
  81. .onClick(()=>{this.name.push(this.addName)})
  82. }
  83. ForEach(this.name,(item:string, index:number)=>{
  84. // 设置选中状态和为选中状态的样式
  85. Row(){
  86. Row(){
  87. Text(item)
  88. .borderWidth(2)
  89. .padding(5)
  90. .fontColor((this.randomList.indexOf(index)!=-1||this.random==item)?Color.Orange:Color.Black)
  91. .borderColor((this.randomList.indexOf(index)!=-1||this.random==item)?Color.Red:Color.Black)
  92. }
  93. .margin(5)
  94. .border({width:3,color:this.random==item?Color.Red:Color.Transparent})
  95. // 编辑状态 显示x删除符号
  96. if(this.compile)
  97. Text('+')
  98. .fontSize(21)
  99. .fontWeight(800)
  100. .rotate({angle:45}) //旋转45
  101. .position({right:2,top:-2})
  102. .onClick(()=>{
  103. this.name.splice(index,1) //删除
  104. })
  105. }
  106. })
  107. }
  108. .width('100%')
  109. }
  110. .padding(15)
  111. .height('100%')
  112. .width('100%')
  113. }
  114. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/986678
推荐阅读
相关标签
  

闽ICP备14008679号