华为鸿蒙应用--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 分钟前
CMP for OpenHarmony 实战:Coil 图片加载库鸿蒙化使用心得
运维·服务器·开发语言·华为·ai编程·harmonyos·openharmony
小雨青年14 分钟前
【HarmonyOS 7 小艺 Skill 开发实战】 01 创建第一个会后行动助手
华为·harmonyos
独隅32 分钟前
HarmonyOS 核心能力与实效展示
华为·harmonyos
2501_944525761 小时前
开源鸿蒙平台 KMP 三方库「KMPalette」适配全流程
华为·开源·harmonyos
2601_949480061 小时前
开源鸿蒙平台 CMP 三方库「skiko」适配全流程
华为·开源·harmonyos
OH_TPC1 小时前
【鸿蒙优选三方库】@ohos/ijkplayer:B 站同源播放器,啥格式都能播
华为·harmonyos·鸿蒙
传奇开心果编程1 小时前
组合优先开发范式:从汉字造字原理到声明式UI组件组合思想
android·flutter·ios·swiftui·移动开发·harmonyos·android jetpack
网络豆2 小时前
开源鸿蒙平台 CMP 三方库「kmp-onboarding」适配全流程
华为·开源·harmonyos
网络豆2 小时前
开源鸿蒙平台 CMP 三方库「dotsindicator」适配全流程
华为·开源·harmonyos
resh_people2 小时前
开源鸿蒙平台 CMP 三方库「kmp-wheel-picker」适配全流程
华为·开源·harmonyos