当前位置:   article > 正文

全局UI方法-弹窗三-文本滑动选择器弹窗(TextPickDialog)

全局UI方法-弹窗三-文本滑动选择器弹窗(TextPickDialog)

1、描述

        根据指定的选择范围创建文本选择器,展示在弹窗上。

2、接口

        TextPickDialog(options?: TextPickDialogOptions)

3、TextPickDialogOptions

参数名称

参数类型

必填

参数描述

rang

string[] | Resource

设置文本选择器的选择范围。

selected

number

设置选中项的索引值。默认值:0。

valuestring设置选中项的文本内容。当设置了selected参数时,该参数不生效。如果设置的value值不在range范围内,则默认去range第一个元素。
DefaultPickItemHeightnumber | string设置选择器中选项的高度。
onAccept(value : TextPickResult) => void点击弹窗中“确定”按钮时触发该回调。
onCancel() => void点击弹窗中“取消”按钮时触发该回调。
onChange(value : TextPickResult) => void滑动弹窗中的选择器使当前选中项改变时触发该回调。

4、TextPickerResult对象说明

类型

描述

value

string

选中项的文本内容。

index

number

选中项在选择范围数组中的索引值。

5、参数解析

1.当selected和value参数都设置时,且selecetd设置值在范围内时,value参数无效。

2.当不设置selected参数时,并且设置的value值不在range范围内时,显示的是默认的range的第一个元素。

3.根据onAccept和onChange监听或者确定选中项的数据信息。

6、示例

  1. @Entry
  2. @Component
  3. struct TextPickerDialogPage {
  4. @State message: string = '根据指定的选择范围创建文本选择器,展示在弹窗上。'
  5. private defaultSelectIndex: number = 2; // 默认选中项索引值
  6. private developList: string[] = ['Android', 'IOS', 'Java', 'ArkTS', 'ArkUI-X', 'Python', 'TypeScript', 'Vue', 'HarmonyOS']; // 弹窗选项列表
  7. build() {
  8. Row() {
  9. Column() {
  10. Text(this.message)
  11. .fontSize(20)
  12. .fontWeight(FontWeight.Bold)
  13. .width("96%")
  14. .margin({ top: 12 })
  15. Button("TextPickerDialog.show Default")
  16. .width("96%")
  17. .fontSize(20)
  18. .margin({ top: 12 })
  19. .onClick(() => {
  20. TextPickerDialog.show({
  21. range: this.developList,
  22. onAccept: (result: TextPickerResult) => {
  23. console.info("TextPickerDialog Default onAccept result = " + JSON.stringify(result));
  24. },
  25. onCancel: () => {
  26. console.info("TextPickerDialog Default onCancel");
  27. },
  28. onChange: (result: TextPickerResult) => {
  29. console.info("TextPickerDialog Default onChange result = " + JSON.stringify(result));
  30. }
  31. })
  32. })
  33. Button("TextPickerDialog.show Selected")
  34. .width("96%")
  35. .fontSize(20)
  36. .margin({ top: 12 })
  37. .onClick(() => {
  38. TextPickerDialog.show({
  39. range: this.developList,
  40. selected: this.defaultSelectIndex,
  41. value: "Vue", // 当设置selected参数时,value参数会失效
  42. defaultPickerItemHeight: 60,
  43. onAccept: (result: TextPickerResult) => {
  44. console.info("TextPickerDialog Selected onAccept result = " + JSON.stringify(result));
  45. },
  46. onCancel: () => {
  47. console.info("TextPickerDialog Selected onCancel");
  48. },
  49. onChange: (result: TextPickerResult) => {
  50. console.info("TextPickerDialog Selected onChange result = " + JSON.stringify(result));
  51. }
  52. })
  53. })
  54. }
  55. .width('100%')
  56. .height("100%")
  57. }
  58. .height('100%')
  59. }
  60. }

7、效果图

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/331052
推荐阅读
相关标签
  

闽ICP备14008679号