华为鸿蒙应用--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)
相关推荐
Georgewu5 小时前
【HarmonyOS】元服务入门详解 (一)
harmonyos
coder_pig6 小时前
跟🤡杰哥一起学Flutter (三十五、玩转Flutter滑动机制📱)
android·flutter·harmonyos
睿麒10 小时前
鸿蒙app 开发中的Record<string,string>的用法和含义
华为·harmonyos
Fanmeang13 小时前
OSPF与BGP的联动特性实验案例
运维·网络·华为·ospf·bgp·路由黑洞·ospf联动bgp
cainiao08060513 小时前
华为HarmonyOS 5.0深度解析:跨设备算力池技术白皮书(2025全场景智慧中枢)
华为·harmonyos
万少13 小时前
04-自然壁纸实战教程-搭建基本工程
前端·harmonyos·客户端
yrjw15 小时前
FileSaver是一个为HarmonyOS ArkTS应用设计的开源库,提供便捷的文件保存功能。主要特性包括:支持将图片保存至系统相册和应用沙盒存储,支持多种
harmonyos
xo1988201116 小时前
鸿蒙选择本地视频文件,并获取首帧预览图
华为·harmonyos
在人间耕耘17 小时前
HarmonyOS组件/模板集成创新活动-开发者工具箱
华为·harmonyos
二流小码农18 小时前
鸿蒙开发:一键更新,让应用无需提交应用市场即可下载安装
android·ios·harmonyos