HarmonyOS SegmentButton 点击事件回调是哪个? onclick无回调?

用户点击切换SegmentButton 时,无回调, 回调需要获取到点击按钮的index

阅读 596
1 个回答

参考以下demo:

import {
  ItemRestriction,
  SegmentButton,
  SegmentButtonItemTuple,
  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%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进