HarmonyOS TextPicker问题?

想把东西给到一个string类型的值上面,那按照现在的方式的话,其实就是想获取到选中的字段的string字符和索引位,那么应该怎么写呢?

阅读 425
1 个回答

可以通过如下方式实现:

private textPickerValue: string = '';
private textPickerIndex: number = 0;

.onChange((value: string | string[], index: number | number[]) => {
  if (Array.isArray(value)) {
    this.textPickerValue= value[0];
  } else {
    this.textPickerValue= value;
  }
  if (Array.isArray(index)) {
    this.textPickerIndex= index[0];
  } else {
    this.textPickerIndex= index;
  }
  console.info('Picker item changed, value: ' + this.textPickerValue+ ', index: ' + this.textPickerIndex)
})

onChange参数返回的就是选中数据,是单选返回的就是字符,多选返回的就是数组,具体看使用场景,可以当做字符使用也可以当做数组使用。

可参照API文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-textpicker-V5\#%E7%A4%BA%E4%BE%8B