学习鸿蒙ArkTS编程中,自带的单选按钮不能满足需要,所以自定义了一个单选按钮。效果如下:
4选1
7选1
代码很简单,如下:
@Component export struct customRadio{ @Link ischeck:number//默认选择第一个 @State radionum:number=2//默认创建radio数量为2个 @State radiotext:string[]=['一','二']//选项的文字,默认2个 build(){ Row(){ ForEach(this.radiotext,(item,index)=>{ Text(item).fontSize(14) .width(24) .height(24) .textAlign(TextAlign.Center) .borderRadius(3) .backgroundColor(index==this.ischeck?'#99CCFF':'#ffffff') .onClick(()=>{ this.ischeck=index }) },item=>item) }.height(24).width(24*this.radionum) .justifyContent(FlexAlign.Center) .backgroundColor(Co