HarmonyOS ArkTS 实现类似Android中RadioButton得效果

在Android中如实现下图可以用radioGroup和RadioButton实现,但在ArkTs中radio不能实现自定义样式,所以用Tabs来实现这种效果,效果图如下:

一、效果图

在这里插入图片描述
在这里插入图片描述

二、实现横向布局的三个TabContent,代码如下

 @State currentIndex: number = 0;


  Row() {
            Tabs({ barPosition: BarPosition.Start }) {
              TabContent() {
              }.tabBar(this.TabBuilder('按钮1', 0))


              TabContent() {
              }.tabBar(this.TabBuilder('按钮2', 1));


              TabContent() {
              }.tabBar(this.TabBuilder('按钮3', 2))

            }.scrollable(false).barMode(BarMode.Fixed).barHeight(45)

          }
          .height(45)
          .width('80%')
          .margin({ top: 15, bottom: 15 })
          .backgroundColor($r('app.color.radio_button_no'))
          .borderRadius(10)
          .justifyContent(FlexAlign.Center)
          .alignItems(VerticalAlign.Center)

三、TabContent单独得样式,样式可自定义字体颜色背景等,根据自己的需求进行调整


  @Builder TabBuilder(title: string, index: number) {
    Column() {
      Text(title)
        // .margin({ top: $r('app.float.mainPage_baseTab_top') })
        .fontSize(16)
        .fontWeight(FontWeight.Bold)
        .fontColor(this.currentIndex === index ? $r('app.color.white') : $r('app.color.title_text_color'))
    }
    .justifyContent(FlexAlign.Center)
    .height(43)
    .height('90%')
    .width(CommonConstants.FULL_PARENT)
    .borderRadius(10)
    .backgroundColor(this.currentIndex === index ? $r('app.color.radio_button_yes') : $r('app.color.radio_button_no'))
    .onClick(() => {

      this.currentIndex = index;
      console.log('index-------' + index)

    })
  }

这样就实现了按钮切换的效果,如果还有其他更好的方法欢迎评论交流!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

志尊宝

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值