彈窗

2024-01-23 13:11 更新

創(chuàng)建并顯示文本提示框、對話框和操作菜單。

說明

本模塊首批接口從API version 9開始支持。后續(xù)版本的新增接口,采用上角標單獨標記接口的起始版本。

該模塊不支持在UIAbility的文件聲明處使用,即不能在UIAbility的生命周期中調(diào)用,需要在創(chuàng)建組件實例后使用。

導入模塊

  1. import promptAction from '@ohos.promptAction'

promptAction.showToast

showToast(options: ShowToastOptions): void

創(chuàng)建并顯示文本提示框。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

參數(shù):

參數(shù)名

類型

必填

說明

options

ShowToastOptions

文本彈窗選項。

錯誤碼:

以下錯誤碼的詳細介紹請參見ohos.promptAction(彈窗)錯誤碼。

錯誤碼ID

錯誤信息

100001

if UI execution context not found.

示例:

  1. try {
  2. promptAction.showToast({
  3. message: 'Message Info',
  4. duration: 2000,
  5. });
  6. } catch (error) {
  7. console.error(`showToast args error code is ${error.code}, message is ${error.message}`);
  8. };

ShowToastOptions

文本提示框的選項。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full。

名稱

類型

必填

說明

message

string| Resource9+

顯示的文本信息。默認字體為'Harmony Sans',不支持設(shè)置其他字體。

duration

number

默認值1500ms,取值區(qū)間:1500ms-10000ms。若小于1500ms則取默認值,若大于10000ms則取上限值10000ms。

bottom

string| number

設(shè)置彈窗邊框距離屏幕底部的位置。

promptAction.showDialog

showDialog(options: ShowDialogOptions): Promise<ShowDialogSuccessResponse>

創(chuàng)建并顯示對話框,對話框響應后同步返回結(jié)果。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

參數(shù):

參數(shù)名

類型

必填

說明

options

ShowDialogOptions

對話框選項。

返回值:

類型

說明

Promise<ShowDialogSuccessResponse>

對話框響應結(jié)果。

錯誤碼:

以下錯誤碼的詳細介紹請參見ohos.promptAction(彈窗)錯誤碼。

錯誤碼ID

錯誤信息

100001

if UI execution context not found.

示例:

  1. try {
  2. promptAction.showDialog({
  3. title: 'Title Info',
  4. message: 'Message Info',
  5. buttons: [
  6. {
  7. text: 'button1',
  8. color: '#000000',
  9. },
  10. {
  11. text: 'button2',
  12. color: '#000000',
  13. }
  14. ],
  15. })
  16. .then(data => {
  17. console.info('showDialog success, click button: ' + data.index);
  18. })
  19. .catch(err => {
  20. console.info('showDialog error: ' + err);
  21. })
  22. } catch (error) {
  23. console.error(`showDialog args error code is ${error.code}, message is ${error.message}`);
  24. };

promptAction.showDialog

showDialog(options: ShowDialogOptions, callback: AsyncCallback<ShowDialogSuccessResponse>):void

創(chuàng)建并顯示對話框,對話框響應結(jié)果異步返回。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

參數(shù):

參數(shù)名

類型

必填

說明

options

ShowDialogOptions

頁面顯示對話框信息描述。

callback

AsyncCallback<ShowDialogSuccessResponse>

對話框響應結(jié)果回調(diào)。

錯誤碼:

以下錯誤碼的詳細介紹請參見ohos.promptAction(彈窗)錯誤碼。

錯誤碼ID

錯誤信息

100001

if UI execution context not found.

示例:

  1. try {
  2. promptAction.showDialog({
  3. title: 'showDialog Title Info',
  4. message: 'Message Info',
  5. buttons: [
  6. {
  7. text: 'button1',
  8. color: '#000000',
  9. },
  10. {
  11. text: 'button2',
  12. color: '#000000',
  13. }
  14. ]
  15. }, (err, data) => {
  16. if (err) {
  17. console.info('showDialog err: ' + err);
  18. return;
  19. }
  20. console.info('showDialog success callback, click button: ' + data.index);
  21. });
  22. } catch (error) {
  23. console.error(`showDialog args error code is ${error.code}, message is ${error.message}`);
  24. };

ShowDialogOptions

對話框的選項。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

名稱

類型

必填

說明

title

string| Resource9+

標題文本。

message

string| Resource9+

內(nèi)容文本。

buttons

[Button,Button?,Button?]

對話框中按鈕的數(shù)組,結(jié)構(gòu)為:{text:'button', color: '#666666'},支持1-3個按鈕。其中第一個為positiveButton;第二個為negativeButton;第三個為neutralButton。

ShowDialogSuccessResponse

對話框的響應結(jié)果。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

名稱

類型

必填

說明

index

number

選中按鈕在buttons數(shù)組中的索引。

promptAction.showActionMenu

showActionMenu(options: ActionMenuOptions, callback: AsyncCallback<ActionMenuSuccessResponse>):void

創(chuàng)建并顯示操作菜單,菜單響應結(jié)果異步返回。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full。

參數(shù):

參數(shù)名

類型

必填

說明

options

ActionMenuOptions

操作菜單選項。

callback

AsyncCallback<ActionMenuSuccessResponse>

菜單響應結(jié)果回調(diào)。

錯誤碼:

以下錯誤碼的詳細介紹請參見ohos.promptAction(彈窗)錯誤碼。

錯誤碼ID

錯誤信息

100001

if UI execution context not found.

示例:

  1. try {
  2. promptAction.showActionMenu({
  3. title: 'Title Info',
  4. buttons: [
  5. {
  6. text: 'item1',
  7. color: '#666666',
  8. },
  9. {
  10. text: 'item2',
  11. color: '#000000',
  12. },
  13. ]
  14. }, (err, data) => {
  15. if (err) {
  16. console.info('showActionMenu err: ' + err);
  17. return;
  18. }
  19. console.info('showActionMenu success callback, click button: ' + data.index);
  20. })
  21. } catch (error) {
  22. console.error(`showActionMenu args error code is ${error.code}, message is ${error.message}`);
  23. };

promptAction.showActionMenu

showActionMenu(options: ActionMenuOptions): Promise<ActionMenuSuccessResponse>

創(chuàng)建并顯示操作菜單,菜單響應后同步返回結(jié)果。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

參數(shù):

參數(shù)名

類型

必填

說明

options

ActionMenuOptions

操作菜單選項。

返回值:

類型

說明

Promise<ActionMenuSuccessResponse>

菜單響應結(jié)果。

錯誤碼:

以下錯誤碼的詳細介紹請參見ohos.promptAction(彈窗)錯誤碼。

錯誤碼ID

錯誤信息

100001

if UI execution context not found.

示例:

  1. try {
  2. promptAction.showActionMenu({
  3. title: 'showActionMenu Title Info',
  4. buttons: [
  5. {
  6. text: 'item1',
  7. color: '#666666',
  8. },
  9. {
  10. text: 'item2',
  11. color: '#000000',
  12. },
  13. ]
  14. })
  15. .then(data => {
  16. console.info('showActionMenu success, click button: ' + data.index);
  17. })
  18. .catch(err => {
  19. console.info('showActionMenu error: ' + err);
  20. })
  21. } catch (error) {
  22. console.error(`showActionMenu args error code is ${error.code}, message is ${error.message}`);
  23. };

ActionMenuOptions

操作菜單的選項。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full。

名稱

類型

必填

說明

title

string| Resource9+

標題文本。

buttons

[Button,Button?,Button?,Button?,Button?,Button?]

菜單中菜單項按鈕的數(shù)組,結(jié)構(gòu)為:{text:'button', color: '#666666'},支持1-6個按鈕。大于6個按鈕時彈窗不顯示。

ActionMenuSuccessResponse

操作菜單的響應結(jié)果。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

名稱

類型

必填

說明

index

number

選中按鈕在buttons數(shù)組中的索引,從0開始。

Button

菜單中的菜單項按鈕。

系統(tǒng)能力: SystemCapability.ArkUI.ArkUI.Full

名稱

類型

必填

說明

text

string| Resource9+

按鈕文本內(nèi)容。

color

string| Resource9+

按鈕文本顏色。

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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號