华为鸿蒙应用--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)
相关推荐
世人万千丶4 分钟前
Flutter 框架跨平台鸿蒙开发 - 嫉妒分析器应用
学习·flutter·华为·开源·harmonyos·鸿蒙
人间打气筒(Ada)12 分钟前
「码动四季·开源同行」HarmonyOS应用开发:鸿蒙系统概述
华为·harmonyos·deveco studio·鸿蒙开发·鸿蒙开发入门
行走的小派26 分钟前
本地跑模型+原生开源鸿蒙:拆解香橙派AI手机的12TOPS端侧硬核玩法
人工智能·开源·harmonyos
小雨青年29 分钟前
鸿蒙 HarmonyOS 6 | 分布式数据同步详解
分布式·华为·harmonyos
小雨天気.34 分钟前
Flutter 框架跨平台鸿蒙开发 - 家庭财务规划应用
flutter·华为·生活·harmonyos·鸿蒙
AI_零食36 分钟前
Flutter 框架跨平台鸿蒙开发 - 社交断舍离应用
运维·服务器·学习·flutter·游戏·开源·harmonyos
世人万千丶38 分钟前
Flutter 框架跨平台鸿蒙开发 - 感恩杀手应用
学习·flutter·开源·harmonyos·鸿蒙
梁山好汉(Ls_man)1 小时前
关于华为鸿蒙系统5A标志的几点看法
华为·鸿蒙·信号·5a
AI_零食1 小时前
Flutter 框架跨平台鸿蒙开发 - 颜色听觉化应用
学习·flutter·信息可视化·开源·harmonyos
特立独行的猫a1 小时前
HarmonyOS鸿蒙PC的QT应用开发:QT项目运行原理与 EmbeddedUIExtensionAbility介绍
qt·华为·harmonyos·openharmony·鸿蒙pc