<acronym id="s8ci2"><small id="s8ci2"></small></acronym>
<rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
<acronym id="s8ci2"></acronym>
<acronym id="s8ci2"><center id="s8ci2"></center></acronym>
0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

深入淺出學習eTs之電量不足提示彈窗實例

Harmony&嵌入式學習 ? 來源:Harmony&嵌入式學習 ? 作者:Harmony&嵌入式學 ? 2023-05-13 13:25 ? 次閱讀

本項目Gitee倉地址:深入淺出eTs學習: 帶大家深入淺出學習eTs (gitee.com)

一、需求分析

wKgaomRfHz-AWHurAAJmQsDI0GA789.png

相信大家生活中也經常會遇到上方情況,本章節我們來模擬提示一個電量不足的顯示,使用自定義彈窗來實現

提示電量不足

可以選擇關閉和低電量模式

顯示當前剩余電量

二、控件介紹

自定義彈窗:官方文檔

說明: 從API Version 7開始支持。后續版本如有新增內容,則采用上角標單獨標記該內容的起始版本。

通過CustomDialogController類顯示自定義彈窗。

接口

CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean, alignment?: DialogAlignment, offset?: Offset, customStyle?: boolean})

參數

參數名 參數類型 必填 默認值 參數描述
builder CustomDialog - 自定義彈窗內容構造器。
cancel () => void - 點擊遮障層退出時的回調。
autoCancel boolean true 是否允許點擊遮障層退出。
alignment DialogAlignment DialogAlignment.Default 彈窗在豎直方向上的對齊方式。
offset { dx: Length | Resource, dy: Length | Resource } - 彈窗相對alignment所在位置的偏移量。
customStyle boolean false 彈窗容器樣式是否自定義。
gridCount8+ number - 彈窗寬度占柵格寬度的個數。

DialogAlignment枚舉說明

名稱 描述
Top 垂直頂部對齊。
Center 垂直居中對齊。
Bottom 垂直底部對齊。
Default 默認對齊。
TopStart8+ 左上對齊。
TopEnd8+ 右上對齊。
CenterStart8+ 左中對齊。
CenterEnd8+ 右中對齊。
BottomStart8+ 左下對齊。
BottomEnd8+ 右下對齊。

代碼介紹:

declare class CustomDialogController {
  constructor(value: CustomDialogControllerOptions); // 對話框控制器,控制彈框樣式等
  open();                                            // 打開對話框
  close();                                           // 關閉對話框
}

// 配置參數的定義
declare interface CustomDialogControllerOptions {
  builder: any;                                      // 彈框構造器
  cancel?: () => void;                               // 點擊蒙層的事件回調
  autoCancel?: boolean;                              // 點擊蒙層是否自動消失
  alignment?: DialogAlignment;                       // 彈框在豎直方向上的對齊方式
  offset?: Offset;                                   // 根據alignment的偏移
  customStyle?: boolean;                             // 是否是自定義樣式
  gridCount?: number;                                // grid數量
}

CustomDialogController

導入對象

dialogController : CustomDialogController = new CustomDialogController(value:{builder: CustomDialog, cancel?: () => void, autoCancel?: boolean})

open()

open(): void

顯示自定義彈窗內容,若已顯示,則不生效。

close

close(): void

關閉顯示的自定義彈窗,若已關閉,則不生效。

三、UI設計

(1)彈窗實現

本章節的UI設計特別簡單,僅需要實現一個彈窗即可

開介紹,我們需要在@Entry外進行定義,定義類型是@CustomDialog,其基本結構如下(官網)

@CustomDialog
struct CustomDialogExample {
  controller: CustomDialogController
  cancel: () => void
  confirm: () => void

  build() {
    Column() {
      Text('Software uninstall').width('70%').fontSize(20).margin({ top: 10, bottom: 10 })
      Image($r('app.media.icon')).width(80).height(80)
      Text('Whether to uninstall a software?').fontSize(16).margin({ bottom: 10 })
      Flex({ justifyContent: FlexAlign.SpaceAround }) {
        Button('cancel')
          .onClick(() => {
            this.controller.close()
            this.cancel()
          }).backgroundColor(0xffffff).fontColor(Color.Black)
        Button('confirm')
          .onClick(() => {
            this.controller.close()
            this.confirm()
          }).backgroundColor(0xffffff).fontColor(Color.Red)
      }.margin({ bottom: 10 })
    }
  }
}

通過上面的程序可以實現下面的效果

wKgZomRfHz-Abk_WAAZeSwTl3AM866.gif

我們需要對內容和格式進行修改

@CustomDialog
struct CustomBatteryDialog  {
  controller: CustomDialogController
  cancel: () => void
  confirm: () => void

  build() {
    Stack() {
      Column() {
        Text('電池電量不足')
          .fontSize(22)
          .margin({top: 15})
          .fontColor(Color.Black)
        Text('還剩20%電量')
          .fontSize(17)
          .margin({top: 15})
          .fontColor(Color.Red)
        Text()
          .size({width: "100%", height: "2px"})
          .backgroundColor("#bebbc1")
          .margin({top: 15})
        Row() {
          Text("關閉")
            .height("100%")
            .layoutWeight(1)
            .textAlign(TextAlign.Center)
            .fontSize(20)
            .fontColor("#317ef5")
            .onClick(() => {
              this.controller.close(); // 關閉彈窗
            })
          Text()
            .size({width: "2px", height: "100%"})
            .backgroundColor("#bebbc1")
          Text("低電量模式")
            .textAlign(TextAlign.Center)
            .fontSize(20)
            .fontColor("#317ef5")
            .height("100%")
            .layoutWeight(1)
            .onClick(() => {
              this.controller.close(); // 關閉彈窗
            })
        }
        .height(45)
        .width('100%')
      }
      .backgroundColor("#e6ffffff")
      .borderRadius(20)
    }
    .padding({left: 40, right: 40})
    .width("100%")
  }
}

實現效果如下:

wKgaomRfH0CABhXaAABA8VSXUfw733.png

(2)彈窗調用

彈窗調用的函數為this.controller.open(),一般是通過給定事件,像點擊按鈕或者之類,我們這里選擇使用直接彈窗的形式(打開APP就彈窗)

使用到函數為onPageShow(),其位置在該位置:

@Entry 
@Component 
struct Index {

  onPageShow() {
    this.controller.open()

  }

  build() {

  }
}

四、系統演示

wKgZomRfH0CAYKHeAAGNHenO-TQ540.gif

已實現效果,如上圖所示。

編輯:黃飛

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • ets
    ets
    +關注

    關注

    0

    文章

    20

    瀏覽量

    1578
  • OpenHarmony
    +關注

    關注

    24

    文章

    3447

    瀏覽量

    15330
收藏 人收藏

    評論

    相關推薦

    深入淺出玩轉fpga PDF教程和光盤資源

    深入淺出玩轉FPGA,作者吳厚航,由北京航空航天大學出版社出版。本書收集整理了作者在FPGA學習和實踐中的經驗點滴。書中既有日常的學習筆記,對一些常用設計技巧和方法進行深入探討;也有很
    發表于 02-27 10:45

    深入淺出AVR

    深入淺出AVR,一本書。
    發表于 07-15 12:02

    深入淺出玩轉FPGA

    深入淺出玩轉FPGA
    發表于 07-21 09:21

    深入淺出Android

    深入淺出Android
    發表于 08-20 10:14

    ARM7 深入淺出學習

    深入淺出ARM7 LPC213x_214 學習
    發表于 12-04 17:28

    深入淺出Android

    深入淺出Android
    發表于 04-26 10:48

    深入淺出AVR

    深入淺出AVR
    發表于 08-23 10:10

    深入淺出排序學習使用指南

    深入淺出排序學習:寫給程序員的算法系統開發實踐
    發表于 09-16 11:38

    #深入淺出學習eTs#(一)模擬器/真機環境搭建

    本項目的Gitee倉地址: 深入淺出eTs學習: 帶大家深入淺出學習eTs (gitee.com
    發表于 12-24 13:02

    #深入淺出學習eTs#(二)拖拽式UI

    本項目Gitee倉地址:深入淺出eTs學習: 帶大家深入淺出學習eTs (gitee.com)一
    發表于 12-29 09:56

    #深入淺出學習eTs#(五)eTs語言初識

    本項目Gitee倉地址:深入淺出eTs學習: 帶大家深入淺出學習eTs (gitee.com)一
    發表于 12-29 10:02

    #深入淺出學習eTs#(六)編寫eTs第一個控件

    本項目Gitee倉地址:深入淺出eTs學習: 帶大家深入淺出學習eTs (gitee.com)一
    發表于 12-29 10:05

    #深入淺出學習eTs#(七)判斷密碼是否正確

    本項目Gitee倉地址:深入淺出eTs學習: 帶大家深入淺出學習eTs (gitee.com)一
    發表于 12-29 10:06

    #深入淺出學習eTs#(十一)別忘了吃藥喔

    本項目Gitee倉地址:深入淺出eTs學習: 帶大家深入淺出學習eTs (gitee.com)一
    發表于 12-29 13:33

    #深入淺出學習eTs#(十二)您的電量不足

    本項目Gitee倉地址:深入淺出eTs學習: 帶大家深入淺出學習eTs (gitee.com)一
    發表于 12-29 13:50
    亚洲欧美日韩精品久久_久久精品AⅤ无码中文_日本中文字幕有码在线播放_亚洲视频高清不卡在线观看
    <acronym id="s8ci2"><small id="s8ci2"></small></acronym>
    <rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
    <acronym id="s8ci2"></acronym>
    <acronym id="s8ci2"><center id="s8ci2"></center></acronym>