华为鸿蒙应用--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)
相关推荐
程序猿追5 小时前
HarmonyOS 7 ArkUI 秒表分圈:单圈用时与累计用时应该怎样核对
华为·harmonyos
默 语5 小时前
HarmonyOS7 悬停事件实战:onHover、hoverEffect 四态与设备适配(附完整案例)
harmonyos
程序猿追5 小时前
HarmonyOS 7 多标签草稿,关掉当前页之后该留下谁
pytorch·深度学习·harmonyos
默 语7 小时前
HarmonyOS7基础属性全解(六):浮层 overlay——角标水印挂上去,容器层级一点不加
harmonyos
默 语9 小时前
HarmonyOS7基础属性全解-组件标识id
harmonyos
默 语9 小时前
HarmonyOS7基础属性全解(八):隐私遮罩 obscured——屏幕上看得清清楚楚,录屏里只剩占位图
wpf·harmonyos
默 语9 小时前
HarmonyOS7基础属性全解(五):背景设置——颜色垫底、图片其上、模糊封顶
harmonyos
默 语9 小时前
HarmonyOS7基础属性全解(七):Z序控制 zIndex——谁浮在上面,代码说了算
harmonyos
ovenslove12 小时前
权限申请:为什么你的相机打不开
harmonyos·arkts
NJPJI_Yang13 小时前
鸿蒙操作系统使用 Media Library Kit 实现媒体库资源保存与查询实战(本文已在AtomGit仓库开源)
华为·开源·harmonyos