【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%')
  }
}
相关推荐
Easonmax11 小时前
基础入门 React Native 鸿蒙跨平台开发:实现面包屑导航
react native·react.js·harmonyos
Easonmax11 小时前
基础入门 React Native 鸿蒙跨平台开发:冒泡排序动画可视化
react native·react.js·harmonyos
鸣弦artha13 小时前
Flutter框架跨平台鸿蒙开发——GridView数据绑定实战
flutter·华为·harmonyos
zhujian8263715 小时前
三十、【鸿蒙 NEXT】实现吸顶效果
harmonyos·鸿蒙·next·吸顶·吸顶效果·nestedscroll
●VON15 小时前
Flutter for OpenHarmony:基于可选描述字段与上下文感知渲染的 TodoList 任务详情子系统实现
学习·flutter·架构·交互·von
C雨后彩虹15 小时前
优雅子数组
java·数据结构·算法·华为·面试
无穷小亮16 小时前
Flutter框架跨平台鸿蒙开发——育儿知识APP的开发流程
flutter·华为·harmonyos·鸿蒙
●VON18 小时前
Flutter for OpenHarmony:基于可空截止日期与时间语义可视化的 TodoList 时间管理子系统实现
安全·flutter·交互·openharmony·跨平台开发
晚霞的不甘18 小时前
Flutter for OpenHarmony 引力弹球游戏开发全解析:从零构建一个交互式物理小游戏
前端·flutter·云原生·前端框架·游戏引擎·harmonyos·骨骼绑定