鸿蒙arkTs Toast抽取 及使用

Toast抽取,创建一个Utils

import promptAction from '@ohos.promptAction';
import display from '@ohos.display';
export function ToastUtils(msg:string){
  try {
    promptAction.showToast({
      message: msg,
      duration: 1500,
      bottom:450
    });
  } catch (error) {
    console.error(`showToast args error code is ${error.code}, message is ${error.message}`);
  };
}

  使用:

import promptAction from '@ohos.promptAction';
import { StringIsEmpty } from '../utils/StringUtils';
import { ToastUtils } from '../utils/ToastUtils';
@Entry
@Component
struct SubmitFormPage {
  @State message: string = 'Hello World'
  @State inputText: string = ""
  build() {
    Row() {
      Column({space:10}) {
        TextInput({ text: this.inputText, placeholder: '请输入名字' })
          .fontSize(15)
          .fontWeight(FontWeight.Normal)
          .width('100%')
          .height(50)
          .backgroundColor('#f6f6f6')
          .borderRadius(10)
          .onChange((value) => {
              this.inputText= value
          })



        Button("提交").height(50).width(100).backgroundColor(Color.Blue).alignSelf(ItemAlign.Center).onClick(()=>{
          let isEmpty = StringIsEmpty(this.inputText);
          if (isEmpty) {
            ToastUtils("请输入文字~")
          }else{
            ToastUtils(this.inputText)
          }

        })


      }
      .width('100%').justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Start)



    }
    .height('100%')
  }
}

stringUtils只是判断是否为空

export function StringIsEmpty(str:string){
  return str === null || str === undefined || str.trim().length === 0;
}

相关推荐

  1. 鸿蒙arkTs Toast抽取 使用

    2023-12-15 15:56:03       67 阅读
  2. vue项目使用typescript创建抽象使用

    2023-12-15 15:56:03       60 阅读
  3. 鸿蒙应用开发】开发环境搭建IDE安装使用

    2023-12-15 15:56:03       69 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-15 15:56:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-15 15:56:03       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-15 15:56:03       82 阅读
  4. Python语言-面向对象

    2023-12-15 15:56:03       91 阅读

热门阅读

  1. 常用的调试方法(段错误产生原因)

    2023-12-15 15:56:03       56 阅读
  2. Linux常见面试题30题详细答案解析(二)

    2023-12-15 15:56:03       55 阅读
  3. Spring Boot中Web类型

    2023-12-15 15:56:03       57 阅读
  4. 取火柴游戏

    2023-12-15 15:56:03       57 阅读
  5. C语言:实现字符串连接

    2023-12-15 15:56:03       62 阅读
  6. 二建自学资料

    2023-12-15 15:56:03       62 阅读
  7. git关于ssh我遇到的问题解决链接

    2023-12-15 15:56:03       66 阅读
  8. 判断数组中对象的某个值是否有相同的并去重

    2023-12-15 15:56:03       58 阅读