• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 文本滑动选择器弹窗 (TextPickerDialog)
2
3根据指定的选择范围创建文本选择器,展示在弹窗上。
4
5>  **说明:**
6>
7> 该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。
8>
9> 本模块功能依赖UI的执行上下文,不可在UI上下文不明确的地方使用,参见[UIContext](../js-apis-arkui-UIContext.md#uicontext)说明。
10>
11> 该组件不支持在Wearable设备上使用。
12
13## TextPickerDialog
14
15### show<sup>(deprecated)</sup>
16
17static show(options?: TextPickerDialogOptions)
18
19定义文本滑动选择器弹窗并弹出。
20
21> **说明:**
22>
23> 从API version 18开始废弃,建议使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showTextPickerDialog](../js-apis-arkui-UIContext.md#showtextpickerdialog)替代。
24>
25> 从API version 10开始,可以通过使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showTextPickerDialog](../js-apis-arkui-UIContext.md#showtextpickerdialog)来明确UI的执行上下文。
26
27**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
28
29**系统能力:** SystemCapability.ArkUI.ArkUI.Full
30
31**参数:**
32
33| 参数名  | 类型                                                        | 必填 | 说明                       |
34| ------- | ----------------------------------------------------------- | ---- | -------------------------- |
35| options | [TextPickerDialogOptions](#textpickerdialogoptions对象说明) | 否   | 配置文本选择器弹窗的参数。 |
36
37## TextPickerDialogOptions对象说明
38
39文本选择器弹窗的参数继承自[TextPickerOptions](ts-basic-components-textpicker.md#textpickeroptions对象说明)。
40
41**系统能力:** SystemCapability.ArkUI.ArkUI.Full
42
43| 名称 | 类型 | 必填 |  说明 |
44| -------- | -------- | -------- |  -------- |
45| defaultPickerItemHeight | number \| string | 否 | 设置选择器中选项的高度。number类型取值范围:[0, +∞),string类型仅支持number类型取值的字符串形式,例如"56"。<br/>默认值:选中项56vp,非选中项36vp。设置该参数后,选中项与非选中项的高度均为所设置的值。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
46| 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开始,该接口支持在原子化服务中使用。 |
47| 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开始,该接口支持在原子化服务中使用。 |
48| selectedTextStyle<sup>10+</sup> | [PickerTextStyle](ts-basic-components-datepicker.md#pickertextstyle10类型说明) | 否 | 设置选中项的文本颜色、字号、字体粗细。<br/>默认值:<br/>{<br/>color: '#ff007dff',<br/>font: {<br/>size: '20fp', <br/>weight: FontWeight.Medium<br/>}<br/>}<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
49| acceptButtonStyle<sup>12+</sup> | [PickerDialogButtonStyle](ts-methods-datepicker-dialog.md#pickerdialogbuttonstyle12类型说明) | 否 | 设置确认按钮显示样式、样式和重要程度、角色、背景色、圆角、文本颜色、字号、字体粗细、字体样式、字体列表、按钮是否默认响应Enter键。<br />**说明:**<br/>1.acceptButtonStyle与cancelButtonStyle中最多只能有一个primary字段配置为true,如果同时设置为true,则primary字段不生效,保持默认值false。<br/>2.按钮高度默认40vp,在关怀模式-大字体场景下高度不变,即使按钮样式设置为圆角矩形[ROUNDED_RECTANGLE](ts-basic-components-button.md#buttontype枚举说明),呈现效果依然是胶囊型按钮[Capsule](ts-basic-components-button.md#buttontype枚举说明)。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
50| cancelButtonStyle<sup>12+</sup> | [PickerDialogButtonStyle](ts-methods-datepicker-dialog.md#pickerdialogbuttonstyle12类型说明) | 否 | 设置取消按钮显示样式、样式和重要程度、角色、背景色、圆角、文本颜色、字号、字体粗细、字体样式、字体列表、按钮是否默认响应Enter键。<br />**说明:**<br/>1.acceptButtonStyle与cancelButtonStyle中最多只能有一个primary字段配置为true,如果同时设置为true,则primary字段不生效,保持默认值false。<br/>2.按钮高度默认40vp,在关怀模式-大字体场景下高度不变,即使按钮样式设置为圆角矩形[ROUNDED_RECTANGLE](ts-basic-components-button.md#buttontype枚举说明),呈现效果依然是胶囊型按钮[Capsule](ts-basic-components-button.md#buttontype枚举说明)。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
51| canLoop<sup>10+</sup> | boolean | 否 | 设置是否可循环滚动,true:可循环,false:不可循环,默认值:true。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
52| alignment<sup>10+</sup>  | [DialogAlignment](ts-methods-alert-dialog-box.md#dialogalignment枚举说明) | 否   | 弹窗在竖直方向上的对齐方式。<br>默认值:DialogAlignment.Default<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
53| offset<sup>10+</sup>     | [Offset](ts-types.md#offset) | 否     | 弹窗相对alignment所在位置的偏移量。<br/>默认值:{&nbsp;dx:&nbsp;0&nbsp;,&nbsp;dy:&nbsp;0&nbsp;}<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
54| 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开始,该接口支持在原子化服务中使用。 |
55| onAccept | (value: [TextPickerResult](#textpickerresult对象说明)) => void | 否 | 点击弹窗中的“确定”按钮时触发该回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
56| onCancel | () => void | 否 | 点击弹窗中的“取消”按钮时触发该回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
57| onChange | (value: [TextPickerResult](#textpickerresult对象说明)) => void | 否 | 滑动弹窗中的选择器后,选项归位至选中项位置时,触发该回调。<br/>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
58| onScrollStop<sup>14+</sup> | (value: [TextPickerResult](#textpickerresult对象说明)) => void | 否 | 滑动弹窗中的选择器的选择列停止触发该回调。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 |
59| backgroundColor<sup>11+</sup> | [ResourceColor](ts-types.md#resourcecolor)  | 否 | 弹窗背板颜色。<br/>默认值:Color.Transparent<br/>**说明:** <br/>当设置了backgroundColor为非透明色时,backgroundBlurStyle需要设置为BlurStyle.NONE,否则颜色显示将不符合预期效果。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
60| 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开始,该接口支持在原子化服务中使用。 |
61| 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开始,该接口支持在原子化服务中使用。 |
62| onDidDisappear<sup>12+</sup> | () => void | 否 | 弹窗消失时的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onAccept/onCancel/onChange/onScrollStop)>>onWillDisappear>>onDidDisappear。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 |
63| onWillAppear<sup>12+</sup> | () => void | 否 | 弹窗显示动效前的事件回调。<br />**说明:**<br />1.正常时序依次为:onWillAppear>>onDidAppear>>(onAccept/onCancel/onChange/onScrollStop)>>onWillDisappear>>onDidDisappear。<br />2.在onWillAppear内设置改变弹窗显示效果的回调事件,二次弹出生效。<br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
64| 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开始,该接口支持在原子化服务中使用。|
65| shadow<sup>12+</sup>              | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions对象说明)&nbsp;\|&nbsp;[ShadowStyle](ts-universal-attributes-image-effect.md#shadowstyle10枚举说明) | 否   | 设置弹窗背板的阴影。<br />当设备为2in1时,默认场景下获焦阴影值为ShadowStyle.OUTER_FLOATING_MD,失焦为ShadowStyle.OUTER_FLOATING_SM <br/>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。|
66| enableHoverMode<sup>14+</sup>     | boolean | 否   | 是否响应悬停态。<br />默认值:false,默认不响应。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。|
67| hoverModeArea<sup>14+</sup>       | [HoverModeAreaType](ts-appendix-enums.md#hovermodeareatype14) | 否   | 悬停态下弹窗默认展示区域。<br />默认值:HoverModeAreaType.BOTTOM_SCREEN。<br/>**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。|
68| disableTextStyleAnimation<sup>15+</sup>   |  boolean | 否   |  设置滑动过程中是否有文本样式变化动效。<br/>true:无文本样式变化动效,false:有文本样式变化动效。<br/>默认值:false |
69| defaultTextStyle<sup>15+</sup>   |  [TextPickerTextStyle](ts-basic-components-textpicker.md#textpickertextstyle15类型说明) | 否   |  设置关闭滑动过程中文本样式变化动效时的各个选项文本的样式,仅当disableTextStyleAnimation为true时生效。<br/>默认值:与[Text](ts-basic-components-text.md)组件默认值相同。 |
70| onEnterSelectedArea<sup>18+</sup>   |  Callback\<[TextPickerResult](#textpickerresult对象说明)> | 否   |  滑动过程中,选项进入分割线区域内,触发该回调。与onChange事件的差别在于,该事件的触发时机早于onChange事件,当当前滑动列滑动距离超过选中项高度的一半时,选项此时已经进入分割线区域内,会触发该事件。<br />**说明:**<br />在多列联动场景中,不建议使用该回调,由于该回调标识的是滑动过程中选项进入分割线区域内的节点,而跟随变化的选项并不涉及滑动,因此,回调的返回值中,仅当前滑动列的值会正常变化,其余未滑动列的值保持不变。<br/>**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。 |
71| enableHapticFeedback<sup>18+</sup> | boolean | 否   | 设置是否开启触控反馈。<br />默认值:true,true表示开启触控反馈,false表示不开启触控反馈。<br />**原子化服务API**: 从API version 18开始,该接口支持在原子化服务中使用。<br />**说明**:<br />开启触控反馈时,需要在工程的module.json5中配置requestPermissions字段开启振动权限,配置如下:<br />"requestPermissions": [{"name": "ohos.permission.VIBRATE"}]|
72
73## TextPickerResult对象说明
74
75**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
76
77**系统能力:** SystemCapability.ArkUI.ArkUI.Full
78
79| 名称 | 类型 | 只读 | 可选 | 说明 |
80| -------- | -------- | -------- | -------- | -------- |
81| value | string \| string []<sup>10+</sup> | 否 | 否 | 选中项的文本内容。<br/>**说明**:当显示文本或图片加文本列表时,value值为选中项中的文本值。(文本选择器显示多列时,value为数组类型。)<br/>当显示图片列表时,value值为空。<br/>value值不支持包含转义字符'\\'。 |
82| index | number \| number []<sup>10+</sup> | 否 | 否 | 选中项在选择范围数组中的索引值,索引从0开始。(文本选择器显示多列时,index为数组类型。) |
83
84## 示例
85
86>  **说明:**
87>
88> 推荐通过使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showTextPickerDialog](../js-apis-arkui-UIContext.md#showtextpickerdialog)来明确UI的执行上下文。
89
90### 示例1(弹出文本选择弹窗)
91
92该示例通过点击按钮弹出文本选择弹窗。
93
94```ts
95// xxx.ets
96@Entry
97@Component
98struct TextPickerDialogExample {
99  private select: number | number[] = 0
100  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
101  @State v: string = '';
102
103  build() {
104    Row() {
105      Column() {
106        Button("TextPickerDialog:" + this.v)
107          .margin(20)
108          .onClick(() => {
109            this.getUIContext().showTextPickerDialog({
110              range: this.fruits,
111              selected: this.select,
112              value: this.v,
113              defaultPickerItemHeight: 40,
114              onAccept: (value: TextPickerResult) => {
115                // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
116                this.select = value.index
117                console.log(this.select + '')
118                // 点击确定后,被选到的文本数据展示到页面
119                this.v = value.value as string
120                console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
121              },
122              onCancel: () => {
123                console.info("TextPickerDialog:onCancel()")
124              },
125              onChange: (value: TextPickerResult) => {
126                console.info("TextPickerDialog:onChange()" + JSON.stringify(value))
127              },
128              onScrollStop: (value: TextPickerResult) => {
129                console.info("TextPickerDialog:onScrollStop()" + JSON.stringify(value))
130              },
131              onDidAppear: () => {
132                console.info("TextPickerDialog:onDidAppear()")
133              },
134              onDidDisappear: () => {
135                console.info("TextPickerDialog:onDidDisappear()")
136              },
137              onWillAppear: () => {
138                console.info("TextPickerDialog:onWillAppear()")
139              },
140              onWillDisappear: () => {
141                console.info("TextPickerDialog:onWillDisappear()")
142              }
143            })
144          })
145      }.width('100%')
146    }.height('100%')
147  }
148}
149```
150
151![TextPickerDialog](figures/TextPickerDialog.gif)
152
153
154### 示例2(自定义样式)
155
156该示例通过配置disappearTextStyle、textStyle、selectedTextStyle、acceptButtonStyle、cancelButtonStyle实现了自定义文本以及按钮样式。
157
158```ts
159// xxx.ets
160@Entry
161@Component
162struct TextPickerDialogExample {
163  private select: number | number[] = 0
164  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
165  @State v: string = '';
166
167  build() {
168    Row() {
169      Column() {
170        Button("TextPickerDialog:" + this.v)
171          .margin(20)
172          .onClick(() => {
173            this.getUIContext().showTextPickerDialog({
174              range: this.fruits,
175              selected: this.select,
176              disappearTextStyle: { color: '#297bec', font: { size: 15, weight: FontWeight.Lighter } },
177              textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal } },
178              selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder } },
179              acceptButtonStyle: {
180                type: ButtonType.Normal,
181                style: ButtonStyleMode.NORMAL,
182                role: ButtonRole.NORMAL,
183                fontColor: 'rgb(81, 81, 216)',
184                fontSize: '26fp',
185                fontWeight: FontWeight.Bolder,
186                fontStyle: FontStyle.Normal,
187                fontFamily: 'sans-serif',
188                backgroundColor: '#A6ACAF',
189                borderRadius: 20
190              },
191              cancelButtonStyle: {
192                type: ButtonType.Normal,
193                style: ButtonStyleMode.NORMAL,
194                role: ButtonRole.NORMAL,
195                fontColor: Color.Blue,
196                fontSize: '16fp',
197                fontWeight: FontWeight.Normal,
198                fontStyle: FontStyle.Italic,
199                fontFamily: 'sans-serif',
200                backgroundColor: '#50182431',
201                borderRadius: 10
202              },
203              onAccept: (value: TextPickerResult) => {
204                // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
205                this.select = value.index
206                console.log(this.select + '')
207                // 点击确定后,被选到的文本数据展示到页面
208                this.v = value.value as string
209                console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
210              },
211              onCancel: () => {
212                console.info("TextPickerDialog:onCancel()")
213              },
214              onChange: (value: TextPickerResult) => {
215                console.info("TextPickerDialog:onChange()" + JSON.stringify(value))
216              },
217              onScrollStop: (value: TextPickerResult) => {
218                console.info("TextPickerDialog:onScrollStop()" + JSON.stringify(value))
219              },
220              onDidAppear: () => {
221                console.info("TextPickerDialog:onDidAppear()")
222              },
223              onDidDisappear: () => {
224                console.info("TextPickerDialog:onDidDisappear()")
225              },
226              onWillAppear: () => {
227                console.info("TextPickerDialog:onWillAppear()")
228              },
229              onWillDisappear: () => {
230                console.info("TextPickerDialog:onWillDisappear()")
231              }
232            })
233          })
234      }.width('100%')
235    }.height('100%')
236  }
237}
238```
239
240![TextPickerDialog](figures/TextPickerDialog_CustomButton.png)
241
242### 示例3(悬停态弹窗)
243
244该示例展示了在折叠屏悬停态下设置dialog布局区域的效果。
245
246```ts
247@Entry
248@Component
249struct TextPickerDialogExample {
250  private select: number | number[] = 0;
251  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5'];
252  @State v: string = '';
253
254  build() {
255    Row() {
256      Column() {
257        Button("TextPickerDialog:" + this.v)
258          .margin(20)
259          .onClick(() => {
260            this.getUIContext().showTextPickerDialog({
261              range: this.fruits,
262              selected: this.select,
263              disappearTextStyle: { color: Color.Red, font: { size: 15, weight: FontWeight.Lighter }},
264              textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal }},
265              selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder }},
266              onAccept: (value: TextPickerResult) => {
267                // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
268                this.select = value.index;
269                console.log(this.select + '');
270                // 点击确定后,被选到的文本数据展示到页面
271                this.v = value.value as string;
272                console.info("TextPickerDialog:onAccept()" + JSON.stringify(value));
273              },
274              onCancel: () => {
275                console.info("TextPickerDialog:onCancel()");
276              },
277              onChange: (value: TextPickerResult) => {
278                console.info("TextPickerDialog:onChange()" + JSON.stringify(value));
279              },
280              onScrollStop: (value: TextPickerResult) => {
281                console.info("TextPickerDialog:onScrollStop()" + JSON.stringify(value));
282              },
283              onDidAppear: () => {
284                console.info("TextPickerDialog:onDidAppear()");
285              },
286              onDidDisappear: () => {
287                console.info("TextPickerDialog:onDidDisappear()");
288              },
289              onWillAppear: () => {
290                console.info("TextPickerDialog:onWillAppear()");
291              },
292              onWillDisappear: () => {
293                console.info("TextPickerDialog:onWillDisappear()");
294              },
295              enableHoverMode: true,
296              hoverModeArea: HoverModeAreaType.TOP_SCREEN
297            })
298          })
299      }.width('100%')
300    }.height('100%')
301  }
302}
303```
304
305![TextPickerDialog](figures/TextPickerDialog_HoverMode.gif)
306
307### 示例4(设置弹窗位置)
308
309该示例通过alignment、offset设置弹窗的位置。
310
311```ts
312// xxx.ets
313@Entry
314@Component
315struct TextPickerDialogExample {
316  private select: number | number[] = 0
317  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
318  @State v: string = '';
319
320  build() {
321    Row() {
322      Column() {
323        Button("TextPickerDialog:" + this.v)
324          .margin(20)
325          .onClick(() => {
326            this.getUIContext().showTextPickerDialog({
327              range: this.fruits,
328              selected: this.select,
329              alignment: DialogAlignment.Center,
330              offset: { dx: 20, dy: 0 },
331              onAccept: (value: TextPickerResult) => {
332                // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
333                this.select = value.index
334                console.log(this.select + '')
335                // 点击确定后,被选到的文本数据展示到页面
336                this.v = value.value as string
337                console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
338              }
339            })
340          })
341      }.width('100%')
342    }.height('100%')
343  }
344}
345```
346
347![TextPickerDialog](figures/TextPickerDialogDemo4.png)
348
349### 示例5(设置遮蔽区)
350
351该示例通过maskRect设置遮蔽区。
352
353```ts
354// xxx.ets
355@Entry
356@Component
357struct TextPickerDialogExample {
358  private select: number | number[] = 0
359  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
360  @State v: string = '';
361
362  build() {
363    Row() {
364      Column() {
365        Button("TextPickerDialog:" + this.v)
366          .margin(20)
367          .onClick(() => {
368            this.getUIContext().showTextPickerDialog({
369              range: this.fruits,
370              selected: this.select,
371              maskRect: {
372                x: 30,
373                y: 60,
374                width: '100%',
375                height: '60%'
376              },
377              onAccept: (value: TextPickerResult) => {
378                // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
379                this.select = value.index
380                console.log(this.select + '')
381                // 点击确定后,被选到的文本数据展示到页面
382                this.v = value.value as string
383                console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
384              }
385            })
386          })
387      }.width('100%')
388    }.height('100%')
389  }
390}
391```
392
393![TextPickerDialog](figures/TextPickerDialogDemo5.png)
394
395### 示例6(设置弹窗背板)
396
397该示例通过backgroundColor、backgroundBlurStyle、shadow设置弹窗背板。
398
399```ts
400// xxx.ets
401@Entry
402@Component
403struct TextPickerDialogExample {
404  private select: number | number[] = 0
405  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
406  @State v: string = '';
407
408  build() {
409    Row() {
410      Column() {
411        Button("TextPickerDialog:" + this.v)
412          .margin(20)
413          .onClick(() => {
414            this.getUIContext().showTextPickerDialog({
415              range: this.fruits,
416              selected: this.select,
417              backgroundColor: 'rgb(204, 226, 251)',
418              backgroundBlurStyle: BlurStyle.NONE,
419              shadow: ShadowStyle.OUTER_FLOATING_SM,
420              onAccept: (value: TextPickerResult) => {
421                // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
422                this.select = value.index
423                console.log(this.select + '')
424                // 点击确定后,被选到的文本数据展示到页面
425                this.v = value.value as string
426                console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
427              }
428            })
429          })
430      }.width('100%')
431    }.height('100%')
432  }
433}
434```
435
436![TextPickerDialog](figures/TextPickerDialogDemo6.png)
437
438
439### 示例7(设置循环滚动)
440
441该示例使用canLoop设置是否循环滚动。
442
443```ts
444// xxx.ets
445@Entry
446@Component
447struct TextPickerDialogExample {
448  private select: number | number[] = 0
449  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
450  @State v: string = '';
451
452  build() {
453    Row() {
454      Column() {
455        Button("TextPickerDialog:" + this.v)
456          .margin(20)
457          .onClick(() => {
458            this.getUIContext().showTextPickerDialog({
459              range: this.fruits,
460              selected: this.select,
461              value: this.v,
462              canLoop: false,
463              onAccept: (value: TextPickerResult) => {
464                // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
465                this.select = value.index
466                console.log(this.select + '')
467                // 点击确定后,被选到的文本数据展示到页面
468                this.v = value.value as string
469                console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
470              }
471            })
472          })
473      }.width('100%')
474    }.height('100%')
475  }
476}
477```
478
479![TextPickerDialog](figures/TextPickerDialogDemo7.gif)
480