1# Fixed Style Dialog Box 2 3The fixed-style dialog box uses a fixed layout format, so that you do not need to care about specific display layout details, and only need to enter text content that needs to be displayed, thereby simplifying a use process and improving convenience. 4 5## Constraints 6 7- The dialog box APIs are dependent on the UI execution context and should not be used in an unclear UI context. For details, see [UIContext](../reference/apis-arkui/js-apis-arkui-UIContext.md#uicontext). 8 9- You can call **UIContext** or **getUIContext** to use the APIs described in this document, except **CalendarPickerDialog**, on non-UI pages or in some asynchronous callbacks. Operation not supported. 10 11- For **showActionMenu** and **showDialog** APIs, you must first call [getPromptAction()](../reference/apis-arkui/js-apis-arkui-UIContext.md#getpromptaction) in **UIContext** to obtain the **PromptAction** object, and then use the object to call the corresponding API. 12 13- For **ActionSheet**, **AlertDialog**, and **PickerDialog** APIs, except **CalendarPickerDialog**, you must first call [getUIContext()](../reference/apis-arkui/js-apis-window.md#getuicontext10) in **ohos.window** to obtain the **UIContext** instance, and then use the instance to call the corresponding API. Alternatively, you can obtain a **UIContext** instance through the built-in method [getUIContext()](../reference/apis-arkui/arkui-ts/ts-custom-component-api.md#getuicontext) of the custom component. 14 15The dialog boxes created using **showActionMenu**, **showDialog**, **ActionSheet**, or **AlertDialog** can be changed to a non-modal dialog box by setting its **isModal** attribute to **false**. 16 17## Action Menu (showActionMenu) 18 19The action menu is implemented by obtaining the **PromptAction** object from the [showActionMenu](../reference/apis-arkui/js-apis-arkui-UIContext.md#showactionmenu11) method in **UIContext** and then calling the [showActionMenu](../reference/apis-arkui/js-apis-arkui-UIContext.md#showactionmenu11) API through this object. It can be used in callbacks or in classes you define. 20 21After an action menu is created and displayed, the index of the selected button in the **buttons** array will be returned asynchronously as the response result. 22 23```ts 24import { PromptAction } from '@kit.ArkUI'; 25 26let uiContext = this.getUIContext(); 27let promptAction: PromptAction = uiContext.getPromptAction(); 28try { 29 promptAction.showActionMenu({ 30 title: 'showActionMenu Title Info', 31 buttons: [ 32 { 33 text: 'item1', 34 color: '#666666' 35 }, 36 { 37 text: 'item2', 38 color: '#000000' 39 }, 40 ] 41 }) 42 .then(data => { 43 console.info('showActionMenu success, click button: ' + data.index); 44 }) 45 .catch((err: Error) => { 46 console.error('showActionMenu error: ' + err); 47 }) 48} catch (error) { 49} 50``` 51 52 53 54## Common Dialog Box (showDialog) 55 56The common dialog box is implemented by obtaining the **PromptAction** object from the **getPromptAction** method in **UIContext** and then calling the [showDialog](../reference/apis-arkui/js-apis-arkui-UIContext.md#showdialog) API through this object. It can be used in callbacks or in classes you define. 57 58After a common dialog box is created and displayed, the index of the selected button in the **buttons** array will be returned asynchronously as the response result. 59 60```ts 61// xxx.ets 62import { PromptAction } from '@kit.ArkUI'; 63 64let uiContext = this.getUIContext(); 65let promptAction: PromptAction = uiContext.getPromptAction(); 66try { 67 promptAction.showDialog({ 68 title: 'showDialog Title Info', 69 message: 'Message Info', 70 buttons: [ 71 { 72 text: 'button1', 73 color: '#000000' 74 }, 75 { 76 text: 'button2', 77 color: '#000000' 78 } 79 ] 80 }, (err, data) => { 81 if (err) { 82 console.error('showDialog err: ' + err); 83 return; 84 } 85 console.info('showDialog success callback, click button: ' + data.index); 86 }); 87} catch (error) { 88} 89``` 90 91 92 93## Picker Dialog Box (PickerDialog) 94 95The picker dialog box is typically used to display specific information or options when a user performs certain actions, such as touching a button. 96 97### Lifecycle 98 99The picker dialog box offers lifecycle functions to notify users about the dialog box's lifecycle events. 100For details about the sequence in which the lifecycle events are triggered, see the API reference for each component. 101 102| Name |Type| Description | 103| ----------------- | ------ | ---------------------------- | 104| onDidAppear | () => void | Event callback when the dialog box appears. | 105| onDidDisappear |() => void | Event callback when the dialog box disappears. | 106| onWillAppear | () => void | Event callback when the dialog box is about to appear.| 107| onWillDisappear | () => void | Event callback when the dialog box is about to disappear.| 108 109### Calendar Picker Dialog Box (CalendarPickerDialog) 110 111The calendar picker dialog box provides a calendar view that includes year, month, and weekday information, implemented through the [CalendarPickerDialog](../reference/apis-arkui/arkui-ts/ts-methods-calendarpicker-dialog.md) API. You can call the **show** API to define and display the calendar picker dialog box. 112 113You can also define custom button styles by configuring **acceptButtonStyle** and **cancelButtonStyle**. 114 115```ts 116// xxx.ets 117@Entry 118@Component 119struct CalendarPickerDialogExample { 120 private selectedDate: Date = new Date('2024-04-23') 121 122 build() { 123 Column() { 124 Button("Show CalendarPicker Dialog") 125 .margin(20) 126 .onClick(() => { 127 console.info("CalendarDialog.show") 128 CalendarPickerDialog.show({ 129 selected: this.selectedDate, 130 acceptButtonStyle: { 131 fontColor: '#2787d9', 132 fontSize: '16fp', 133 backgroundColor: '#f7f7f7', 134 borderRadius: 10 135 }, 136 cancelButtonStyle: { 137 fontColor: Color.Red, 138 fontSize: '16fp', 139 backgroundColor: '#f7f7f7', 140 borderRadius: 10 141 }, 142 onAccept: (date: Date)=>{ 143 // Display the last selected date when the dialog box is shown again. 144 this.selectedDate = date 145 } 146 }) 147 }) 148 }.width('100%') 149 } 150} 151``` 152 153 154 155### Date Picker Dialog Box (DatePickerDialog) 156 157The date picker dialog box allows users to select a date from the given range, presenting the date information clearly. 158 159You use the [showDatePickerDialog](../reference/apis-arkui/js-apis-arkui-UIContext.md#showdatepickerdialog) API in **UIContext** to implement a date picker dialog box. 160 161When **lunarSwitch** and **showTime** are set to **true** for the dialog box, it displays a switch for toggling the lunar calendar and time. When the check box is selected, the lunar calendar is shown. When the confirm button is touched, the dialog box returns the currently selected date through **onDateAccept**. To display the last confirmed date when the dialog box is shown again, reassign the value to **selectTime** in the callback. 162 163```ts 164@Entry 165@Component 166struct DatePickerDialogExample { 167 @State selectTime: Date = new Date('2023-12-25T08:30:00'); 168 169 build() { 170 Column() { 171 Button('showDatePickerDialog') 172 .margin(30) 173 .onClick(() => { 174 this.getUIContext().showDatePickerDialog({ 175 start: new Date("2000-1-1"), 176 end: new Date("2100-12-31"), 177 selected: this.selectTime, 178 lunarSwitch: true, 179 showTime: true, 180 onDateAccept: (value: Date) => { 181 this.selectTime = value 182 console.info("DatePickerDialog:onAccept()" + JSON.stringify(value)) 183 }, 184 }) 185 }) 186 }.width('100%').margin({ top: 5 }) 187 } 188} 189 190``` 191 192 193 194In this example, **disappearTextStyle**, **textStyle**, **selectedTextStyle**, **acceptButtonStyle**, and **cancelButtonStyle** are configured to customize the text and button style. 195 196```ts 197@Entry 198@Component 199struct DatePickerDialogExample { 200 @State selectTime: Date = new Date('2023-12-25T08:30:00'); 201 202 build() { 203 Column() { 204 Button('showDatePickerDialog') 205 .margin(30) 206 .onClick(() => { 207 this.getUIContext().showDatePickerDialog({ 208 start: new Date("2000-1-1"), 209 end: new Date("2100-12-31"), 210 selected: this.selectTime, 211 textStyle: { color: '#2787d9', font: { size: '14fp', weight: FontWeight.Normal } }, 212 selectedTextStyle: { color: '#004aaf', font: { size: '18fp', weight: FontWeight.Regular } }, 213 acceptButtonStyle: { 214 fontColor: '#2787d9', 215 fontSize: '16fp', 216 backgroundColor: '#f7f7f7', 217 borderRadius: 10 218 }, 219 cancelButtonStyle: { 220 fontColor: Color.Red, 221 fontSize: '16fp', 222 backgroundColor: '#f7f7f7', 223 borderRadius: 10 224 } 225 }) 226 }) 227 }.width('100%').margin({ top: 5 }) 228 } 229} 230``` 231 232 233 234### Time Picker Dialog Box (TimePickerDialog) 235 236The time picker dialog box allows users to select a time from the 24-hour range, presenting the time information clearly. 237 238You use the [showTimePickerDialog](../reference/apis-arkui/js-apis-arkui-UIContext.md#showtimepickerdialog) API in **UIContext** to implement a time picker dialog box. 239 240In this example, **disappearTextStyle**, **textStyle**, **selectedTextStyle**, **acceptButtonStyle**, and **cancelButtonStyle** are configured to customize the text and button style. 241 242```ts 243// xxx.ets 244 245@Entry 246@Component 247struct TimePickerDialogExample { 248 @State selectTime: Date = new Date('2023-12-25T08:30:00'); 249 250 build() { 251 Column() { 252 Button('showTimePickerDialog') 253 .margin(30) 254 .onClick(() => { 255 this.getUIContext().showTimePickerDialog({ 256 selected: this.selectTime, 257 textStyle: { color: '#2787d9', font: { size: '14fp', weight: FontWeight.Normal } }, 258 selectedTextStyle: { color: '#004aaf', font: { size: '18fp', weight: FontWeight.Regular } }, 259 acceptButtonStyle: { 260 fontColor: '#2787d9', 261 fontSize: '16fp', 262 backgroundColor: '#f7f7f7', 263 borderRadius: 10 264 }, 265 cancelButtonStyle: { 266 fontColor: Color.Red, 267 fontSize: '16fp', 268 backgroundColor: '#f7f7f7', 269 borderRadius: 10 270 } 271 }) 272 }) 273 }.width('100%').margin({ top: 5 }) 274 } 275} 276 277``` 278 279 280 281### Text Picker Dialog Box (TextPickerDialog) 282 283The text picker dialog box allows users to select text from the given range, presenting the text information clearly. 284 285You use the [showTextPickerDialog](../reference/apis-arkui/js-apis-arkui-UIContext.md#showtextpickerdialog) API in **UIContext** to implement a date picker dialog box. 286 287This example demonstrates how to implement a three-column text picker dialog box by setting the **range** parameter type to TextCascadePickerRangeContent[]. When the confirm button is touched, the dialog box returns the currently selected text and index value through the **onAccept** callback. To display the last confirmed text when the dialog box is shown again, reassign the value to **select** in the callback. 288 289```ts 290@Entry 291@Component 292struct TextPickerDialogExample { 293 private fruits: TextCascadePickerRangeContent[] = [ 294 { 295 text: 'Liaoning Province', 296 children: [{ text: 'Shenyang', children: [{ text: 'Shenhe District' }, { text: 'Heping District' }, { text: 'Hunnan District' }] }, 297 { text: 'Dalian', children: [{ text: 'Zhongshan District' }, { text: 'Jinzhou District' }, { text: 'Changhai County' }] }] 298 }, 299 { 300 text: 'Jilin Province', 301 children: [{ text: 'Changchun', children: [{ text: 'Nanguan District' }, { text: 'Kuancheng District' }, { text: 'Chaoyang District' }] }, 302 { text: 'Siping', children: [{ text: 'Tiexi District' }, { text: 'Tiedong District' }, { text: 'Lishu County' }] }] 303 }, 304 { 305 text: 'Heilongjiang Province', 306 children: [{ text: 'Harbin', children: [{ text: 'Daoli District' }, { text: 'Daowai District' }, { text: 'Nangang District' }] }, 307 { text: 'Mudanjiang', children: [{ text: `Dong'an District` }, { text: `Xi'an District` }, { text: 'Aimin District' }] }] 308 } 309 ] 310 private select : number = 0; 311 build() { 312 Column() { 313 Button('showTextPickerDialog') 314 .margin(30) 315 .onClick(() => { 316 this.getUIContext().showTextPickerDialog({ 317 range: this.fruits, 318 selected: this.select, 319 onAccept: (value: TextPickerResult) => { 320 this.select = value.index as number 321 } 322 }) 323 }) 324 }.width('100%').margin({ top: 5 }) 325 } 326} 327``` 328 329 330 331## Action Sheet (ActionSheet) 332 333The action sheet is ideal for presenting multiple action options, especially when the UI only needs to display a list of actions without additional content. 334 335You use the [showActionSheet](../reference/apis-arkui/js-apis-arkui-UIContext.md#showactionsheet) API in UIContext to implement an action sheet. 336 337This example shows how to configure the style and animation effects of the action sheet by setting APIs like **width**, **height**, and **transition**. 338 339```ts 340@Entry 341@Component 342struct showActionSheetExample { 343 build() { 344 Column() { 345 Button('showActionSheet') 346 .margin(30) 347 .onClick(() => { 348 this.getUIContext().showActionSheet({ 349 title: 'ActionSheet title', 350 message: 'Message', 351 autoCancel: false, 352 width: 300, 353 height: 300, 354 cornerRadius: 20, 355 borderWidth: 1, 356 borderStyle: BorderStyle.Solid, 357 borderColor: Color.Blue, 358 backgroundColor: Color.White, 359 transition: TransitionEffect.asymmetric(TransitionEffect.OPACITY 360 .animation({ duration: 3000, curve: Curve.Sharp }) 361 .combine(TransitionEffect.scale({ x: 1.5, y: 1.5 }).animation({ duration: 3000, curve: Curve.Sharp })), 362 TransitionEffect.OPACITY.animation({ duration: 100, curve: Curve.Smooth }) 363 .combine(TransitionEffect.scale({ x: 0.5, y: 0.5 }).animation({ duration: 100, curve: Curve.Smooth }))), 364 confirm: { 365 value: 'OK', 366 action: () => { 367 console.info('Get Alert Dialog handled') 368 } 369 }, 370 alignment: DialogAlignment.Center, 371 sheets: [ 372 { 373 title: 'Apples', 374 action: () => { 375 } 376 }, 377 { 378 title: 'Bananas', 379 action: () => { 380 } 381 }, 382 { 383 title: 'Pears', 384 action: () => { 385 console.log('Pears') 386 } 387 } 388 ] 389 }) 390 }) 391 }.width('100%').margin({ top: 5 }) 392 } 393} 394``` 395 396 397 398## Alert Dialog Box (AlertDialog) 399 400The alert dialog box is used when you need to ask a question or get permission from the user. 401 402* The alert dialog box interrupts the current task. Therefore, only use it to provide necessary information and useful operations. 403* Avoid using alert dialog boxes to provide information only; users do not like to be interrupted by information-rich but non-operable alerts. 404 405You use the [showAlertDialog](../reference/apis-arkui/js-apis-arkui-UIContext.md#showalertdialog) API in UIContext to implement an alert dialog box. 406 407This example shows how to configure the style and animation effects of an alert dialog with multiple buttons by setting APIs like **width**, **height**, and **transition**. 408 409```ts 410@Entry 411@Component 412struct showAlertDialogExample { 413 build() { 414 Column() { 415 Button('showAlertDialog') 416 .margin(30) 417 .onClick(() => { 418 this.getUIContext().showAlertDialog( 419 { 420 title: 'Title', 421 message: 'Text', 422 autoCancel: true, 423 alignment: DialogAlignment.Center, 424 offset: { dx: 0, dy: -20 }, 425 gridCount: 3, 426 transition: TransitionEffect.asymmetric(TransitionEffect.OPACITY 427 .animation({ duration: 3000, curve: Curve.Sharp }) 428 .combine(TransitionEffect.scale({ x: 1.5, y: 1.5 }).animation({ duration: 3000, curve: Curve.Sharp })), 429 TransitionEffect.OPACITY.animation({ duration: 100, curve: Curve.Smooth }) 430 .combine(TransitionEffect.scale({ x: 0.5, y: 0.5 }) 431 .animation({ duration: 100, curve: Curve.Smooth }))), 432 buttons: [{ 433 value: 'Cancel', 434 action: () => { 435 console.info('Callback when the first button is clicked') 436 } 437 }, 438 { 439 enabled: true, 440 defaultFocus: true, 441 style: DialogButtonStyle.HIGHLIGHT, 442 value: 'OK', 443 action: () => { 444 console.info('Callback when the second button is clicked') 445 } 446 }], 447 } 448 ) 449 }) 450 }.width('100%').margin({ top: 5 }) 451 } 452} 453``` 454 455 456