1# 文本滑动选择器弹窗 (TextPickerDialog) 2 3根据指定的选择范围创建文本选择器,展示在弹窗上。 4 5> **说明:** 6> 7> 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 8> 9> 本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见[UIContext](../js-apis-arkui-UIContext.md#uicontext)说明。 10> 11> 从API version 10开始,可以通过使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showTextPickerDialog](../js-apis-arkui-UIContext.md#showtextpickerdialog)来明确UI的执行上下文。 12 13## TextPickerDialog 14 15### show 16 17static show(options?: TextPickerDialogOptions) 18 19定义文本滑动选择器弹窗并弹出。 20 21**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 22 23**系统能力:** SystemCapability.ArkUI.ArkUI.Full 24 25**参数:** 26 27| 参数名 | 类型 | 必填 | 说明 | 28| ------- | ----------------------------------------------------------- | ---- | -------------------------- | 29| options | [TextPickerDialogOptions](#textpickerdialogoptions对象说明) | 否 | 配置文本选择器弹窗的参数。 | 30 31## TextPickerDialogOptions对象说明 32 33文本选择器弹窗的参数继承自[TextPickerOptions](ts-basic-components-textpicker.md#textpickeroptions对象说明)。 34 35**系统能力:** SystemCapability.ArkUI.ArkUI.Full 36 37| 名称 | 类型 | 必填 | 说明 | 38| -------- | -------- | -------- | -------- | 39| defaultPickerItemHeight | number \| string | 否 | 设置选择器中选项的高度。<br/>默认值:选中项56vp,非选中项36vp。设置该参数后,选中项与非选中项的高度均为所设置的值。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 40| disappearTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置所有选项中最上和最下两个选项的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff182431',<br/>font: {<br/>size: '14fp', <br/>weight: FontWeight.Regular<br/>}<br/>}<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 41| textStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置所有选项中除了最上、最下及选中项以外的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff182431',<br/>font: {<br/>size: '16fp', <br/>weight: FontWeight.Regular<br/>}<br/>}<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 42| selectedTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置选中项的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff007dff',<br/>font: {<br/>size: '20vp', <br/>weight: FontWeight.Medium<br/>}<br/>}<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 43| acceptButtonStyle<sup>12+</sup> | [PickerDialogButtonStyle](ts-methods-datepicker-dialog.md#pickerdialogbuttonstyle12类型说明) | 否 | 设置确认按钮显示样式、样式和重要程度、角色、背景色、圆角、文本颜色、字号、字体粗细、字体样式、字体列表、按钮是否默认响应Enter键。<br />**说明:**<br />acceptButtonStyle与cancelButtonStyle中最多只能有一个primary字段配置为true,二者primary字段均配置为true时均不生效。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 44| cancelButtonStyle<sup>12+</sup> | [PickerDialogButtonStyle](ts-methods-datepicker-dialog.md#pickerdialogbuttonstyle12类型说明) | 否 | 设置取消按钮显示样式、样式和重要程度、角色、背景色、圆角、文本颜色、字号、字体粗细、字体样式、字体列表、按钮是否默认响应Enter键。<br />**说明:**<br />acceptButtonStyle与cancelButtonStyle中最多只能有一个primary字段配置为true,二者primary字段均配置为true时均不生效。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 45| canLoop<sup>10+</sup> | boolean | 否 | 设置是否可循环滚动,true:可循环,false:不可循环,默认值:true。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 46| alignment<sup>10+</sup> | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment枚举说明) | 否 | 弹窗在竖直方向上的对齐方式。<br>默认值:DialogAlignment.Default<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 47| offset<sup>10+</sup> | [Offset](ts-types.md#offset) | 否 | 弹窗相对alignment所在位置的偏移量。<br/>默认值:{ dx: 0 , dy: 0 }<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 48| maskRect<sup>10+</sup>| [Rectangle](ts-methods-alert-dialog-box.md#rectangle8类型说明) | 否 | 弹窗遮蔽层区域,在遮蔽层区域内的事件不透传,在遮蔽层区域外的事件透传。<br/>默认值:{ x: 0, y: 0, width: '100%', height: '100%' }<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 49| onAccept | (value: [TextPickerResult](#textpickerresult对象说明)) => void | 否 | 点击弹窗中的“确定”按钮时触发该回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 50| onCancel | () => void | 否 | 点击弹窗中的“取消”按钮时触发该回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 51| onChange | (value: [TextPickerResult](#textpickerresult对象说明)) => void | 否 | 滑动弹窗中的选择器使当前选中项改变时触发该回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 | 52| onScrollStop<sup>14+</sup> | (value: [TextPickerResult](#textpickerresult对象说明)) => void | 否 | 滑动弹窗中的选择器的选择列停止触发该回调。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 | 53| backgroundColor<sup>11+</sup> | [ResourceColor](ts-types.md#resourcecolor) | 否 | 弹窗背板颜色。<br/>默认值:Color.Transparent<br/>**说明:** <br/>当设置了backgroundColor为非透明色时,backgroundBlurStyle需要设置为BlurStyle.NONE,否则颜色显示将不符合预期效果。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 54| backgroundBlurStyle<sup>11+</sup> | [BlurStyle](ts-universal-attributes-background.md#blurstyle9) | 否 | 弹窗背板模糊材质。<br/>默认值:BlurStyle.COMPONENT_ULTRA_THICK<br/>**说明:** <br/>设置为BlurStyle.NONE即可关闭背景虚化。当设置了backgroundBlurStyle为非NONE值时,则不要设置backgroundColor,否则颜色显示将不符合预期效果。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 55| onDidAppear<sup>12+</sup> | () => void | 否 | 弹窗弹出时的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onAccept/onCancel/onChange/onScrollStop)>>onWillDisappear>>onDidDisappear。<br />2.在onDidAppear内设置改变弹窗显示效果的回调事件,二次弹出生效。<br />3.快速点击弹出,消失弹窗时,存在onWillDisappear在onDidAppear前生效。<br />4. 当弹窗入场动效未完成时关闭弹窗,该回调不会触发。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 56| onDidDisappear<sup>12+</sup> | () => void | 否 | 弹窗消失时的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onAccept/onCancel/onChange/onScrollStop)>>onWillDisappear>>onDidDisappear。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 | 57| onWillAppear<sup>12+</sup> | () => void | 否 | 弹窗显示动效前的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onAccept/onCancel/onChange/onScrollStop)>>onWillDisappear>>onDidDisappear。<br />2.在onWillAppear内设置改变弹窗显示效果的回调事件,二次弹出生效。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 58| onWillDisappear<sup>12+</sup> | () => void | 否 | 弹窗退出动效前的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onAccept/onCancel/onChange/onScrollStop)>>onWillDisappear>>onDidDisappear。<br />2.快速点击弹出,消失弹窗时,存在onWillDisappear在onDidAppear前生效。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 59| shadow<sup>12+</sup> | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions对象说明) \| [ShadowStyle](ts-universal-attributes-image-effect.md#shadowstyle10枚举说明) | 否 | 设置弹窗背板的阴影。<br />当设备为2in1时,默认场景下获焦阴影值为ShadowStyle.OUTER_FLOATING_MD,失焦为ShadowStyle.OUTER_FLOATING_SM <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。| 60| enableHoverMode<sup>14+</sup> | boolean | 否 | 是否响应悬停态。<br />默认值:false,默认不响应。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。| 61| hoverModeArea<sup>14+</sup> | [HoverModeAreaType](ts-appendix-enums.md#hovermodeareatype14) | 否 | 悬停态下弹窗默认展示区域。<br />默认值:HoverModeAreaType.BOTTOM_SCREEN。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。| 62 63## TextPickerResult对象说明 64 65**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 66 67**系统能力:** SystemCapability.ArkUI.ArkUI.Full 68 69| 名称 | 类型 | 只读 | 可选 | 说明 | 70| -------- | -------- | -------- | -------- | -------- | 71| value | string \| string []<sup>10+</sup> | 否 | 否 | 选中项的文本内容。<br/>**说明**:当显示文本或图片加文本列表时,value值为选中项中的文本值。(文本选择器显示多列时,value为数组类型。)<br/>当显示图片列表时,value值为空。<br/>value值不支持包含转义字符'\\'。 | 72| index | number \| number []<sup>10+</sup> | 否 | 否 | 选中项在选择范围数组中的索引值。(文本选择器显示多列时,index为数组类型。) | 73 74## 示例 75 76> **说明:** 77> 78> 推荐通过使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showTextPickerDialog](../js-apis-arkui-UIContext.md#showtextpickerdialog)来明确UI的执行上下文。 79 80### 示例1(弹出文本选择弹窗) 81 82该示例通过点击按钮弹出文本选择弹窗。 83 84```ts 85// xxx.ets 86@Entry 87@Component 88struct TextPickerDialogExample { 89 private select: number | number[] = 0 90 private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5'] 91 @State v:string = ''; 92 93 build() { 94 Row() { 95 Column() { 96 Button("TextPickerDialog:" + this.v) 97 .margin(20) 98 .onClick(() => { 99 TextPickerDialog.show({ // 建议使用 this.getUIContext().showTextPickerDialog()接口 100 range: this.fruits, 101 selected: this.select, 102 disappearTextStyle: {color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}}, 103 textStyle: {color: Color.Black, font: {size: 20, weight: FontWeight.Normal}}, 104 selectedTextStyle: {color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}}, 105 onAccept: (value: TextPickerResult) => { 106 // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项 107 this.select = value.index 108 console.log(this.select + '') 109 // 点击确定后,被选到的文本数据展示到页面 110 this.v = value.value as string 111 console.info("TextPickerDialog:onAccept()" + JSON.stringify(value)) 112 }, 113 onCancel: () => { 114 console.info("TextPickerDialog:onCancel()") 115 }, 116 onChange: (value: TextPickerResult) => { 117 console.info("TextPickerDialog:onChange()" + JSON.stringify(value)) 118 }, 119 onScrollStop: (value: TextPickerResult) => { 120 console.info("TextPickerDialog:onScrollStop()" + JSON.stringify(value)) 121 }, 122 onDidAppear: () => { 123 console.info("TextPickerDialog:onDidAppear()") 124 }, 125 onDidDisappear: () => { 126 console.info("TextPickerDialog:onDidDisappear()") 127 }, 128 onWillAppear: () => { 129 console.info("TextPickerDialog:onWillAppear()") 130 }, 131 onWillDisappear: () => { 132 console.info("TextPickerDialog:onWillDisappear()") 133 } 134 }) 135 }) 136 }.width('100%') 137 }.height('100%') 138 } 139} 140``` 141 142 143 144 145### 示例2(自定义样式) 146 147该示例通过配置disappearTextStyle、textStyle、selectedTextStyle、acceptButtonStyle、cancelButtonStyle实现了自定义文本以及按钮样式。 148 149```ts 150// xxx.ets 151@Entry 152@Component 153struct TextPickerDialogExample { 154 private select: number | number[] = 0 155 private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5'] 156 @State v:string = ''; 157 158 build() { 159 Row() { 160 Column() { 161 Button("TextPickerDialog:" + this.v) 162 .margin(20) 163 .onClick(() => { 164 TextPickerDialog.show({ // 建议使用 this.getUIContext().showTextPickerDialog()接口 165 range: this.fruits, 166 selected: this.select, 167 disappearTextStyle: {color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}}, 168 textStyle: {color: Color.Black, font: {size: 20, weight: FontWeight.Normal}}, 169 selectedTextStyle: {color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}}, 170 acceptButtonStyle: { type: ButtonType.Normal, style: ButtonStyleMode.NORMAL, role: ButtonRole.NORMAL, fontColor: Color.Red, 171 fontSize: '26fp', fontWeight: FontWeight.Bolder, fontStyle: FontStyle.Normal, fontFamily: 'sans-serif', backgroundColor:'#80834511', 172 borderRadius: 20 }, 173 cancelButtonStyle: { type: ButtonType.Normal, style: ButtonStyleMode.NORMAL, role: ButtonRole.NORMAL, fontColor: Color.Blue, 174 fontSize: '16fp', fontWeight: FontWeight.Normal, fontStyle: FontStyle.Italic, fontFamily: 'sans-serif', backgroundColor:'#50182431', 175 borderRadius: 10 }, 176 onAccept: (value: TextPickerResult) => { 177 // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项 178 this.select = value.index 179 console.log(this.select + '') 180 // 点击确定后,被选到的文本数据展示到页面 181 this.v = value.value as string 182 console.info("TextPickerDialog:onAccept()" + JSON.stringify(value)) 183 }, 184 onCancel: () => { 185 console.info("TextPickerDialog:onCancel()") 186 }, 187 onChange: (value: TextPickerResult) => { 188 console.info("TextPickerDialog:onChange()" + JSON.stringify(value)) 189 }, 190 onScrollStop: (value: TextPickerResult) => { 191 console.info("TextPickerDialog:onScrollStop()" + JSON.stringify(value)) 192 }, 193 onDidAppear: () => { 194 console.info("TextPickerDialog:onDidAppear()") 195 }, 196 onDidDisappear: () => { 197 console.info("TextPickerDialog:onDidDisappear()") 198 }, 199 onWillAppear: () => { 200 console.info("TextPickerDialog:onWillAppear()") 201 }, 202 onWillDisappear: () => { 203 console.info("TextPickerDialog:onWillDisappear()") 204 } 205 }) 206 }) 207 }.width('100%') 208 }.height('100%') 209 } 210} 211``` 212 213 214 215### 示例3(悬停态弹窗) 216 217该示例展示了在折叠屏悬停态下设置dialog布局区域的效果。 218 219```ts 220@Entry 221@Component 222struct TextPickerDialogExample { 223 private select: number | number[] = 0; 224 private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']; 225 @State v: string = ''; 226 227 build() { 228 Row() { 229 Column() { 230 Button("TextPickerDialog:" + this.v) 231 .margin(20) 232 .onClick(() => { 233 TextPickerDialog.show({ // 建议使用 this.getUIContext().showTextPickerDialog()接口 234 range: this.fruits, 235 selected: this.select, 236 disappearTextStyle: { color: Color.Red, font: { size: 15, weight: FontWeight.Lighter }}, 237 textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal }}, 238 selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder }}, 239 onAccept: (value: TextPickerResult) => { 240 // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项 241 this.select = value.index; 242 console.log(this.select + ''); 243 // 点击确定后,被选到的文本数据展示到页面 244 this.v = value.value as string; 245 console.info("TextPickerDialog:onAccept()" + JSON.stringify(value)); 246 }, 247 onCancel: () => { 248 console.info("TextPickerDialog:onCancel()"); 249 }, 250 onChange: (value: TextPickerResult) => { 251 console.info("TextPickerDialog:onChange()" + JSON.stringify(value)); 252 }, 253 onScrollStop: (value: TextPickerResult) => { 254 console.info("TextPickerDialog:onScrollStop()" + JSON.stringify(value)) 255 }, 256 onDidAppear: () => { 257 console.info("TextPickerDialog:onDidAppear()"); 258 }, 259 onDidDisappear: () => { 260 console.info("TextPickerDialog:onDidDisappear()"); 261 }, 262 onWillAppear: () => { 263 console.info("TextPickerDialog:onWillAppear()"); 264 }, 265 onWillDisappear: () => { 266 console.info("TextPickerDialog:onWillDisappear()"); 267 }, 268 enableHoverMode: true, 269 hoverModeArea: HoverModeAreaType.TOP_SCREEN 270 }) 271 }) 272 }.width('100%') 273 }.height('100%') 274 } 275} 276``` 277 278