华为鸿蒙应用--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)
相关推荐
less_121385 小时前
HarmonyOS WPS Open SDK:打开时配置水印与修订模式
华为·harmonyos·wps
HarmonyOS_SDK9 小时前
差异化推送消息能力,助力腕上支付业务闭环
harmonyos
小雨青年12 小时前
【HarmonyOS 7 沉浸光感深度实战】 03 五档 ImmersiveStyle 到底怎么选
华为·harmonyos
云端漫步198713 小时前
HarmonyOS NEXT AI 智能生活助手:性能优化
华为·性能优化·生活·harmonyos
云端漫步198713 小时前
HarmonyOS NEXT AI 智能生活助手:统一 AIService 封装
人工智能·华为·生活·harmonyos
云端漫步198716 小时前
HarmonyOS NEXT AI 智能生活助手:设置中心开发
人工智能·华为·生活·harmonyos
用户09340777351417 小时前
HarmonyOS WPS Open SDK 实践:关文档后怎么把结果拿回本应用
harmonyos
less_1213818 小时前
HarmonyOS WPS Open SDK:用 extraOptions 管住分享打印与导出
深度学习·harmonyos·wps
less_1213819 小时前
HarmonyOS WPS Open SDK:OpenFileRequest 只读与可编辑模式控制
深度学习·harmonyos·wps
程序员黑豆1 天前
使用AI编程开发鸿蒙应用:从环境搭建到实战示例
前端·harmonyos