【HarmonyOS开发】弹窗交互(promptAction )

实现效果

  • 点击按钮实现不同方式的弹窗
  • showToast
  • showDialog
  • showActionMenu

代码实现

1.引入'@ohos.promptAction'

typescript 复制代码
import promptAction from '@ohos.promptAction';

2.通过promptAction 实现系统既定的弹窗

typescript 复制代码
import promptAction from '@ohos.promptAction';

@Entry
@Component
struct Show_Page {
  @State message: string = 'Hello World';

  showToast() {
    promptAction.showToast({
      message: "登录成功", //显示内容
      duration: 2000, //显示持续时间
      bottom: 400//设置显示的距离底部位置
    })
  }

  showDialog() {
    promptAction.showDialog({
      title: "提示",
      message: "您确定要删除嘛?",
      buttons: [
        {
          text: "取消",
          color: "#000"
        },
        {
          text: "确定",
          color: "#000"
        }
      ]

    }).then((data) => {
      console.log(data.index.toString());
    })
  }

  showActionMenu() {
    promptAction.showActionMenu({
      title: "选择字体",
      buttons: [
        {
          text: "测试1",
          color: "#ccc"
        },
        {
          text: "测试2",
          color: "#ccc"
        },
        {
          text: "测试3",
          color: "#ccc"
        },
        {
          text: "测试4",
          color: "#ccc"
        },
        {
          text: "测试5",
          color: "#ccc"
        }
      ]
    }).then((data) => {
      console.log(data.index.toString());
    })
  }

  build() {
    Column() {
      Button() {
        Text("ShowToast").fontColor(Color.White).fontSize(18)
      }
      .width("90%")
      .height(40)
      .margin({ top: 40 })
      .onClick(() => {
        this.showToast();
      })

      Button() {
        Text("ShowDialog").fontColor(Color.White).fontSize(18)
      }
      .width("90%")
      .height(40)
      .margin({ top: 40 })
      .onClick(() => {
        this.showDialog();
      })

      Button() {
        Text("ShowActionMenu").fontColor(Color.White).fontSize(18)
      }
      .width("90%")
      .height(40)
      .margin({ top: 40 })
      .onClick(() => {
        this.showActionMenu();
      })
    }
    .height('100%')
    .width('100%')
  }
}
相关推荐
n***63277 分钟前
华为HuaweiCloudStack(一)介绍与架构
服务器·华为·架构
遇到困难睡大觉哈哈16 分钟前
Harmony os Socket 编程实战:TCP / UDP / 多播 / TLS 一锅炖学习笔记
学习·tcp/ip·udp·harmonyos·鸿蒙
遇到困难睡大觉哈哈23 分钟前
Harmony os HTTP 网络访问(Network Kit 版)
网络·http·iphone·harmonyos·鸿蒙
遇到困难睡大觉哈哈30 分钟前
Harmony os ArkTS 卡片生命周期管理:我怎么把 EntryFormAbility 用顺手的
前端·harmonyos·鸿蒙
遇到困难睡大觉哈哈1 小时前
HarmonyOS IPC/RPC 实战:用 ArkTS 跑通 Proxy–Stub 整条链路
qt·rpc·harmonyos·鸿蒙
●VON2 小时前
Flutter 与鸿蒙深度整合:如何实现原生功能调用
flutter·华为·harmonyos
云边有个稻草人4 小时前
以用户为中心:Rokid AR+Unity 打造高效实用的行业虚实交互方案
ar·交互·rokid·rokid ar+unity
食品一少年9 小时前
【Day7-10】开源鸿蒙组件封装实战(3)仿知乎日报的首页轮播图实现
华为·开源·harmonyos
HONG````10 小时前
鸿蒙应用HTTP网络请求实战指南:从基础到进阶优化
网络·http·harmonyos
赵财猫._.10 小时前
HarmonyOS内存优化实战:泄漏检测、大对象管理与垃圾回收策略
华为·wpf·harmonyos