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

实现效果

  • 点击按钮实现不同方式的弹窗
  • showToast
    在这里插入图片描述
  • showDialog
    在这里插入图片描述
  • showActionMenu
    在这里插入图片描述

代码实现

1.引入’@ohos.promptAction’

import promptAction from '@ohos.promptAction';

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

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%')
  }
}

相关推荐

  1. #HarmonyOS:@ohos.promptAction ()---onProgressChange

    2024-07-17 05:32:02       47 阅读
  2. 【鸿蒙HarmonyOS开发笔记】如何自定义

    2024-07-17 05:32:02       88 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 05:32:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 05:32:02       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 05:32:02       58 阅读
  4. Python语言-面向对象

    2024-07-17 05:32:02       69 阅读

热门阅读

  1. volatile的应用

    2024-07-17 05:32:02       23 阅读
  2. MySQL 分库分表

    2024-07-17 05:32:02       26 阅读
  3. C# 文件上传总结

    2024-07-17 05:32:02       24 阅读
  4. Linux的线程

    2024-07-17 05:32:02       22 阅读
  5. Transformer模型在多任务学习中的革新应用

    2024-07-17 05:32:02       26 阅读
  6. 【Qt+opencv】ROI与图像混合

    2024-07-17 05:32:02       25 阅读
  7. Jmeter二次开发Demo

    2024-07-17 05:32:02       24 阅读
  8. ubuntu上通过修改grub启动参数,将串口重定向到sol

    2024-07-17 05:32:02       23 阅读
  9. 【mysql】02在ubuntu24安装并配置mysql

    2024-07-17 05:32:02       27 阅读
  10. 常见云存储服务对比

    2024-07-17 05:32:02       20 阅读
  11. Linux基础 -- 运行安全之ASLR的作用与实现方式

    2024-07-17 05:32:02       29 阅读
  12. Django获取request请求中的参数

    2024-07-17 05:32:02       26 阅读
  13. 上传文件给Ubuntu服务器

    2024-07-17 05:32:02       27 阅读