HarmonyOS NEXT鸿蒙开发入门,列表选择弹窗 (ActionSheet)

1. 官方文档指南

  1. 列表选择弹窗 (ActionSheet): https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-methods-action-sheet-V5

2. 描述

定义列表弹窗并弹出

3. 使用说明

ActionSheet.show
static show(value: ActionSheetOptions)
static 修饰的方法,直接通过类名.方法的形式调用

  1. 参数:
参数名类型必填描述
valueActionSheetOptions配置列表选择弹窗的参数
  1. ActionSheetOptions对象说明:
参数名类型必填描述
titleResource or string弹窗标题。
元服务API: 从API version 11开始,该接口支持在元服务中使用。
subtitle10+ResourceStr弹窗副标题。
元服务API: 从API version 11开始,该接口支持在元服务中使用
messageResource orstring弹窗内容。
元服务API: 从API version 11开始,该接口支持在元服务中使用。
autoCancelboolean点击遮障层时,是否关闭弹窗。
默认值:true
值为true时,点击遮障层关闭弹窗,值为false时,点击遮障层不关闭弹窗。
元服务API: 从API version 11开始,该接口支持在元服务中使用。
confirm{
enabled10+?: boolean,
defaultFocus10+?: boolean,
style10+?: DialogButtonStyle,
value: Resourceorstring,
action: () => void
}
确认Button的使能状态、默认焦点、按钮风格、文本内容和点击回调。在弹窗获焦且未进行tab键走焦时,该按钮默认响应Enter键,且多重弹窗可自动获焦连续响应。默认响应Enter键能力在defaultFocus为true时不生效。
enabled:点击Button是否响应,true表示Button可以响应,false表示Button不可以响应。
默认值:true
defaultFocus:设置Button是否是默认焦点,true表示Button是默认焦点,false表示Button不是默认焦点。
默认值:false
style:设置Button的风格样式。
默认值:DialogButtonStyle.DEFAULT
value:Button文本内容。
action: Button选中时的回调。
元服务API: 从API version 11开始,该接口支持在元服务中使用。
cancel() => void点击遮障层关闭dialog时的回调。
元服务API: 从API version 11开始,该接口支持在元服务中使用。
alignmentDialogAlignment弹窗在竖直方向上的对齐方式。
默认值:DialogAlignment.Bottom
元服务API: 从API version 11开始,该接口支持在元服务中使用。
说明:
若在UIExtension中设置showInSubWindow为true, 弹窗将基于UIExtension的宿主窗口对齐。
sheetsArray<SheetInfo>设置选项内容,每个选择项支持设置图片、文本和选中的回调。
元服务API: 从API version 11开始,该接口支持在元服务中使用。

这里我只列举了常用的属性,更多使用方法请查看官方API文档

  1. SheetInfo接口说明
参数名类型必填描述
titlestring or Resource选项的文本内容。
iconstring or Resource选项的图标,默认无图标显示。
action()=>void选项选中的回调。

4.代码实现过程

import { promptAction } from '@kit.ArkUI'

@Entry
@Component
struct ActionSheetPage {
  build() {
    RelativeContainer() {
      Button('列表选择弹窗')
        .id('ActionSheetPageHelloWorld')
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .width('100%')

        .onClick(() => {
          ActionSheet.show({
            title: '温馨提示',
            message: 'ActionSheet列表弹框',
            autoCancel: false,
            alignment: DialogAlignment.Bottom,
            confirm: {
              value: '选择确认',
              action: () => {
                promptAction.showToast({
                  message: '选择确认'
                })
              }
            },
            sheets: [
              {
                title: '北京 \n',
                action: () => {
                  promptAction.showToast({
                    message: '北京'
                  })
                }
              },
              {
                title: '东京\n',
                action: () => {
                  promptAction.showToast({
                    message: '东京'
                  })
                }
              },
              {
                title: '巴黎\n',
                action: () => {
                  promptAction.showToast({
                    message: '巴黎'
                  })
                }
              }
              ,
              {
                title: '伦敦\n',
                action: () => {

                }
              }
              ,
              {
                title: '纽约\n',
                action: () => {

                }
              }
              ,
              {
                title: '悉尼\n',
                action: () => {

                }
              }
              ,
              {
                title: '莫斯科\n',
                action: () => {

                }
              },
              {
                title: '开罗\n',
                action: () => {

                }
              },
              {
                title: '柏林\n',
                action: () => {

                }
              }
              ,
              {
                title: '罗马\n',
                action: () => {

                }
              }
              ,
              {
                title: '雅典\n',
                action: () => {

                }
              }
              ,
              {
                title: '首尔\n',
                action: () => {

                }
              }
              ,
              {
                title: '曼谷\n',
                action: () => {

                }
              }
              ,
              {
                title: '新德里\n',
                action: () => {

                }
              }
              ,
              {
                title: '里约\n',
                action: () => {

                }
              }
              ,
              {
                title: '洛杉矶\n',
                action: () => {

                }
              },
              {
                title: '温哥华\n',
                action: () => {

                }
              },
              {
                title: '慕尼黑\n',
                action: () => {

                }
              }

            ]
          })
        })
    }
    .height('100%')
    .width('100%')
    .padding(10)
  }
}

5.运行效果

请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩宇软件开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值