华为鸿蒙应用--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)
相关推荐
咱入行浅12 分钟前
汽车之家联合HarmonyOS SDK,深度构建鸿蒙生态体系
华为·汽车·harmonyos
yaoyaoxingzhe5 小时前
HarmonyOS应用开发实战:猫猫大作战-Popup 的实现【apple_product_name】
华为·harmonyos
程序员黑豆5 小时前
鸿蒙应用开发之@Styles 装饰器:定义可复用的组件样式
前端·harmonyos
qizayaoshuap5 小时前
HarmonyOS标签页 — 顶部 Tab 切换的内容展示设计
华为·harmonyos
程序员黑豆8 小时前
鸿蒙应用开发之持久化存储解析:PersistentStorage / PersistenceV2 / preferences 选型与实战
前端·harmonyos
DRXB25072010 小时前
开源自由还是生态红利?LangChain 的灵活性与小艺开放平台的鸿蒙流量池,开发者该如何抉择?
langchain·开源·harmonyos
qizayaoshuap10 小时前
HarmonyOS :底部导航 — 构建 Tab 导航栏的标准模式
华为·harmonyos
程序员黑豆10 小时前
鸿蒙应用开发之路由:Router 页面路由使用教程
前端·harmonyos
lmy_loveF15 小时前
构建第一个HarmonyOS元服务
华为·harmonyos
哦***715 小时前
鸿蒙小知识 | 华为耳机双击没法暂停音乐?
华为·音频·harmonyos