当前位置:   article > 正文

【HarmonyOS开发】ArkUI中的自定义弹窗_harmonyos 消息弹窗

harmonyos 消息弹窗

弹窗是一种模态窗口,通常用来展示用户当前需要的或用户必须关注的信息或操作。在弹出框消失之前,用户无法操作其他界面内容。ArkUI 为我们提供了丰富的弹窗功能,弹窗按照功能可以分为以下两类:

  • 确认类:例如警告弹窗 AlertDialog。

  • 选择类:包括文本选择弹窗 TextPickerDialog 、日期滑动选择弹窗 DatePickerDialog、时间滑动选择弹窗 TimePickerDialog 等。

您可以根据业务场景,选择不同类型的弹窗。

参考:OpenHarmony 弹窗文档V4.0

 今天我们主要了解一下自定义弹窗的使用

自定义弹窗

自定义弹窗的使用更加灵活,适用于更多的业务场景,在自定义弹窗中您可以自定义弹窗内容,构建更加丰富的弹窗界面。自定义弹窗的界面可以通过装饰器@CustomDialog 定义的组件来实现,然后结合 CustomDialogController 来控制自定义弹窗的显示和隐藏。

1、定义自定义弹窗

  1. @CustomDialog
  2. struct CustomDialogExample {
  3. // 双向绑定传值
  4. @Prop title: string
  5. @Link inputValue: string
  6. // 弹窗控制器,控制打开/关闭,必须传入,且名称必须为:controller
  7. controller: CustomDialogController
  8. // 弹窗中的按钮事件
  9. cancel: () => void
  10. confirm: () => void
  11. // 弹窗中的内容描述
  12. build() {
  13. Column() {
  14. Text(this.title || "是否修改文本框内容?")
  15. .fontSize(16)
  16. .margin(24)
  17. .textAlign(TextAlign.Start)
  18. .width("100%")
  19. TextInput({
  20. placeholder: '文本输入框',
  21. text: this.inputValue
  22. })
  23. .height(60)
  24. .width('90%')
  25. .onChange((value: string) => {
  26. this.textValue = value
  27. })
  28. Flex({ justifyContent: FlexAlign.SpaceAround }) {
  29. Button('取消')
  30. .onClick(() => {
  31. this.controller.close()
  32. this.cancel()
  33. }).backgroundColor(0xffffff).fontColor(Color.Black)
  34. Button('确定')
  35. .onClick(() => {
  36. this.controller.close()
  37. this.confirm()
  38. }).backgroundColor(0xffffff).fontColor(Color.Red)
  39. }.margin({ bottom: 10 })
  40. }
  41. }
  42. }

2、使用自定义弹窗

  1. @Entry
  2. @Component
  3. struct Index {
  4. @State title: string = '标题'
  5. @State inputValue: string = '文本框父子组件数据双绑'
  6. // 定义自定义弹窗的Controller,传入参数和回调函数
  7. dialogController: CustomDialogController = new CustomDialogController({
  8. builder: CustomDialogExample({
  9. cancel: this.onCancel,
  10. confirm: this.onAccept,
  11. textValue: $textValue,
  12. inputValue: $inputValue
  13. }),
  14. cancel: this.existApp,
  15. autoCancel: true,
  16. alignment: DialogAlignment.Bottom,
  17. offset: { dx: 0, dy: -20 },
  18. gridCount: 4,
  19. customStyle: false
  20. })
  21. aboutToDisappear() {
  22. this.dialogController = undefined // 将dialogController置空
  23. }
  24. onCancel() {
  25. console.info('点击取消按钮', this.inputValue)
  26. }
  27. onAccept() {
  28. console.info('点击确认按钮', this.inputValue)
  29. }
  30. build() {
  31. Column() {
  32. Button('打开自定义弹窗')
  33. .width('60%')
  34. .margin({top:320})
  35. .zIndex(999)
  36. .onClick(()=>{
  37. if (this.dialogController != undefined) {
  38. this.dialogController.open()
  39. }
  40. })
  41. }
  42. .height('100%')
  43. .width('100%')
  44. }

3、一个完整的示例(常用网站选择)

  1. export interface HobbyBean {
  2. label: string;
  3. isChecked: boolean;
  4. }
  5. export type DataItemType = { value: string }
  6. @Extend(Button) function dialogButtonStyle() {
  7. .fontSize(16)
  8. .fontColor("#007DFF")
  9. .layoutWeight(1)
  10. .backgroundColor(Color.White)
  11. .width(500)
  12. .height(40)
  13. }
  14. @CustomDialog
  15. struct CustomDialogWidget {
  16. @State hobbyBeans: HobbyBean[] = [];
  17. @Prop title:string;
  18. @Prop hobbyResult: Array<DataItemType>;
  19. @Link hobbies: string;
  20. private controller: CustomDialogController;
  21. setHobbiesValue(hobbyBeans: HobbyBean[]) {
  22. let hobbiesText: string = '';
  23. hobbiesText = hobbyBeans.filter((isCheckItem: HobbyBean) =>
  24. isCheckItem?.isChecked)
  25. .map((checkedItem: HobbyBean) => {
  26. return checkedItem.label;
  27. }).join(',');
  28. this.hobbies = hobbiesText;
  29. }
  30. aboutToAppear() {
  31. // let context: Context = getContext(this);
  32. // let manager = context.resourceManager;
  33. // manager.getStringArrayValue($r('app.strarray.hobbies_data'), (error, hobbyResult) => {
  34. // });
  35. this.hobbyResult.forEach(item => {
  36. const hobbyBean = {
  37. label: item.value,
  38. isChecked: this.hobbies.includes(item.value)
  39. }
  40. this.hobbyBeans.push(hobbyBean);
  41. });
  42. }
  43. build() {
  44. Column() {
  45. Text(this.title || "兴趣爱好")
  46. .fontWeight(FontWeight.Bold)
  47. .alignSelf(ItemAlign.Start)
  48. .margin({ left: 24, bottom: 12 })
  49. List() {
  50. ForEach(this.hobbyBeans, (itemHobby: HobbyBean) => {
  51. ListItem() {
  52. Row() {
  53. Text(itemHobby.label)
  54. .fontSize(16)
  55. .fontColor("#182431")
  56. .layoutWeight(1)
  57. .textAlign(TextAlign.Start)
  58. .fontWeight(500)
  59. .margin({ left: 24 })
  60. Toggle({ type: ToggleType.Checkbox, isOn: itemHobby.isChecked })
  61. .margin({
  62. right: 24
  63. })
  64. .onChange((isCheck) => {
  65. itemHobby.isChecked = isCheck;
  66. })
  67. }
  68. }
  69. .height(36)
  70. }, itemHobby => itemHobby.label)
  71. }
  72. .margin({
  73. top: 6,
  74. bottom: 8
  75. })
  76. .divider({
  77. strokeWidth: 0.5,
  78. color: "#0D182431"
  79. })
  80. .listDirection(Axis.Vertical)
  81. .edgeEffect(EdgeEffect.None)
  82. .width("100%")
  83. // .height(248)
  84. Row({
  85. space: 20
  86. }) {
  87. Button("关闭")
  88. .dialogButtonStyle()
  89. .onClick(() => {
  90. this.controller.close();
  91. })
  92. Blank()
  93. .backgroundColor("#F2F2F2")
  94. .width(1)
  95. .opacity(1)
  96. .height(25)
  97. Button("保存")
  98. .dialogButtonStyle()
  99. .onClick(() => {
  100. this.setHobbiesValue(this.hobbyBeans);
  101. this.controller.close();
  102. })
  103. }
  104. }
  105. .width("93.3%")
  106. .padding({
  107. top: 14,
  108. bottom: 16
  109. })
  110. .borderRadius(32)
  111. .backgroundColor(Color.White)
  112. }
  113. }
  114. @Entry
  115. @Component
  116. struct HomePage {
  117. @State hobbies: string = '';
  118. @State hobbyResult: Array<DataItemType> = [
  119. {
  120. "value": "FaceBook"
  121. },
  122. {
  123. "value": "Google"
  124. },
  125. {
  126. "value": "Instagram"
  127. },
  128. {
  129. "value": "Twitter"
  130. },
  131. {
  132. "value": "Linkedin"
  133. }
  134. ]
  135. private title: string = '常用网站'
  136. customDialogController: CustomDialogController = new CustomDialogController({
  137. builder: CustomDialogWidget({
  138. hobbies: $hobbies,
  139. hobbyResult: this.hobbyResult,
  140. title: this.title
  141. }),
  142. alignment: DialogAlignment.Bottom,
  143. customStyle: true,
  144. offset: { dx: 0,dy: -20 }
  145. });
  146. build() {
  147. Column() {
  148. Button('打开自定义弹窗')
  149. .width('60%')
  150. .margin({top: 50})
  151. .zIndex(999)
  152. .onClick(()=>{
  153. if (this.customDialogController != undefined) {
  154. this.customDialogController.open()
  155. }
  156. })
  157. Text(this.hobbies).fontSize(16).padding(24)
  158. }
  159. .width('100%')
  160. }
  161. }

参考:https://gitee.com/harmonyos/codelabs/tree/master/MultipleDialog

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

闽ICP备14008679号