【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%')
  }
}
相关推荐
安卓开发者43 分钟前
鸿蒙剪贴板服务的新特性
华为·harmonyos
mit6.8241 小时前
[GazeTracking] 摄像头交互与显示 | OpenCV
人工智能·opencv·交互
bst@微胖子11 小时前
鸿蒙实现滴滴出行项目之线路规划图
华为·harmonyos
我是华为OD~HR~栗栗呀12 小时前
23届考研-Java面经(华为OD)
java·c++·python·华为od·华为·面试
路很长OoO13 小时前
Flutter 插件开发实战:桥接原生 SDK
前端·flutter·harmonyos
2501_9197490315 小时前
鸿蒙:使用Rating组件实现五角星打分评价
华为·harmonyos
2501_9197490320 小时前
鸿蒙:实现滑动选择日期操作
华为·harmonyos
程序员潘Sir1 天前
鸿蒙应用开发从入门到实战(十九):样式复用案例
harmonyos·鸿蒙
std860211 天前
华为 Mate80 要来了,或搭载最新麒麟芯片
华为