openharmony 鸿蒙 js-apis-system-prompt

2025-06-12 浏览 (1)

@system.prompt (Prompt)

The Prompt module provides APIs for creating and showing toasts, dialog boxes, and action menus.

NOTE

  • The APIs of this module are deprecated since API version 8. You are advised to use @ohos.promptAction instead.

  • The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import prompt from '@system.prompt';

prompt.showToast

showToast(options: ShowToastOptions): void

Shows the toast.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
optionsShowToastOptionsYesOptions for showing the toast.

Example

import prompt from '@system.prompt';
class A{
  showToast() {
    prompt.showToast({
      message: 'Message Info',
      duration: 2000
    });
  }
}
export default new A()

prompt.showDialog

showDialog(options: ShowDialogOptions): void

Shows the dialog box.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
optionsShowDialogOptionsYesOptions for showing the dialog box.

Example

import prompt from '@system.prompt';
class B{
  showDialog() {
    prompt.showDialog({
      title: 'Title Info',
      message: 'Message Info',
      buttons: [
        {
          text: 'button',
          color: '#666666'
        },
      ],
      success: (data)=> {
        console.log('dialog success callback, click button : ' + data.index);
      },
      cancel: ()=> {
        console.log('dialog cancel callback');
      },
    });
  }
}
export default new B()

prompt.showActionMenu6+

showActionMenu(options: ShowActionMenuOptions): void

Shows the action menu.

System capability: SystemCapability.ArkUI.ArkUI.Full

Parameters

NameTypeMandatoryDescription
optionsShowActionMenuOptionsYesOptions for showing the action menu.

Example

import prompt from '@system.prompt';
class C{
  showActionMenu() {
    prompt.showActionMenu({
      title: 'Title Info',
      buttons: [
        {
          text: 'item1',
          color: '#666666'
        },
        {
          text: 'item2',
          color: '#000000'
        },
      ],
      success: (tapIndex)=> {
        console.log('dialog success callback, click button : ' + data.tapIndex);
      },
      fail: (errMsg)=> {
        console.log('dialog fail callback' + errMsg);
      },
    });
  }
}
export default new C()

ShowToastOptions

Describes the options for showing the toast.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
messagestringYesText to display.
durationnumberNoDuration that the toast will remain on the screen. The default value is 1500 ms. The recommended value range is 1500 ms to 10000 ms. If a value less than 1500 ms is set, the default value is used.
bottom5+string|numberNoDistance between the toast border and the bottom of the screen.

Button

Defines the prompt information of a button.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
textstringYesText displayed on the button.
colorstringYesColor of the button.

ShowDialogSuccessResponse

Defines the dialog box response result.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
indexnumberYesData index.

ShowDialogOptions

Describes the options for showing the dialog box.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
titlestringNoTitle of the text to display.
messagestringNoText body.
buttons[Button, Button?, Button?]NoArray of buttons in the dialog box. The array structure is {text:'button', color: '#666666'}. One to six buttons are supported. If there are more than six buttons, extra buttons will not be displayed.
success(data: ShowDialogSuccessResponse) => voidNoCallback upon success.
cancel(data: string, code: string) => voidNoCallback upon failure.
complete(data: string) => voidNoCalled when the API call is complete.

ShowActionMenuOptions6+

Describes the options for showing the action menu.

System capability: SystemCapability.ArkUI.ArkUI.Full

NameTypeMandatoryDescription
titlestringNoTitle of the text to display.
buttons[Button, Button?, Button?, Button?, Button?, Button?]YesArray of buttons in the dialog box. The array structure is {text:'button', color: '#666666'}. One to six buttons are supported.
success(tapIndex: number, errMsg: string) => voidNoInvoked when a dialog box is displayed.
fail(errMsg: string) => voidNoCallback upon failure.
complete(data: string) => voidNoInvoked when a dialog box is closed.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkUI

harmony 鸿蒙ARKUI_TextPickerCascadeRangeContent

harmony 鸿蒙ARKUI_TextPickerRangeContent

harmony 鸿蒙ArkUI_AnimateCompleteCallback

harmony 鸿蒙ArkUI_AttributeItem

harmony 鸿蒙ArkUI_ColorStop

harmony 鸿蒙ArkUI_ContextCallback

harmony 鸿蒙ArkUI_EventModule

harmony 鸿蒙ArkUI_ExpectedFrameRateRange

harmony 鸿蒙ArkUI_IntOffset

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/Mpj5eD