华为鸿蒙应用--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)
相关推荐
maaath2 小时前
【maaath】Flutter for OpenHarmony 乐器学习应用开发实战
flutter·华为·harmonyos
数智顾问3 小时前
(123页PPT)华为流程管理体系精髓提炼(附下载方式)
运维·华为
李游Leo5 小时前
HarmonyOS AbilityStage 实战:别把启动参数散落在每个页面里
harmonyos
李李李勃谦7 小时前
鸿蒙PCBI 报表工具:连接数据库与可视化报表生成
数据库·华为·交互·harmonyos
maaath7 小时前
【maaath】 Flutter for OpenHarmony 实战:电池优化应用开发指南
flutter·华为·harmonyos
aqi009 小时前
一文读懂 HarmonyOS 6.1 带来的十大重要升级
android·华为·harmonyos·鸿蒙·harmony
智能化咨询9 小时前
(105页PPT)华为智慧供应链ISC+IT蓝图规划设计方案ISC+战略愿景与蓝图设计ISC+能力框架与架构设计实施路径(附下载方式)
华为
李李李勃谦10 小时前
鸿蒙PC配色方案工具:取色、配色生成与 CSS 导出
前端·css·华为·harmonyos
SmartBrain11 小时前
《资治通鉴》20 条智慧赋能企业经营管理
华为·架构·创业创新
条tiao条11 小时前
从静态到动态:鸿蒙 ArkTS 列表组件与状态装饰器实战
华为·harmonyos