ts-methods-action-sheet.md 3.7 KB
Newer Older
Z
zengyawen 已提交
1
# Action Sheet
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
An action sheet is a dialog box that displays actions a user can take.
Z
zengyawen 已提交
4

E
ester.zhou 已提交
5
>  **NOTE**
E
ester.zhou 已提交
6
>
E
ester.zhou 已提交
7
>  The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
8 9 10


## Required Permissions
Z
zengyawen 已提交
11 12 13

None

Z
zengyawen 已提交
14 15 16

## ActionSheet.show

E
ester.zhou 已提交
17
show(value: { title: string | Resource, message: string | Resource, confirm?: {value: string | Resource, action:() => void}, cancel?:()=>void, sheets: Array<SheetInfo>, autoCancel?:boolean, alignment?: DialogAlignment, offset?: { dx: number | string | Resource; dy: number | string | Resource } })
Z
zengyawen 已提交
18

E
ester.zhou 已提交
19
Defines and shows the action sheet.
Z
zengyawen 已提交
20

E
ester.zhou 已提交
21
**Parameters**
Z
zengyawen 已提交
22

E
ester.zhou 已提交
23 24 25 26 27 28 29 30 31 32
| Name       | Type                   | Mandatory | Description                         |
| ---------- | -------------------------- | ------- | ----------------------------- |
| title      | string \| [Resource](ts-types.md#resource) | Yes    |  Title of the dialog box.|
| message    | string \| [Resource](ts-types.md#resource) | Yes    | Content of the dialog box. |
| autoCancel | boolean                           | No    | Whether to close the dialog box when the overlay is clicked.<br>Default value: **true**|
| confirm    | {<br>value: string \| [Resource](ts-types.md#resource),<br>action: () =&gt; void<br>} | No | Text content of the confirm button and callback upon button clicking.<br>Default value:<br>**value**: button text.<br>**action**: callback upon button clicking.|
| cancel     | () =&gt; void           | No    | Callback invoked when the dialog box is closed after the overlay is clicked.  |
| alignment  | [DialogAlignment](ts-methods-custom-dialog-box.md#dialogalignment) | No    |  Alignment mode of the dialog box in the vertical direction.<br>Default value: **DialogAlignment.Default**|
| offset     | {<br>dx: Length,<br>dy: Length<br>} | No     | Offset of the dialog box relative to the alignment position.{<br>dx: 0,<br>dy: 0<br>} |
| sheets     | Array&lt;SheetInfo&gt; | Yes      | Options in the dialog box. Each option supports the image, text, and callback.|
Z
zengyawen 已提交
33

E
ester.zhou 已提交
34
## SheetInfo parameters
Z
zengyawen 已提交
35

E
ester.zhou 已提交
36 37 38 39 40
| Name| Type                                                    | Mandatory| Description         |
| ------ | ------------------------------------------------------------ | ---- | ----------------- |
| title  | string \| [Resource](ts-types.md#resource) | Yes  | Sheet text.      |
| icon   | string \| [Resource](ts-types.md#resource) | No  | Sheet icon. By default, no icon is displayed.    |
| action | ()=&gt;void                                          | Yes  | Callback when the sheet is selected.|
Z
zengyawen 已提交
41 42


E
ester.zhou 已提交
43 44
## Example

Z
zengyawen 已提交
45

E
ester.zhou 已提交
46 47
```ts
// xxx.ets
Z
zengyawen 已提交
48 49
@Entry
@Component
E
ester.zhou 已提交
50
struct ActionSheetExample {
E
ester.zhou 已提交
51

Z
zengyawen 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Button('Click to Show ActionSheet')
        .onClick(() => {
          ActionSheet.show({
            title: 'ActionSheet title',
            message: 'message',
            confirm: {
              value: 'Confirm button',
              action: () => {
                console.log('Get Alert Dialog handled')
              }
            },
            sheets: [
              {
                title: 'apples',
                action: () => {
                  console.error('apples')
                }
              },
              {
                title: 'bananas',
                action: () => {
                  console.error('bananas')
                }
              },
              {
                title: 'pears',
                action: () => {
                  console.error('pears')
                }
              }
            ]
          })
        })
    }.width('100%')
    .height('100%')
  }
}
```

Z
zengyawen 已提交
93
![en-us_image_0000001212058508](figures/en-us_image_0000001212058508.gif)