1# Select 2 3提供下拉选择菜单,可以让用户在多个选项之间选择。 4 5> **说明:** 6> 7> 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8 9## 子组件 10 11无 12 13## 接口 14 15Select(options: Array\<[SelectOption](#selectoption对象说明)\>) 16 17## SelectOption对象说明 18 19| 参数名 | 参数类型 | 必填 | 参数描述 | 20| ------ | ----------------------------------- | ---- | -------------- | 21| value | [ResourceStr](ts-types.md#resourcestr) | 是 | 下拉选项内容。 | 22| icon | [ResourceStr](ts-types.md#resourcestr) | 否 | 下拉选项图片。 | 23 24## 属性 25 26除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性: 27 28| 名称 | 参数类型 | 描述 | 29| ----------------------- | ------------------------------------- | --------------------------------------------- | 30| selected | number | 设置下拉菜单初始选项的索引,第一项的索引为0。<br>当不设置selected属性时,默认选择值为-1,菜单项不选中;当设置为undefined、null时,选中第一项。<br />从API version 10开始,该属性支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量。 | 31| value | string | 设置下拉按钮本身的文本内容。当菜单选中时默认会替换为菜单项文本内容。<br />从API version 10开始,该参数支持[$$](../../quick-start/arkts-two-way-sync.md)双向绑定变量。 | 32| font | [Font](ts-types.md#font) | 设置下拉按钮本身的文本样式。<br/>默认值:<br/>{<br/>size: '16fp',<br/>weight: FontWeight.Medium<br/>} | 33| fontColor | [ResourceColor](ts-types.md#resourcecolor) | 设置下拉按钮本身的文本颜色。<br/>默认值:'\#E6FFFFFF' | 34| selectedOptionBgColor | [ResourceColor](ts-types.md#resourcecolor) | 设置下拉菜单选中项的背景色。<br/>默认值:'\#33007DFF' | 35| selectedOptionFont | [Font](ts-types.md#font) | 设置下拉菜单选中项的文本样式。<br/>默认值:<br/>{<br/>size: '16fp',<br/>weight: FontWeight.Regular<br/>} | 36| selectedOptionFontColor | [ResourceColor](ts-types.md#resourcecolor) | 设置下拉菜单选中项的文本颜色。<br/>默认值:'\#ff007dff' | 37| optionBgColor | [ResourceColor](ts-types.md#resourcecolor) | 设置下拉菜单项的背景色。<br/>默认值:'\#ffffffff' | 38| optionFont | [Font](ts-types.md#font) | 设置下拉菜单项的文本样式。<br/>默认值:<br/>{<br/>size: '16fp',<br/>weight: FontWeight.Regular<br/>} | 39| optionFontColor | [ResourceColor](ts-types.md#resourcecolor) | 设置下拉菜单项的文本颜色。<br/>默认值:'\#ff182431' | 40| space<sup>10+</sup> | [Length](ts-types.md#length) | 设置下拉菜单项的文本与箭头之间的间距。<br/>**说明:** <br/>不支持设置百分比。 | 41| arrowPosition<sup>10+</sup> | [ArrowPosition](#arrowposition10枚举说明) | 设置下拉菜单项的文本与箭头之间的对齐方式。<br/>默认值:ArrowPosition.END | 42| menuAlign<sup>10+</sup> | alignType: [MenuAlignType](#menualigntype10枚举说明),<br/> offset?: [Offset](ts-types.md#offset) | 设置下拉按钮与下拉菜单间的对齐方式。<br/> -alignType: 对齐方式类型,必填。 <br/> -offset: 按照对齐类型对齐后,下拉菜单相对下拉按钮的偏移量。<br/> 默认值:{dx: 0, dy: 0}| 43 44## ArrowPosition<sup>10+</sup>枚举说明 45 46| 名称 | 描述 | 47| ------------------- | ------------------ | 48| END<sup>10+</sup> | 文字在前,箭头在后。 | 49| START<sup>10+</sup> | 箭头在前,文字在后。 | 50 51 52## MenuAlignType<sup>10+</sup>枚举说明 53 54| 名称 | 描述 | 55| ------------------- | ------------------ | 56| START | 按照语言方向起始端对齐。 | 57| CENTER | 居中对齐。 | 58| END | 按照语言方向末端对齐。 | 59 60## 事件 61 62| 名称 | 功能描述 | 63| ------------------------------------------------------------ | ------------------------------------------------------------ | 64| onSelect(callback: (index: number, value?: string) => void) | 下拉菜单选中某一项的回调。<br/>index:选中项的索引。<br/>value:选中项的值。 | 65 66## 示例 67 68```ts 69// xxx.ets 70@Entry 71@Component 72struct SelectExample { 73 @State text: string = "TTTTT" 74 @State index: number = 2 75 @State space: number = 8 76 @State arrowPosition: ArrowPosition = ArrowPosition.END 77 build() { 78 Column() { 79 Select([{ value: 'aaa', icon: "/common/public_icon.svg" }, 80 { value: 'bbb', icon: "/common/public_icon.svg" }, 81 { value: 'ccc', icon: "/common/public_icon.svg" }, 82 { value: 'ddd', icon: "/common/public_icon.svg" }]) 83 .selected(this.index) 84 .value(this.text) 85 .font({ size: 16, weight: 500 }) 86 .fontColor('#182431') 87 .selectedOptionFont({ size: 16, weight: 400 }) 88 .optionFont({ size: 16, weight: 400 }) 89 .space(this.space) 90 .arrowPosition(this.arrowPosition) 91 .menuAlign(MenuAlignType.START, {dx:0, dy:0}) 92 .onSelect((index:number, text?: string | undefined)=>{ 93 console.info('Select:' + index) 94 this.index = index; 95 if(text){ 96 this.text = text; 97 } 98 }) 99 }.width('100%') 100 } 101} 102``` 103 104 105