华为鸿蒙应用--Toast工具(鸿蒙工具)-ArkTs

0、代码

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

export enum Gravity {
  TOP = 10000,
  CENTER = 10001,
  BOTTOM = 10002,
}

function bottomF(bottom?: number) {
  let height = px2vp(display.getDefaultDisplaySync().height);
  let mBottom = undefined
  switch (bottom) {
    case Gravity.TOP:
      mBottom = height - 200;
      break;
    case Gravity.CENTER:
      mBottom = height / 2;
      break;
    case Gravity.BOTTOM:
      mBottom = undefined
      break;
    default:
      mBottom = bottom
      break;
  }
  return mBottom;
}

/**
 * 弹出1500ms
 * @param message
 * @param bottom 居顶、居中、居底、自定义高度弹出
 */
export function toast(message: string, bottom?: number) {
  promptAction.showToast({
    message: message,
    duration: 1500,
    bottom: bottom === undefined ? undefined : bottomF(bottom)
  })
}

/**
 * 自定义弹出时长
 * @param message
 * @param duration
 * @param bottom  居顶、居中、居底、自定义高度弹出
 */
export function toastDuration(message: string, duration: number, bottom?: number) {
  promptAction.showToast({
    message: message,
    duration: duration,
    bottom: bottom === undefined ? undefined : bottomF(bottom)
  })
}

/**
 * 弹出3000ms
 * @param message
 * @param bottom  居顶、居中、居底、自定义高度弹出
 */
export function toastLong(message: string, bottom?: number) {
  promptAction.showToast({
    message: message,
    duration: 3000,
    bottom: bottom === undefined ? undefined : bottomF(bottom)
  })
}

1、使用:

复制代码
toast("toast")
toast("toast", Gravity.CENTER)
toastDuration("toastDuration", 5000)
toastDuration("toastDuration", 5000, Gravity.CENTER)
toastLong("toastLong")
toastLong("toastLong", Gravity.CENTER)
相关推荐
爱笑的眼睛116 小时前
HarmonyOS 应用开发:深入探索截屏与录屏API的创新实践
华为·harmonyos
爱笑的眼睛117 小时前
深入探讨HarmonyOS中ListItem的滑动操作实现与优化
华为·harmonyos
爱笑的眼睛1111 小时前
可可图片编辑 HarmonyOS 上架应用分享
华为·harmonyos
东林知识库11 小时前
HarmonyOS 6 开发者预览版 Beta 招募
华为·harmonyos
无尽星海max11 小时前
用虚拟机体验纯血鸿蒙所有机型!
华为·harmonyos
I'mAlex11 小时前
0 基础入门鸿蒙:跟着《HarmonyOS 第一课》1 周掌握应用开发核心能力
华为·harmonyos
工藤学编程11 小时前
仓颉原子操作封装:从底层原理到鸿蒙高并发实战
华为·harmonyos
User_芊芊君子11 小时前
【成长纪实】我的鸿蒙成长之路:从“小白”到独立开发,带你走进鸿蒙的世界
学习·华为·harmonyos·鸿蒙开发
●VON11 小时前
【成长纪实】三个月的鸿蒙成长之路:大学生从0开始的鸿蒙心得与体会
华为·架构·harmonyos·鸿蒙·鸿蒙系统·鸿蒙开发·成长纪实
Jinkxs11 小时前
仓颉语言性能优化指南:实测对比,让鸿蒙应用运行效率提升 40%
华为·性能优化·harmonyos