警告彈窗

2024-01-22 18:20 更新

顯示警告彈窗組件,可設(shè)置文本內(nèi)容與響應(yīng)回調(diào)。

說明

從API Version 7開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標單獨標記該內(nèi)容的起始版本。

屬性

名稱參數(shù)類型參數(shù)描述
showAlertDialogParamWithConfirm | AlertDialogParamWithButtons定義并顯示AlertDialog組件。

AlertDialogParamWithConfirm對象說明

參數(shù)名參數(shù)類型必填參數(shù)描述
titleResourceStr彈窗標題。
messageResourceStr彈窗內(nèi)容。
autoCancelboolean

點擊遮障層時,是否關(guān)閉彈窗。

默認值:true

confirm

{

value: ResourceStr,

fontColor?: ResourceColor,

backgroundColor?: ResourceColor,

action: () => void

}

確認按鈕的文本內(nèi)容、文本色、按鈕背景色和點擊回調(diào)。
cancel() => void點擊遮障層關(guān)閉dialog時的回調(diào)。
alignmentDialogAlignment

彈窗在豎直方向上的對齊方式。

默認值:DialogAlignment.Default

offsetOffset彈窗相對alignment所在位置的偏移量。
gridCountnumber彈窗容器寬度所占用柵格數(shù)。

AlertDialogParamWithButtons對象說明

參數(shù)名參數(shù)類型必填參數(shù)描述
titleResourceStr彈窗標題。
messageResourceStr彈窗內(nèi)容。
autoCancelboolean

點擊遮障層時,是否關(guān)閉彈窗。

默認值:true

primaryButton

{

value: ResourceStr,

fontColor?: ResourceColor,

backgroundColor?: ResourceColor,

action: () => void;

}

按鈕的文本內(nèi)容、文本色、按鈕背景色和點擊回調(diào)。
secondaryButton

{

value: ResourceStr,

fontColor?: ResourceColor,

backgroundColor?: ResourceColor,

action: () => void;

}

按鈕的文本內(nèi)容、文本色、按鈕背景色和點擊回調(diào)。
cancel() => void點擊遮障層關(guān)閉dialog時的回調(diào)。
alignmentDialogAlignment

彈窗在豎直方向上的對齊方式。

默認值:DialogAlignment.Default

offsetOffset彈窗相對alignment所在位置的偏移量。
gridCountnumber彈窗容器寬度所占用柵格數(shù)。

DialogAlignment枚舉說明

名稱描述
Top垂直頂部對齊。
Center垂直居中對齊。
Bottom垂直底部對齊。
Default默認對齊。
TopStart8+左上對齊。
TopEnd8+右上對齊。
CenterStart8+左中對齊。
CenterEnd8+右中對齊。
BottomStart8+左下對齊。
BottomEnd8+右下對齊。

示例

  1. // xxx.ets
  2. @Entry
  3. @Component
  4. struct AlertDialogExample {
  5. build() {
  6. Column({ space: 5 }) {
  7. Button('one button dialog')
  8. .onClick(() => {
  9. AlertDialog.show(
  10. {
  11. title: 'title',
  12. message: 'text',
  13. autoCancel: true,
  14. alignment: DialogAlignment.Bottom,
  15. offset: { dx: 0, dy: -20 },
  16. gridCount: 3,
  17. confirm: {
  18. value: 'button',
  19. action: () => {
  20. console.info('Button-clicking callback')
  21. }
  22. },
  23. cancel: () => {
  24. console.info('Closed callbacks')
  25. }
  26. }
  27. )
  28. })
  29. .backgroundColor(0x317aff)
  30. Button('two button dialog')
  31. .onClick(() => {
  32. AlertDialog.show(
  33. {
  34. title: 'title',
  35. message: 'text',
  36. autoCancel: true,
  37. alignment: DialogAlignment.Bottom,
  38. gridCount: 4,
  39. offset: { dx: 0, dy: -20 },
  40. primaryButton: {
  41. value: 'cancel',
  42. action: () => {
  43. console.info('Callback when the first button is clicked')
  44. }
  45. },
  46. secondaryButton: {
  47. value: 'ok',
  48. action: () => {
  49. console.info('Callback when the second button is clicked')
  50. }
  51. },
  52. cancel: () => {
  53. console.info('Closed callbacks')
  54. }
  55. }
  56. )
  57. }).backgroundColor(0x317aff)
  58. }.width('100%').margin({ top: 5 })
  59. }
  60. }

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號