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

    浏览量

    9134
  • OpenHarmony
    +关注

    关注

    31

    文章

    3926

    浏览量

    20725
收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

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

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

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

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

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

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

    HarmonyOS开发案例:【 自定义弹窗

    基于ArkTS的声明式开发范式实现了三种不同的弹窗,第一种直接使用公共组件,后两种使用CustomDialogController实现自定义弹窗
    的头像 发表于 05-16 18:18 2299次阅读
    HarmonyOS开发案例:【 <b class='flag-5'>自定义</b><b class='flag-5'>弹窗</b>】

    HarmonyOS应用自定义键盘解决方案

    自定义键盘是一种替换系统默认键盘的解决方案,可实现键盘个性化交互。允许用户结合业务需求与操作习惯,对按键布局进行可视化重构、设置多功能组合键位,使输入更加便捷和舒适。在安全防护层面,自定义键盘可以
    的头像 发表于 06-05 14:19 1595次阅读

    OpenHarmony应用开发之自定义弹窗

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

    1602自定义字符

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

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

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

    自定义视图组件教程案例

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

    labview自定义控件

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

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

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

    自定义算子开发

    一个完整的自定义算子应用过程包括注册算子、算子实现、含自定义算子模型转换和运行含自定义op模型四个阶段。在大多数情况下,您的模型应该可以通过使用hb_mapper工具完成转换并顺利部署到地平线芯片上……
    的头像 发表于 04-07 16:11 4839次阅读
    <b class='flag-5'>自定义</b>算子开发

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

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

    鸿蒙ArkUI开发-应用添加弹窗

    弹窗是一种模态窗口,通常用来展示用户当前需要的或用户必须关注的信息或操作。在弹出框消失之前,用户无法操作其他界面内容。ArkUI为我们提供了丰富的弹窗功能
    的头像 发表于 01-24 17:22 1625次阅读
    鸿蒙<b class='flag-5'>ArkUI</b>开发-应用添加<b class='flag-5'>弹窗</b>

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

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