1# 气泡提示 (Popup) 2 3 4Popup属性可绑定在组件上显示气泡弹窗提示,设置弹窗内容、交互逻辑和显示状态。主要用于屏幕录制、信息弹出提醒等显示状态。 5 6 7气泡分为两种类型,一种是系统提供的气泡[PopupOptions](../reference/apis-arkui/arkui-ts/ts-universal-attributes-popup.md#popupoptions类型说明),一种是开发者可以自定义的气泡[CustomPopupOptions](../reference/apis-arkui/arkui-ts/ts-universal-attributes-popup.md#custompopupoptions8类型说明)。其中PopupOptions为系统提供的气泡,通过配置primaryButton、secondaryButton来设置带按钮的气泡。CustomPopupOptions通过配置[builder](../quick-start/arkts-builder.md)参数来设置自定义的气泡。 8 9 10## 文本提示气泡 11 12文本提示气泡常用于只展示带有文本的信息提示,不带有任何交互的场景。Popup属性需绑定组件,当bindPopup属性中参数show为true时会弹出气泡提示。 13 14在Button组件上绑定Popup属性,每次点击Button按钮,handlePopup会切换布尔值,当值为true时,触发bindPopup弹出气泡。 15 16```ts 17@Entry 18@Component 19struct PopupExample { 20 @State handlePopup: boolean = false 21 22 build() { 23 Column() { 24 Button('PopupOptions') 25 .onClick(() => { 26 this.handlePopup = !this.handlePopup 27 }) 28 .bindPopup(this.handlePopup, { 29 message: 'This is a popup with PopupOptions', 30 }) 31 }.width('100%').padding({ top: 5 }) 32 } 33} 34``` 35 36 37 38 39## 添加气泡状态变化的事件 40 41通过onStateChange参数为气泡添加状态变化的事件回调,可以判断当前气泡的显示状态。 42 43```ts 44@Entry 45@Component 46struct PopupExample { 47 @State handlePopup: boolean = false 48 49 build() { 50 Column() { 51 Button('PopupOptions') 52 .onClick(() => { 53 this.handlePopup = !this.handlePopup 54 }) 55 .bindPopup(this.handlePopup, { 56 message: 'This is a popup with PopupOptions', 57 onStateChange: (e)=> { // 返回当前的气泡状态 58 if (!e.isVisible) { 59 this.handlePopup = false 60 } 61 } 62 }) 63 }.width('100%').padding({ top: 5 }) 64 } 65} 66``` 67 68 69 70## 带按钮的提示气泡 71 72通过primaryButton、secondaryButton属性为气泡最多设置两个Button按钮,通过此按钮进行简单的交互,开发者可以通过配置action参数来设置想要触发的操作。 73 74```ts 75@Entry 76@Component 77struct PopupExample22 { 78 @State handlePopup: boolean = false 79 80 build() { 81 Column() { 82 Button('PopupOptions').margin({ top: 200 }) 83 .onClick(() => { 84 this.handlePopup = !this.handlePopup 85 }) 86 .bindPopup(this.handlePopup, { 87 message: 'This is a popup with PopupOptions', 88 primaryButton: { 89 value: 'Confirm', 90 action: () => { 91 this.handlePopup = !this.handlePopup 92 console.info('confirm Button click') 93 } 94 }, 95 secondaryButton: { 96 value: 'Cancel', 97 action: () => { 98 this.handlePopup = !this.handlePopup 99 } 100 }, 101 onStateChange: (e) => { 102 if (!e.isVisible) { 103 this.handlePopup = false 104 } 105 } 106 }) 107 }.width('100%').padding({ top: 5 }) 108 } 109} 110``` 111 112 113 114 115 116## 气泡的动画 117 118气泡通过定义transition控制气泡的进场和出场动画效果。 119 120```ts 121// xxx.ets 122@Entry 123@Component 124struct PopupExample { 125 @State handlePopup: boolean = false 126 @State customPopup: boolean = false 127 128 // popup构造器定义弹框内容 129 @Builder popupBuilder() { 130 Row() { 131 Text('Custom Popup with transitionEffect').fontSize(10) 132 }.height(50).padding(5) 133 } 134 135 build() { 136 Flex({ direction: FlexDirection.Column }) { 137 // PopupOptions 类型设置弹框内容 138 Button('PopupOptions') 139 .onClick(() => { 140 this.handlePopup = !this.handlePopup 141 }) 142 .bindPopup(this.handlePopup, { 143 message: 'This is a popup with transitionEffect', 144 placementOnTop: true, 145 showInSubWindow: false, 146 onStateChange: (e) => { 147 if (!e.isVisible) { 148 this.handlePopup = false 149 } 150 }, 151 // 设置弹窗显示动效为透明度动效与平移动效的组合效果,无退出动效 152 transition:TransitionEffect.asymmetric( 153 TransitionEffect.OPACITY.animation({ duration: 1000, curve: Curve.Ease }).combine( 154 TransitionEffect.translate({ x: 50, y: 50 })), 155 TransitionEffect.IDENTITY) 156 }) 157 .position({ x: 100, y: 150 }) 158 159 // CustomPopupOptions 类型设置弹框内容 160 Button('CustomPopupOptions') 161 .onClick(() => { 162 this.customPopup = !this.customPopup 163 }) 164 .bindPopup(this.customPopup, { 165 builder: this.popupBuilder, 166 placement: Placement.Top, 167 showInSubWindow: false, 168 onStateChange: (e) => { 169 if (!e.isVisible) { 170 this.customPopup = false 171 } 172 }, 173 // 设置弹窗显示动效与退出动效为缩放动效 174 transition:TransitionEffect.scale({ x: 1, y: 0 }).animation({ duration: 500, curve: Curve.Ease }) 175 }) 176 .position({ x: 80, y: 300 }) 177 }.width('100%').padding({ top: 5 }) 178 } 179} 180``` 181 182 183 184 185## 自定义气泡 186 187开发者可以使用CustomPopupOptions的builder创建自定义气泡,\@Builder中可以放自定义的内容。除此之外,还可以通过popupColor等参数控制气泡样式。 188 189```ts 190@Entry 191@Component 192struct Index { 193 @State customPopup: boolean = false 194 // popup构造器定义弹框内容 195 @Builder popupBuilder() { 196 Row({ space: 2 }) { 197 Image($r("app.media.icon")).width(24).height(24).margin({ left: 5 }) 198 Text('This is Custom Popup').fontSize(15) 199 }.width(200).height(50).padding(5) 200 } 201 build() { 202 Column() { 203 Button('CustomPopupOptions') 204 .position({x:100,y:200}) 205 .onClick(() => { 206 this.customPopup = !this.customPopup 207 }) 208 .bindPopup(this.customPopup, { 209 builder: this.popupBuilder, // 气泡的内容 210 placement:Placement.Bottom, // 气泡的弹出位置 211 popupColor:Color.Pink, // 气泡的背景色 212 onStateChange: (e) => { 213 if (!e.isVisible) { 214 this.customPopup = false 215 } 216 } 217 }) 218 } 219 .height('100%') 220 } 221} 222``` 223 224 225使用者通过配置placement参数将弹出的气泡放到需要提示的位置。弹窗构造器会触发弹出提示信息,来引导使用者完成操作,也让使用者有更好的UI体验。 226 227 228 229 230