【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%')
  }
}
相关推荐
__Benco1 小时前
OpenHarmony平台驱动使用(十五),SPI
人工智能·驱动开发·harmonyos
暗雨1 小时前
封装一个 auth 工具
harmonyos
ChinaDragon2 小时前
HarmonyOS:进度条 (Progress)
harmonyos
半路下车2 小时前
【Harmony OS 5】DevEco Testing赋能智慧教育
harmonyos·arkts
libo_20252 小时前
HarmonyOS5 灰度发布:通过AGC控制台分阶段更新Uniapp混合应用
harmonyos
libo_20252 小时前
自动化测试:将Uniapp页面注入HarmonyOS5 UITest框架
harmonyos
libo_20252 小时前
HarmonyOS5 Uniapp+OpenHarmony标准设备适配指南
harmonyos
libo_20252 小时前
HarmonyOS5 内存优化:用DevEco Studio Profiler分析Uniapp混合栈泄漏
harmonyos
libo_20252 小时前
AI能力整合:在Uniapp中调用HarmonyOS5 HiAI Kit的图像识别
harmonyos