【HarmonyOS开发】弹窗交互(promptAction )

实现效果

  • 点击按钮实现不同方式的弹窗
  • showToast
  • showDialog
  • showActionMenu

代码实现

1.引入'@ohos.promptAction'

typescript 复制代码
import promptAction from '@ohos.promptAction';

2.通过promptAction 实现系统既定的弹窗

typescript 复制代码
import promptAction from '@ohos.promptAction';

@Entry
@Component
struct Show_Page {
  @State message: string = 'Hello World';

  showToast() {
    promptAction.showToast({
      message: "登录成功", //显示内容
      duration: 2000, //显示持续时间
      bottom: 400//设置显示的距离底部位置
    })
  }

  showDialog() {
    promptAction.showDialog({
      title: "提示",
      message: "您确定要删除嘛?",
      buttons: [
        {
          text: "取消",
          color: "#000"
        },
        {
          text: "确定",
          color: "#000"
        }
      ]

    }).then((data) => {
      console.log(data.index.toString());
    })
  }

  showActionMenu() {
    promptAction.showActionMenu({
      title: "选择字体",
      buttons: [
        {
          text: "测试1",
          color: "#ccc"
        },
        {
          text: "测试2",
          color: "#ccc"
        },
        {
          text: "测试3",
          color: "#ccc"
        },
        {
          text: "测试4",
          color: "#ccc"
        },
        {
          text: "测试5",
          color: "#ccc"
        }
      ]
    }).then((data) => {
      console.log(data.index.toString());
    })
  }

  build() {
    Column() {
      Button() {
        Text("ShowToast").fontColor(Color.White).fontSize(18)
      }
      .width("90%")
      .height(40)
      .margin({ top: 40 })
      .onClick(() => {
        this.showToast();
      })

      Button() {
        Text("ShowDialog").fontColor(Color.White).fontSize(18)
      }
      .width("90%")
      .height(40)
      .margin({ top: 40 })
      .onClick(() => {
        this.showDialog();
      })

      Button() {
        Text("ShowActionMenu").fontColor(Color.White).fontSize(18)
      }
      .width("90%")
      .height(40)
      .margin({ top: 40 })
      .onClick(() => {
        this.showActionMenu();
      })
    }
    .height('100%')
    .width('100%')
  }
}
相关推荐
嘿嘿-g1 小时前
华为IP(9)
网络·华为
self_myth1 小时前
[特殊字符] 深入理解操作系统核心特性:从并发到分布式,从单核到多核的全面解析
windows·macos·wpf·harmonyos
安卓开发者10 小时前
鸿蒙NEXT主题设置指南:应用级与页面级主题定制详解
华为·harmonyos
深盾科技16 小时前
鸿蒙ABC开发中的名称混淆与反射处理策略:安全与效率的平衡
安全·华为·harmonyos
2501_9197490317 小时前
鸿蒙:获取UIContext实例的方法
华为·harmonyos
王佳斌20 小时前
tp报错解决
harmonyos
coooliang20 小时前
【鸿蒙 NEXT】V1迁移V2状态管理
java·前端·harmonyos
江拥羡橙1 天前
【目录-单选】鸿蒙HarmonyOS开发者基础
前端·ui·华为·typescript·harmonyos
爱笑的眼睛111 天前
HarmonyOS应用开发:深入ArkUI声明式开发范式与最佳实践
华为·harmonyos
云水木石1 天前
开源鸿蒙+龙芯CPU,能擦出怎样的火花?
华为·开源·harmonyos