ArkUI如何自定义弹窗(eTS)

自定义弹窗其实也是比较简单的,通过customdialogcontroller类就可以显示自定义弹窗。
接下来我们通过代码来看一下
大家也都用过@entry,@component等弹窗的话,只要用@customdialog就可以
先来预览一下我实现的效果:
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+ 右下对齐。
参考文档 自定义弹窗
语法糖


怎么做SGS科特迪瓦COC清关证书多少钱
新唐科技W79E632A主板简介
芯片巨头高通将面临“拆分”和“重组”?
中小企业IT主管如何为数字化转型提供支持
高频电缆和普通电缆二者之间的区别是什么
ArkUI如何自定义弹窗(eTS)
到底哪家强?三星与华为的战争
周正宁:WebRTC+AV1将会是未来技术的风向标
AI+医疗政策助推,促行业快速发展
无线充电越来越火 其实诺基亚早在2012年就推出了
基于DBL结构的嵌入式64kb SRAM的低功耗设计
iPhone7plus双摄像头惊艳 双摄产业全面解读
可能或将能升级安卓8.0的手机,看看你的在不在里面?
英特尔14nm的产能不足,或将会影响SSD市场
解析工程师所熟知的CAN、UART、IIC、USB等协议解码
GPD Win3掌上游戏PC即将众筹:起售价799美元
中国2020上半年SDS市场分析
英特尔 Agilex 7 FPGA 产品现已量产
R&S提供整车无源天线测试的全套解决方案助力自动驾驶汽车成为现实
在allegro中进行更换元件封装技巧应用