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)

    })
  }

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

相关推荐

  1. Android RadioButton+GridLayout实现多行多列的单选效果

    2024-05-16 10:30:08       32 阅读
  2. Android - RadioGroup多个radiobutton同时被选中问题

    2024-05-16 10:30:08       30 阅读
  3. Android RadioButton,定制按钮样式

    2024-05-16 10:30:08       33 阅读
  4. Android 实现跑马灯效果

    2024-05-16 10:30:08       50 阅读
  5. Android 循环滚动的列表-类似弹幕效果

    2024-05-16 10:30:08       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-16 10:30:08       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-16 10:30:08       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-16 10:30:08       87 阅读
  4. Python语言-面向对象

    2024-05-16 10:30:08       96 阅读

热门阅读

  1. 英特尔处理器-----ERMS

    2024-05-16 10:30:08       28 阅读
  2. 科林算法_4 基础算法

    2024-05-16 10:30:08       26 阅读
  3. electron 使用两个页面(额外添加一个html文件)

    2024-05-16 10:30:08       36 阅读
  4. Log4j2滚动策略深度解析:保持日志轻量高效

    2024-05-16 10:30:08       24 阅读
  5. fastapi+vue实现导入Excel表格的功能

    2024-05-16 10:30:08       41 阅读
  6. 编译gdb:在x86虚拟机上,加载分析arm程序及崩溃

    2024-05-16 10:30:08       37 阅读
  7. 贪吃蛇(C++)

    2024-05-16 10:30:08       38 阅读
  8. C#数据库密码加密保存和登录验证方法

    2024-05-16 10:30:08       30 阅读