Harmony Codelab 样例—弹窗基本使用

news/2024/7/21 12:06:02 标签: HarmonyOS

一、介绍

本篇 Codelab 主要基于 dialog 和 button 组件,实现弹窗的几种自定义效果,具体效果有:  

1.  警告弹窗,点击确认按钮弹窗关闭。

2.  确认弹窗,点击取消按钮或确认按钮,触发对应操作。

3.  加载弹窗,展示加载中效果。

4.  提示弹窗,支持用户输入内容,点击取消和确认按钮,触发对应操作。

5.  进度条弹窗,展示进度条以及百分比。

相关概念

dialog组件:自定义弹窗容器组件。

button组件:按钮组件。

完整示例

gitee源码地址

源码下载

弹窗基本使用(JS).zip

二、环境搭建

我们首先需要完成 HarmonyOS 开发环境搭建,可参照如下步骤进行。

软件要求

DevEco Studio版本:DevEco Studio 3.1 Release。 

HarmonyOS SDK版本:API version 9。

硬件要求

设备类型:华为手机或运行在 DevEco Studio 上的华为手机设备模拟器。

HarmonyOS 系统:3.1.0 Developer Release。

环境搭建

1.  安装 DevEco Studio,详情请参考下载和安装软件。

2.  设置 DevEco Studio 开发环境,DevEco Studio 开发环境需要依赖于网络环境,需要连接上网络才能确保工具的正常使用,可以根据如下两种情况来配置开发环境:

● 如果可以直接访问 Internet,只需进行下载HarmonyOS SDK操作。

● 如果网络不能直接访问 Internet,需要通过代理服务器才可以访问,请参考配置开发环境。

3.  开发者可以参考以下链接,完成设备调试的相关配置:

● 使用真机进行调试

● 使用模拟器进行调试

三、代码结构解读

本篇 Codelab 只对核心代码进行讲解,对于完整代码,我们会在源码下载或 gitee 中提供。

├──entry/src/main/js	     // 代码区│  └──MainAbility│     ├──common│     │  └──images           // 图片资源│     ├──i18n		     // 国际化中英文│     │  ├──en-US.json			│     │  └──zh-CN.json			│     ├──pages│     │  └──index│     │     ├──index.css     // 页面整体布局以及弹窗样式│     │     ├──index.hml     // 自定义弹窗展示页面│     │     └──index.js      // 弹窗显示关闭逻辑以及动画逻辑│     └──app.js              // 程序入口└──entry/src/main/resources  // 应用资源目录

四、构建应用页面

界面主要包括按钮列表页和自定义弹窗两部分,我们可以通过在 dialog 标签中添加自定义组件设置弹窗,具体效果如图所示:

首先搭建 index.hml 中的按钮页,主要包括 5 种常见的弹窗,分别为 AlertDialog、ConfirmDialog、LoadingDialog、PromptDialog 以及 ProgressDialog。

<!--index.hml--><div class="btn-div">    <button type="capsule" value="AlertDialog" class="btn" onclick="showAlert"></button>    <button type="capsule" value="ConfirmDialog" class="btn" onclick="showConfirm"></button>    <button type="capsule" value="LoadingDialog" class="btn" onclick="showLoading"></button>    <button type="capsule" value="PromptDialog" class="btn" onclick="showPrompt"></button>    <button type="capsule" value="ProgressDialog" class="btn" onclick="showProgress"></button></div>

然后在 index.hml 中创建 AlertDialog 自定义弹窗,效果如图所示:

<!-- index.hml --><!-- AlertDialog自定义弹窗 --><dialog id="alertDialog" class="alert-dialog">    <div class="dialog-div">        <div class="alert-inner-txt">            <text class="txt">AlertDialog</text>        </div>        <div class="alert-inner-btn">            <button class="btn-single" type="capsule" value="Confirm"                 onclick="confirmClick('alertDialog')"></button>        </div>    </div></dialog>

创建 ConfirmDialog 自定义弹窗,效果如图所示:

<!-- index.hml --><!-- ConfirmDialog自定义弹窗 --><dialog id="confirmDialog" class="dialog-main">    <div class="dialog-div">        <div class="inner-txt">            <text class="txt">ConfirmDialog</text>        </div>        <div class="inner-btn">            <button type="capsule" value="Cancel" class="btn-txt-left"                 onclick="cancelClick('confirmDialog')"></button>            <button type="capsule" value="Confirm" class="btn-txt-right"                 onclick="confirmClick('confirmDialog')"></button>        </div>    </div></dialog>

创建 LoadingDialog 自定义弹窗,效果如图所示:

<!-- index.hml --><!-- LoadingDialog自定义弹窗 --><dialog id="loadingDialog" class="low-height-dialog">    <div class="dialog-loading">        <text>Loading...</text>        <image class="loading-img img-rotate" id="loading-img"             src="/common/images/ic_loading.svg"></image>    </div></dialog>

创建 PromptDialog 自定义弹窗,效果如图所示:

<!-- index.hml --><!-- PromptDialog自定义弹窗 --><dialog id="promptDialog" class="dialog-prompt">    <div class="dialog-div-prompt">        <div class="inner-txt-prompt">            <text class="txt">PromptDialog</text>        </div>        <input class="prompt-input" type="password" placeholder="please enter password"></input>        <div class="inner-btn">            <button type="capsule" value="Cancel" class="btn-txt-left"                 onclick="cancelClick('promptDialog')"></button>            <button type="capsule" value="Confirm" class="btn-txt-right"                 onclick="confirmClick('promptDialog')"></button>        </div>    </div></dialog>

创建 ProgressDialog 自定义弹窗,效果如图所示:

<!-- index.hml --><!-- ProgressDialog自定义弹窗 --><dialog id="progressDialog" class="low-height-dialog" oncancel="onCancel">    <div class="dialog-progress-div">        <div class="inner-txt-progress">            <text class="download-txt">Downloading...</text>            <text>{{ percent + '%' }}</text>        </div>        <div class="progress-div">            <progress class="min-progress" type="horizontal" percent="{{ percent }}"                 secondarypercent="50"></progress>        </div>    </div></dialog>

然后在 index.js 文件中实现不同 button 的点击事件,展示对应自定义弹窗:

// index.jsexport default {  data: {...},
  // 展示AlertDialog  showAlert() {    this.$element('alertDialog').show();  },
  // 展示ConfirmDialog  showConfirm() {    this.$element('confirmDialog').show();  },
  // 展示LoadingDialog  showLoading() {    ...    this.animation = this.$element('loading-img').animate(frames, options);    this.animation.play();    this.$element('loadingDialog').show();  },
  // 展示PromptDialog  showPrompt() {    this.$element('promptDialog').show();  },
  // 展示ProgressDialog  showProgress() {    ...  }}

五、总结

您已经完成了本次 Codelab 的学习,并了解到以下知识点:

1.  dialog 自定义弹窗容器组件的使用。

2.  button 按钮组件的使用。


http://www.niftyadmin.cn/n/5035099.html

相关文章

思科路由器:NAT的基础配置

一直以来&#xff0c;对于华为、H3C、锐捷交换机的命令配置&#xff0c;不断的有朋友留言&#xff0c;三家交换机的配置命令容易弄混&#xff0c;经常在实际项目配置中出错&#xff0c;因此&#xff0c;找几个基础的示例来练练。 R1配置 Router>en Router>enable Rout…

一文告诉你为什么时序场景下 TDengine 数据订阅比 Kafka 好

在 TDengine 3.0 中&#xff0c;我们对流式计算、数据订阅功能都进行了再升级&#xff0c;帮助用户极大简化了数据架构的复杂程度&#xff0c;降低整体运维成本。TDengine 提供的类似消息队列产品的数据订阅、消费接口&#xff0c;本质上是为了帮助应用实时获取写入 TDengine 的…

计算机网络选择题笔记

令牌环&#xff1a;令牌环上传输的小的数据&#xff08;3个字节的一种特殊帧&#xff09;叫为令牌&#xff0c;谁有令牌谁就有传输权限。如果环上的某个工作站收到令牌并且有信息发送&#xff0c;它就改变令牌中的一位&#xff08;该操作将令牌变成一个帧开始序列&#xff09;&…

mysql中server_id的作用

在MySQL中&#xff0c;server_id是一个重要的配置参数&#xff0c;用于唯一标识MySQL服务器实例。server_id的配置和使用在MySQL复制和高可用性&#xff08;HA&#xff09;方案中至关重要&#xff0c;因为它允许不同的MySQL服务器之间进行数据同步和复制&#xff0c;并确保数据…

慢SQL治理经验总结

在过去两年的工作中&#xff0c;我们团队曾负责大淘宝技术的慢SQL治理工作&#xff0c;作为横向的数据安全治理平台&#xff0c;如何快速准确地发现部门内所有应用的慢SQL&#xff0c;并进行高效的推动治理&#xff0c;同时覆盖多个开发、生产环境&#xff0c;是一个很大的挑战…

Python Burp Header 整理代码

&#xff08;自用&#xff09; #!/usr/bin/env python # coding:utf-8 # autor:Pai/Part_03 # email:*** # wechat:***input_headers """ User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/116.0 Accept: applicatio…

广告弹窗关不掉?或许是网站流量被劫持了

相信大家都有过这样的经验&#xff0c;正在浏览网页时忽然出现广告弹窗&#xff0c;手动关闭窗口的话&#xff0c;则会跳转到另一个不知名的网站。其实&#xff0c;这正是网站流量被劫持的表现。网站流量劫持分为域名劫持和HTTP劫持&#xff0c;HTTP劫持即是在网站页面上添加弹…

什么是MQ消息队列及四大主流MQ的优缺点(个人网站复习搬运)

什么是&#xff2d;&#xff31;消息队列及四大主流&#xff2d;&#xff31;的优缺点 小程序要上一个限时活动模块&#xff0c;需要有延时队列&#xff0c;从网上了解到用RabbitMQ可以解决&#xff0c;就了解了下 MQ 并以此做记录。 一、为什么要用 MQ 核心就是解耦、异步和…