0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

ArkUI如何自定义弹窗(eTS)

ArkUI详解 来源:鸿蒙实验室 作者:鸿蒙实验室 2022-08-31 08:24 次阅读

自定义弹窗其实也是比较简单的,通过CustomDialogController类就可以显示自定义弹窗。

接下来我们通过代码来看一下

大家也都用过@Entry,@Component等弹窗的话,只要用@CustomDialog就可以

先来预览一下我实现的效果:

gif145gif

import

CustomDialogExample

from

'./customdialog'

@

Entry

@

Component

struct

Index

{

// 方式一:使用箭头函数

onAccept

=

()

=>

{

console

.

info

(

'确定'

)

this

.

dialogController

.

close

();

}

dialogController

:

CustomDialogController

=

new

CustomDialogController

({

builder

:

CustomDialogExample

({

cancel

:

this

.

onCancel

,

confirm

:

this

.

onAccept

}),

alignment

:

DialogAlignment

.

Center

,

cancel

: ()

=>

{

console

.

log

(

"cancel"

)

// 点击蒙层的回调

},

autoCancel

:

true

,

// 允许点击蒙层关闭弹窗

customStyle

:

false

// 使用自定义样式

})

onCancel

() {

console

.

info

(

'取消'

)

}

build

() {

Column

({}) {

Button

(

' 自定义弹窗'

)

.

onClick

(()

=>

{

//打开弹窗

this

.

dialogController

.

open

();

})

}.

width

(

"100%"

).

height

(

"100%"

).

alignItems

(

HorizontalAlign

.

Center

).

justifyContent

(

FlexAlign

.

Center

)

}

}

/*

* Copyright (c) 2021 JianGuo Device Co., Ltd.

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

//通过CustomDialogController类显示自定义弹窗。

@

CustomDialog

struct

CustomDialogExample

{

controller

:

CustomDialogController

cancel

: ()

=>

void

confirm

: ()

=>

void

build

() {

Flex

({

justifyContent

:

FlexAlign

.

Center

,

alignItems

:

ItemAlign

.

Center

,

alignContent

:

FlexAlign

.

Center

}) {

Button

(

'取消'

).

fontSize

(

36

)

.

onClick

(()

=>

{

//方式二:关闭弹窗

this

.

controller

.

close

()

this

.

cancel

()

}).

backgroundColor

(

0xffffff

).

fontColor

(

Color

.

Black

)

Button

(

'确定'

).

fontSize

(

36

)

.

onClick

(()

=>

{

// this.controller.close()

this

.

confirm

()

}).

backgroundColor

(

0xffffff

).

fontColor

(

Color

.

Red

)

}.

margin

({

bottom

:

10

}).

width

(

"100%"

).

height

(

200

)

}

}

export

default

CustomDialogExample

上面就是一个简单的自定义弹窗

接下来看一下它的有关属性

CustomDialogController 定义了 open()close() 方法,它们说明如下:

open:打开对话框,如果对话框已经打开,则再次打开无效。

close:关闭对话框,如果对话框已经关闭,则再次关闭无效。

value:创建控制器需要的配置参数

  • CustomDialogControllerOptions

说明如下:

builder:创建自定义弹窗的构造器。

cancel:点击蒙层的事件回调。

autoCancel:是否允许点击遮障层退出。

alignment:弹窗在竖直方向上的对齐方式。

offset:弹窗相对 alignment 所在位置的偏移量。

customStyle:弹窗容器样式是否自定义。

源码

declare interface CustomDialogControllerOptions {

/**

* Custom builder function.

* @since 7

*/

builder: any;

/**

* Defines the cancel function.

* @since 7

*/

cancel?: () => void;

/**

* Defines if use auto cancel when click on the outside of the dialog.

* @since 7

*/

autoCancel?: boolean;

/**

* Defines the dialog alignment of the screen.

* @since 7

*/

alignment?: DialogAlignment;

/**

* Defines the dialog offset.

* @since 7

*/

offset?: Offset;

/**

* Defines if use costom style.

* @since 7

*/

customStyle?: boolean;

/**

* Grid count of dialog.

* @since 8

*/

gridCount?: number;

}

DialogAlignment的位置

名称 描述
Top 垂直顶部对齐。
Center 垂直居中对齐。
Bottom 垂直底部对齐。
Default 默认对齐。
TopStart8+ 左上对齐。
TopEnd8+ 右上对齐。
CenterStart8+ 左中对齐。
CenterEnd8+ 右中对齐。
BottomStart8+ 左下对齐。
BottomEnd8+ 右下对齐。

参考文档

自定义弹窗

语法糖

审核编辑:汤梓红
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • Component
    +关注

    关注

    0

    文章

    13

    浏览量

    8827
  • OpenHarmony
    +关注

    关注

    23

    文章

    3266

    浏览量

    15159
收藏 人收藏

    评论

    相关推荐

    基于ArkUI eTS开发的坚果食谱(NutRecipes)

    基于ArkUI eTS开发的坚果食谱(NutRecipes)
    的头像 发表于 08-18 08:23 1224次阅读
    基于<b class='flag-5'>ArkUI</b> <b class='flag-5'>eTS</b>开发的坚果食谱(NutRecipes)

    HarmonyOS开发实例:【自定义Emitter】

    使用[Emitter]实现事件的订阅和发布,使用[自定义弹窗]设置广告信息。
    的头像 发表于 04-14 11:37 613次阅读
    HarmonyOS开发实例:【<b class='flag-5'>自定义</b>Emitter】

    HarmonyOS开发案例:【弹窗使用】

    基于dialog和button组件,实现弹窗的几种自定义效果
    的头像 发表于 04-25 17:44 182次阅读
    HarmonyOS开发案例:【<b class='flag-5'>弹窗</b>使用】

    【学习打卡】一种eTS自定义icon图标组件的思路

    ;gt;在tetcl_icon/src/main/ets/components中创建TeIcon.ets文件,并编写如下代码:/** * @Description 自定义图标 * @date 2022
    发表于 07-26 15:07

    本周四晚19:00知识赋能第八期第2课丨ArkUI自定义组件

    本周四晚19:00知识赋能第八期第2课丨ArkUI自定义组件9月21日19:00~20:00,第八期知识赋能第2节直播就要开始啦!本次直播将为同学们带来涂鸦小游戏的趣味体验,让大家全面了解ArkUI
    发表于 09-21 17:13

    OpenHarmony应用开发之自定义弹窗

    本文转载自《OpenHarmony应用开发之自定义弹窗》,作者:zhushangyuan_ ​ 应用场景 在应用的使用和开发中,弹窗是一个很常见的场景,自定义
    发表于 09-06 14:40

    OpenHarmony自定义组件介绍

    一、创建自定义组件 在ArkUI中,UI显示的内容均为组件,由框架直接提供的称为系统组件,由开发者定义的称为自定义组件。在进行 UI 界面开发时,通常不是简单的将系统组件进行组合使用,
    发表于 09-25 15:36

    OpenHarmony自定义构建函数:@Builder装饰器

    前面章节介绍了如何创建一个自定义组件。该自定义组件内部UI结构固定,仅与使用方进行数据传递。ArkUI还提供了一种更轻量的UI元素复用机制@Builder,@Builder所装饰的函数遵循build
    发表于 09-26 16:36

    1602自定义字符

    1602液晶能够显示自定义字符,能够根据读者的具体情况显示自定义字符。
    发表于 01-20 15:43 1次下载

    三种自定义弹窗UI组件封装的实现

    鸿蒙已经提供了全局 UI 方法自定义弹窗,本文是基于基础的自定义弹窗来实现提示消息弹窗、确认弹窗
    的头像 发表于 03-30 09:28 2535次阅读

    自定义视图组件教程案例

    自定义组件 1.自定义组件-particles(粒子效果) 2.自定义组件- pulse(脉冲button效果) 3.自定义组件-progress(progress效果) 4.
    发表于 04-08 10:48 14次下载

    labview自定义控件

    labview自定义精美控件
    发表于 05-15 16:46 9次下载

    在OpenHarmony上如何使用不同的弹窗

    应用中经常用到弹窗,比如警告弹窗、日期选择弹窗、文本选择弹窗以及其他自定义弹窗等等。
    的头像 发表于 06-18 15:10 715次阅读
    在OpenHarmony上如何使用不同的<b class='flag-5'>弹窗</b>

    labview超快自定义控件制作和普通自定义控件制作

    labview超快自定义控件制作和普通自定义控件制作
    发表于 08-21 10:32 5次下载

    鸿蒙ArkUI实例:【自定义组件】

    组件是 OpenHarmony 页面最小显示单元,一个页面可由多个组件组合而成,也可只由一个组件组合而成,这些组件可以是ArkUI开发框架自带系统组件,比如 `Text` 、 `Button` 等,也可以是自定义组件,本节笔者简单介绍一下
    的头像 发表于 04-08 10:17 144次阅读