当前位置:   article > 正文

鸿蒙ArkUI封装的单选组件_鸿蒙自定义选择框

鸿蒙自定义选择框

Radio是单选框组件,通常用于提供相应的用户交互选择项,同一组的Radio中只有一个可以被选中。
ArkUI创建一个单选框,其中value是单选框的名称,group是单选框的所属群组名称。checked属性可以设置单选框的状态,状态分别为false和true时,设置为true时表示单选框被选中。Radio仅支持选中和未选中两种样式,不支持自定义颜色和形状。
Radio({ value: 'Radio1', group: 'radioGroup' })
  .checked(false)
Radio({ value: 'Radio2', group: 'radioGroup' })
  .checked(true)
当使用的时,发现不是很友好,没有对应的文本,而且点击事件不方便选中单选框。

DIY可视化低代码鸿蒙开发发现使用过程非常得不方便,至此我们封装了一套自己的单选框组件,其中单选框用了图片来表示显中不显中,而且还增加了标签。非常得方便。

  1. import {DynamicObject} from './type'
  2. /**
  3. * 自定义颜色
  4. */
  5. @Component
  6. export default struct DiygwRadio{
  7. //绑定的值
  8. @Link @Watch('onValue') value:string;
  9. // 保存所有单选框的名称
  10. @State list: DynamicObject[] = [];
  11. // 隐藏值
  12. @State valueField: string = 'value';
  13. // 显示值
  14. @State labelField: string = 'label';
  15. // 选中/未选中状态下的图标
  16. @State checkedValues: Resource[] = [];
  17. //选中图标
  18. @State checkedImg: Resource = $r('app.media.radioon');
  19. //未选中图标
  20. @State noCheckedImg: Resource = $r('app.media.radio');
  21. //未选中图标
  22. @State labelImg: Resource = $r('app.media.user');
  23. //是否文本图片
  24. @State isLabelImg: boolean = false;
  25. @State labelImgWidth: number = 20;
  26. @State labelImgHeight: number = 20;
  27. //标题文本
  28. @State label:string = '单选';
  29. //水平状态时,文本占大小
  30. @State labelWidth:number = 80;
  31. //是否标题文本换行
  32. @State isWrapLabel:boolean = true;
  33. //是否标题文本
  34. @State isLabel:boolean = true;
  35. //文本字体大小
  36. @State textSize:number = 14;
  37. //选中图版本大小
  38. @State imgSize:number = 28;
  39. //每个占比
  40. @State itemWidth:string = '33%';
  41. //组件内边距
  42. @State formPadding:number = 5;
  43. //初始化选中
  44. initCheck(){
  45. for (let i = 0; i < this.list.length; i++) {
  46. if(this.list[i][this.valueField] == this.value) {
  47. this.checkedValues[i] = this.checkedImg;
  48. }else{
  49. this.checkedValues[i] = this.noCheckedImg;
  50. }
  51. }
  52. }
  53. //监听选中
  54. onValue() {
  55. this.initCheck()
  56. }
  57. onChecked(index: number){
  58. //点击文本选中当前单选框
  59. for (let i = 0; i < this.list.length; i++) {
  60. this.checkedValues[i] = this.noCheckedImg;
  61. }
  62. this.checkedValues[index] = this.checkedImg;
  63. this.value = this.list[index][this.valueField];
  64. }
  65. build() {
  66. Flex({
  67. alignItems:this.isWrapLabel?ItemAlign.Start:ItemAlign.Center,
  68. direction:this.isWrapLabel?FlexDirection.Column:FlexDirection.Row,
  69. justifyContent:FlexAlign.Start
  70. }){
  71. if(this.isLabel){
  72. Row(){
  73. if(this.isLabelImg){
  74. Image(this.labelImg)
  75. .width(this.labelImgWidth)
  76. .height(this.labelImgHeight)
  77. .margin({ left:3 })
  78. }
  79. Text(this.label).width(this.isWrapLabel?'100%':this.labelWidth).fontSize(this.textSize).margin({
  80. bottom:this.isWrapLabel?15:0
  81. }).textAlign(TextAlign.Start);
  82. }
  83. }
  84. Flex({
  85. wrap:FlexWrap.Wrap
  86. }){
  87. ForEach(this.list, (item: any,index: number) => {
  88. Row(){
  89. Image(this.checkedValues[index])
  90. .borderRadius('50%')
  91. .size({width: this.imgSize , height: this.imgSize}).margin({
  92. top:1,
  93. bottom:1
  94. })
  95. Text(item[this.labelField])
  96. .fontSize(this.textSize)
  97. .margin({left: 10})
  98. }.onClick(()=>{
  99. this.onChecked(index)
  100. }).width(this.itemWidth)
  101. })
  102. }.width('100%')
  103. }.padding(this.formPadding)
  104. .onAppear(() => {
  105. this.initCheck()
  106. })
  107. }
  108. }

  1. DiygwRadio({
  2. itemWidth:'50%',
  3. value: $radio,
  4. list: [{
  5. value: 'radio1',
  6. label: '单选1'
  7. }, {
  8. value: 'radio2',
  9. label: '单选2'
  10. }, {
  11. value: 'radio3',
  12. label: '单选3'
  13. }, {
  14. value: 'radio21',
  15. label: '单选2'
  16. }, {
  17. value: 'radio31',
  18. label: '单选3'
  19. }]
  20. }).backgroundColor('#fff').margin(10).borderRadius(5)
  21. DiygwRadio({
  22. value: $radio,
  23. itemWidth:'50%',
  24. valueField: 'value1',
  25. labelField: 'label1',
  26. list: [{
  27. value1: 'radio1',
  28. label1: '单选1'
  29. }, {
  30. value1: 'radio2',
  31. label1: '单选2'
  32. }, {
  33. value1: 'radio3',
  34. label1: '单选3'
  35. }]
  36. }).backgroundColor('#fff').margin(10).borderRadius(5)
  37. DiygwRadio({
  38. value: $radio,
  39. itemWidth:'100%',
  40. valueField: 'value1',
  41. labelField: 'label1',
  42. list: [{
  43. value1: 'radio1',
  44. label1: '单选1'
  45. }, {
  46. value1: 'radio2',
  47. label1: '单选2'
  48. }, {
  49. value1: 'radio3',
  50. label1: '单选3'
  51. }]
  52. }).backgroundColor('#fff').margin(10).borderRadius(5)

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

闽ICP备14008679号