1 个回答

ets拿不到组件实例,无法获取组件的值,获取组件值参考如下:

@Entry
@Component
struct Index2 {
  private select: number = 0
  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4']
  private defaultValue: string = ''

  aboutToAppear(): void {
    this.defaultValue = this.fruits[this.select]
    console.log(this.fruits.toString()+"★")
  }

  build() {
    Column() {
      Text('Whether to change a text?').fontSize(16).margin({ bottom: 10 })
      Column() {
        TextPicker({ range: this.fruits })
          .onChange((value: string | string[], index: number | number[]) => {
            // console.info('Picker item changed, value: ' + value + ', index: ' + index)
            this.defaultValue = value.toString()
            console.info('defaultValue是' + this.defaultValue)
          })
          .disappearTextStyle({ color: Color.Red, font: { size: 15, weight: FontWeight.Lighter } })
          .textStyle({ color: Color.Black, font: { size: 20, weight: FontWeight.Normal } })
          .selectedTextStyle({ color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder } })
      }
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('cancel')
          .onClick(() => {
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button('confirm')
          .onClick(() => {
            console.log(this.defaultValue)
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }
    }
  }

TextPicker没有初始化完成事件,只能遍历数据,传的值在数据源中,则默认值为传的值,否则,默认值为为数据源第一条记录。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进