1 个回答

当前textPicker不支持label/value形式:可以获取选择器值的索引,根据索引自行封装映射

下面一个demo可以参考:

interface Fruits { 
  id: number 
  name: string 
} 
 
@Entry 
@Component 
struct TextPickerExample { 
  private select: number = 1 
  private fruitsList: Fruits[] = [ 
    { id: 1, name: "apple1" }, 
    { id: 2, name: "orange2" }, 
    { id: 3, name: "peach3" }, 
    { id: 4, name: "grape4" }, 
  ] 
 
  build() { 
    Column() { 
      TextPicker({ range: this.fruitsList.map((item) => item.name), selected: this.select }) 
        .onChange((value: string | string[], index: number | number[]) => { 
          console.info('Picker item changed, value: ' + value + ', index: ' + index) 
          this.fruitsList[index.toString()].id; 
        }) 
        .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 } }) 
    }.width('100%').height('100%') 
  } 
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进