华为鸿蒙应用--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)
相关推荐
AlbertZein几秒前
HarmonyOS一杯冰美式的时间 -- FullScreenLaunchComponent
harmonyos
人工智能知识库4 小时前
华为HCCDP-GaussDB工作级开发者题库(带详细解析)
华为·gaussdb·hccdp-gaussdb·工作级开发者认证
威哥爱编程7 小时前
鸿蒙开发:那些让我熬秃头的“灵异事件”
harmonyos·arkts·arkui
威哥爱编程7 小时前
2026年的IT圈,看看谁在“裸泳”,谁在“吃肉”
后端·ai编程·harmonyos
奔跑的露西ly9 小时前
【HarmonyOS NEXT】进程与线程的理解
华为·harmonyos
REDcker11 小时前
Android WebView 升级 - WebViewUpgrade 库使用详解
android·华为·harmonyos·webview
行者9611 小时前
Flutter跨平台开发:颜色选择器适配OpenHarmony
flutter·harmonyos·鸿蒙
funnycoffee12312 小时前
华为路由器或3层设备接口下的arp broadcast enable作用是啥?
网络·华为
baobao熊13 小时前
【Harmony OS 6】IBest-ORM库使用详解(一)
华为·harmonyos
行者9614 小时前
Flutter鸿蒙跨平台开发:实现高性能可拖拽排序列表组件
flutter·harmonyos·鸿蒙