华为鸿蒙应用--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)
相关推荐
2401_8603195211 小时前
在React Native鸿蒙跨平台开发中实现一个冒泡排序算法并将其应用于数据排序,如何进行复制数组以避免直接修改状态中的数组
javascript·算法·react native·react.js·harmonyos
ujainu13 小时前
FlutterOHOS开发:从基础到跨端实战
flutter·harmonyos·开发
爱吃大芒果13 小时前
Flutter 基础组件详解:Text、Image、Button 使用技巧
开发语言·javascript·flutter·华为·ecmascript·harmonyos
赵财猫._.14 小时前
React Native鸿蒙开发实战(二):基础组件与Flex布局
react native·react.js·harmonyos
盐焗西兰花16 小时前
鸿蒙学习实战之路-多端交互最佳实践
学习·交互·harmonyos
90后的晨仔17 小时前
鸿蒙开发必备:macOS 上 ohpm 的完整安装与配置指南(从报错到成功)
harmonyos
90后的晨仔18 小时前
安装ohpm报错ERROROR: node: command not found Failed to find the executable 'node'
harmonyos
盐焗西兰花18 小时前
鸿蒙学习实战之路-Web 页面适配最佳实践
前端·学习·harmonyos
90后的晨仔18 小时前
ANDROID_HOME not set. Set the environment variable. Then, close DevEco Studio an
harmonyos
俩毛豆19 小时前
【鸿蒙生态共建】一文讲清耗时操作对定时器的超时事件影响及解决方案-《精通HarmonyOS NEXT :鸿蒙App开发入门与项目化实战》读者福利
华为·harmonyos