当前位置:   article > 正文

华为鸿蒙应用--Toast工具(鸿蒙工具)-ArkTs_arkts toast

arkts toast

0、代码

  1. import promptAction from '@ohos.promptAction'
  2. import display from '@ohos.display';
  3. export enum Gravity {
  4. TOP = 10000,
  5. CENTER = 10001,
  6. BOTTOM = 10002,
  7. }
  8. function bottomF(bottom?: number) {
  9. let height = px2vp(display.getDefaultDisplaySync().height);
  10. let mBottom = undefined
  11. switch (bottom) {
  12. case Gravity.TOP:
  13. mBottom = height - 200;
  14. break;
  15. case Gravity.CENTER:
  16. mBottom = height / 2;
  17. break;
  18. case Gravity.BOTTOM:
  19. mBottom = undefined
  20. break;
  21. default:
  22. mBottom = bottom
  23. break;
  24. }
  25. return mBottom;
  26. }
  27. /**
  28. * 弹出1500ms
  29. * @param message
  30. * @param bottom 居顶、居中、居底、自定义高度弹出
  31. */
  32. export function toast(message: string, bottom?: number) {
  33. promptAction.showToast({
  34. message: message,
  35. duration: 1500,
  36. bottom: bottom === undefined ? undefined : bottomF(bottom)
  37. })
  38. }
  39. /**
  40. * 自定义弹出时长
  41. * @param message
  42. * @param duration
  43. * @param bottom 居顶、居中、居底、自定义高度弹出
  44. */
  45. export function toastDuration(message: string, duration: number, bottom?: number) {
  46. promptAction.showToast({
  47. message: message,
  48. duration: duration,
  49. bottom: bottom === undefined ? undefined : bottomF(bottom)
  50. })
  51. }
  52. /**
  53. * 弹出3000ms
  54. * @param message
  55. * @param bottom 居顶、居中、居底、自定义高度弹出
  56. */
  57. export function toastLong(message: string, bottom?: number) {
  58. promptAction.showToast({
  59. message: message,
  60. duration: 3000,
  61. bottom: bottom === undefined ? undefined : bottomF(bottom)
  62. })
  63. }

1、使用:

toast("toast")
toast("toast", Gravity.CENTER)
toastDuration("toastDuration", 5000)
toastDuration("toastDuration", 5000, Gravity.CENTER)
toastLong("toastLong")
toastLong("toastLong", Gravity.CENTER)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/124427
推荐阅读
相关标签
  

闽ICP备14008679号