HarmonyOS @ohos.ArkUI.advanced.SegmentButton (分段按钮) 如何触发点击事件

HarmonyOS
2024-12-18 16:51:14
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

可以在@watch监听方法中编写点击事件的逻辑

import {
  ItemRestriction,
  SegmentButton,
  SegmentButtonOptions,
  SegmentButtonTextItem
} from '@ohos.ArkUI.advanced.SegmentButton'

@Entry
@Component
struct Index {
  @State tabOptions: SegmentButtonOptions = SegmentButtonOptions.tab({
    buttons: [{ text: '页签按钮1' }, { text: '页签按钮2' },
      { text: '页签按钮3' }] as ItemRestriction<SegmentButtonTextItem>,
    backgroundBlurStyle: BlurStyle.BACKGROUND_THICK
  })
  @State tf: boolean = true
  @State @Watch('onSegmentButtonChange') tabSelectedIndexes: number[] = [0]

  onSegmentButtonChange() {
    this.tf = !this.tf
    console.log(`选中按钮索引 -- ${this.tabSelectedIndexes}`);
  }

  aboutToAppear(): void {
    console.log("122233")
  }

  build() {
    Row() {
      Column() {
        Column({ space: 25 }) {
          SegmentButton({ options: this.tabOptions, selectedIndexes: $tabSelectedIndexes })
          TextInput({ text: `${this.tabSelectedIndexes}` }).enabled(this.tf)
        }.width('90%')
      }.width('100%')
    }.height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
分享
微博
QQ
微信
回复
2024-12-18 18:45:05
相关问题
HarmonyOS 回车触发点击事件
788浏览 • 1回复 待解决
HarmonyOS 分段按钮的监听事件
1448浏览 • 1回复 待解决
HarmonyOS ImageSpan点击事件无法触发
1130浏览 • 1回复 待解决
修改分段按钮样式为圆角矩形
1609浏览 • 1回复 待解决