华为鸿蒙应用--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)
相关推荐
熊猫钓鱼>_>7 小时前
Kotlin Multiplatform for OpenHarmony 实战:为 KMPNotifier 实现 OpenHarmony 本地通知引擎
kotlin·ai编程·harmonyos·鸿蒙·openharmony·适配·kmp
特立独行的猫A10 小时前
cj-tauri开源项目介绍与imovie观影客户端项目实战
harmonyos
fellow9914 小时前
Electron + pnpm 在鸿蒙应用里跑起来
electron·harmonyos·openharmony·deepseek·deepseekharness
熊猫钓鱼>_>16 小时前
从闲置平板到家里的“控制大脑“:鸿蒙智慧中控面板完整实战
运维·人工智能·华为·自动化·电脑·ai编程·harmonyos
5008416 小时前
React Native for OpenHarmony 实战:三方库 react-native-url-polyfill 的鸿蒙化适配指南
javascript·react native·react.js·性能优化·electron·harmonyos
熊猫钓鱼>_>18 小时前
从2D平铺到3D沉浸:我用HarmonyOS 7端侧AI做了一个全程数据不出设备的空间化私密相册
前端·人工智能·3d·华为·华为云·harmonyos·鸿蒙
m0_7381858219 小时前
Flutter 鸿蒙化实战:foundation_fluttify 适配 OpenHarmony,Fluttify 桥接层
flutter·华为·harmonyos·鸿蒙
m0_7381858219 小时前
Flutter 鸿蒙化实战:http_proxy 适配 OpenHarmony,HTTP 代理
flutter·http·华为·harmonyos·鸿蒙
m0_7381858219 小时前
Flutter 鸿蒙化实战:headset_connection_event 适配 OpenHarmony,耳机插拔监听
flutter·华为·harmonyos·鸿蒙
5008420 小时前
React Native for OpenHarmony 实战:三方库 react-native-volume-control 的鸿蒙化适配指南
javascript·react native·react.js·electron·harmonyos