华为鸿蒙应用--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)
相关推荐
HarmonyOS_SDK1 小时前
HarmonyOS SDK使能美团高效开发,打造优质创新应用体验
harmonyos
zhanshuo15 小时前
HarmonyOS 分布式神器!手把手教你用 ArkTS 写出全屋联动智能助手
harmonyos
zhanshuo15 小时前
让你的鸿蒙应用“离线也能飞”——ArkTS 存储机制实战
harmonyos
AORO202521 小时前
国产智能三防手机哪款最好?这款支持单北斗、5G-A、IP68
5g·安全·智能手机·信息与通信·harmonyos
啥都不会的码仔1 天前
鸿蒙ArkTS日期时间处理:全面解析DateUtil工具类
harmonyos·arkts
城中的雾1 天前
HarmonyOS 5 应用拉起系列(二):如何拉起微信小程序
harmonyos
AORO20251 天前
三防平板电脑是什么?这款三防平板支持红外测温!
5g·安全·智能手机·电脑·harmonyos
AlbertZein1 天前
HarmonyOS 5 一杯冰美式的时间 -- MVVM?
harmonyos
zhanshuo2 天前
HarmonyOS 多屏适配最佳实践:基于 ArkUI 的响应式 UI 方案
harmonyos
zhanshuo2 天前
玩转 ArkUI 拖拽功能:5 分钟搞定拖放交互与场景实战
harmonyos