• 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> 从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 | 否 | 设置选择器中选项的高度。number类型取值范围:[0, +∞],string类型仅支持number类型取值的字符串形式,例如"56"。<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/>默认值:{&nbsp;dx:&nbsp;0&nbsp;,&nbsp;dy:&nbsp;0&nbsp;}<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对象说明)&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开始,该接口支持在原子化服务中使用。|
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| disableTextStyleAnimation<sup>15+</sup>   |  boolean | 否   |  设置滑动过程中是否有文本样式变化动效。<br/>true:无文本样式变化动效,false:有文本样式变化动效。<br/>默认值:false |
63| defaultTextStyle<sup>15+</sup>   |  [TextPickerTextStyle](ts-basic-components-textpicker.md#textpickertextstyle15类型说明) | 否   |  设置关闭滑动过程中文本样式变化动效时的各个选项文本的样式,仅当disableTextStyleAnimation为true时生效。<br/>默认值:与[Text](ts-basic-components-text.md)组件默认值相同。 |
64
65## TextPickerResult对象说明
66
67**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
68
69**系统能力:** SystemCapability.ArkUI.ArkUI.Full
70
71| 名称 | 类型 | 只读 | 可选 | 说明 |
72| -------- | -------- | -------- | -------- | -------- |
73| value | string \| string []<sup>10+</sup> | 否 | 否 | 选中项的文本内容。<br/>**说明**:当显示文本或图片加文本列表时,value值为选中项中的文本值。(文本选择器显示多列时,value为数组类型。)<br/>当显示图片列表时,value值为空。<br/>value值不支持包含转义字符'\\'。 |
74| index | number \| number []<sup>10+</sup> | 否 | 否 | 选中项在选择范围数组中的索引值,索引从0开始。(文本选择器显示多列时,index为数组类型。) |
75
76## 示例
77
78>  **说明:**
79>
80> 推荐通过使用[UIContext](../js-apis-arkui-UIContext.md#uicontext)中的[showTextPickerDialog](../js-apis-arkui-UIContext.md#showtextpickerdialog)来明确UI的执行上下文。
81
82### 示例1(弹出文本选择弹窗)
83
84该示例通过点击按钮弹出文本选择弹窗。
85
86```ts
87// xxx.ets
88@Entry
89@Component
90struct TextPickerDialogExample {
91  private select: number | number[] = 0
92  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
93  @State v:string = '';
94
95  build() {
96    Row() {
97      Column() {
98        Button("TextPickerDialog:" + this.v)
99          .margin(20)
100          .onClick(() => {
101            TextPickerDialog.show({ // 建议使用 this.getUIContext().showTextPickerDialog()接口
102              range: this.fruits,
103              selected: this.select,
104              disappearTextStyle: {color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}},
105              textStyle: {color: Color.Black, font: {size: 20, weight: FontWeight.Normal}},
106              selectedTextStyle: {color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}},
107              onAccept: (value: TextPickerResult) => {
108                // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
109                this.select = value.index
110                console.log(this.select + '')
111                // 点击确定后,被选到的文本数据展示到页面
112                this.v = value.value as string
113                console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
114              },
115              onCancel: () => {
116                console.info("TextPickerDialog:onCancel()")
117              },
118              onChange: (value: TextPickerResult) => {
119                console.info("TextPickerDialog:onChange()" + JSON.stringify(value))
120              },
121              onScrollStop: (value: TextPickerResult) => {
122                console.info("TextPickerDialog:onScrollStop()" + JSON.stringify(value))
123              },
124              onDidAppear: () => {
125                console.info("TextPickerDialog:onDidAppear()")
126              },
127              onDidDisappear: () => {
128                console.info("TextPickerDialog:onDidDisappear()")
129              },
130              onWillAppear: () => {
131                console.info("TextPickerDialog:onWillAppear()")
132              },
133              onWillDisappear: () => {
134                console.info("TextPickerDialog:onWillDisappear()")
135              }
136            })
137          })
138      }.width('100%')
139    }.height('100%')
140  }
141}
142```
143
144![TextPickerDialog](figures/TextPickerDialog.gif)
145
146
147### 示例2(自定义样式)
148
149该示例通过配置disappearTextStyle、textStyle、selectedTextStyle、acceptButtonStyle、cancelButtonStyle实现了自定义文本以及按钮样式。
150
151```ts
152// xxx.ets
153@Entry
154@Component
155struct TextPickerDialogExample {
156  private select: number | number[] = 0
157  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5']
158  @State v:string = '';
159
160  build() {
161    Row() {
162      Column() {
163        Button("TextPickerDialog:" + this.v)
164          .margin(20)
165          .onClick(() => {
166            TextPickerDialog.show({ // 建议使用 this.getUIContext().showTextPickerDialog()接口
167              range: this.fruits,
168              selected: this.select,
169              disappearTextStyle: {color: Color.Red, font: {size: 15, weight: FontWeight.Lighter}},
170              textStyle: {color: Color.Black, font: {size: 20, weight: FontWeight.Normal}},
171              selectedTextStyle: {color: Color.Blue, font: {size: 30, weight: FontWeight.Bolder}},
172              acceptButtonStyle: { type: ButtonType.Normal, style: ButtonStyleMode.NORMAL, role: ButtonRole.NORMAL, fontColor: Color.Red,
173                fontSize: '26fp', fontWeight: FontWeight.Bolder, fontStyle: FontStyle.Normal, fontFamily: 'sans-serif', backgroundColor:'#80834511',
174                borderRadius: 20 },
175              cancelButtonStyle: { type: ButtonType.Normal, style: ButtonStyleMode.NORMAL, role: ButtonRole.NORMAL, fontColor: Color.Blue,
176                fontSize: '16fp', fontWeight: FontWeight.Normal, fontStyle: FontStyle.Italic, fontFamily: 'sans-serif', backgroundColor:'#50182431',
177                borderRadius: 10 },
178              onAccept: (value: TextPickerResult) => {
179                // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
180                this.select = value.index
181                console.log(this.select + '')
182                // 点击确定后,被选到的文本数据展示到页面
183                this.v = value.value as string
184                console.info("TextPickerDialog:onAccept()" + JSON.stringify(value))
185              },
186              onCancel: () => {
187                console.info("TextPickerDialog:onCancel()")
188              },
189              onChange: (value: TextPickerResult) => {
190                console.info("TextPickerDialog:onChange()" + JSON.stringify(value))
191              },
192              onScrollStop: (value: TextPickerResult) => {
193                console.info("TextPickerDialog:onScrollStop()" + JSON.stringify(value))
194              },
195              onDidAppear: () => {
196                console.info("TextPickerDialog:onDidAppear()")
197              },
198              onDidDisappear: () => {
199                console.info("TextPickerDialog:onDidDisappear()")
200              },
201              onWillAppear: () => {
202                console.info("TextPickerDialog:onWillAppear()")
203              },
204              onWillDisappear: () => {
205                console.info("TextPickerDialog:onWillDisappear()")
206              }
207            })
208          })
209      }.width('100%')
210    }.height('100%')
211  }
212}
213```
214
215![TextPickerDialog](figures/TextPickerDialog_CustomButton.png)
216
217### 示例3(悬停态弹窗)
218
219该示例展示了在折叠屏悬停态下设置dialog布局区域的效果。
220
221```ts
222@Entry
223@Component
224struct TextPickerDialogExample {
225  private select: number | number[] = 0;
226  private fruits: string[] = ['apple1', 'orange2', 'peach3', 'grape4', 'banana5'];
227  @State v: string = '';
228
229  build() {
230    Row() {
231      Column() {
232        Button("TextPickerDialog:" + this.v)
233          .margin(20)
234          .onClick(() => {
235            TextPickerDialog.show({ // 建议使用 this.getUIContext().showTextPickerDialog()接口
236              range: this.fruits,
237              selected: this.select,
238              disappearTextStyle: { color: Color.Red, font: { size: 15, weight: FontWeight.Lighter }},
239              textStyle: { color: Color.Black, font: { size: 20, weight: FontWeight.Normal }},
240              selectedTextStyle: { color: Color.Blue, font: { size: 30, weight: FontWeight.Bolder }},
241              onAccept: (value: TextPickerResult) => {
242                // 设置select为按下确定按钮时候的选中项index,这样当弹窗再次弹出时显示选中的是上一次确定的选项
243                this.select = value.index;
244                console.log(this.select + '');
245                // 点击确定后,被选到的文本数据展示到页面
246                this.v = value.value as string;
247                console.info("TextPickerDialog:onAccept()" + JSON.stringify(value));
248              },
249              onCancel: () => {
250                console.info("TextPickerDialog:onCancel()");
251              },
252              onChange: (value: TextPickerResult) => {
253                console.info("TextPickerDialog:onChange()" + JSON.stringify(value));
254              },
255              onScrollStop: (value: TextPickerResult) => {
256                console.info("TextPickerDialog:onScrollStop()" + JSON.stringify(value))
257              },
258              onDidAppear: () => {
259                console.info("TextPickerDialog:onDidAppear()");
260              },
261              onDidDisappear: () => {
262                console.info("TextPickerDialog:onDidDisappear()");
263              },
264              onWillAppear: () => {
265                console.info("TextPickerDialog:onWillAppear()");
266              },
267              onWillDisappear: () => {
268                console.info("TextPickerDialog:onWillDisappear()");
269              },
270              enableHoverMode: true,
271              hoverModeArea: HoverModeAreaType.TOP_SCREEN
272            })
273          })
274      }.width('100%')
275    }.height('100%')
276  }
277}
278```
279
280![TextPickerDialog](figures/TextPickerDialog_HoverMode.gif)
281