首页 > 其他分享 >『江鸟中原』鸿蒙---CustomDialog

『江鸟中原』鸿蒙---CustomDialog

时间:2023-12-09 12:32:05浏览次数:26  
标签:江鸟 自定义 CustomDialogController --- CustomDialog dialogController textValue 弹窗

我是中工的陈金灿,这是我的鸿蒙结课大作业,以下是我的作业报告。

自定义弹窗(CustomDialog)可用于广告、中奖、警告、软件更新等与用户交互响应操作。开发者可以通过CustomDialogController类显示自定义弹窗。具体用法请参考自定义弹窗。

一、创建自己第一个自定义弹窗

1.1 创建自定义弹窗

使用@CustomDialog装饰器装饰自定义弹窗。 @CustomDialog装饰器用于装饰自定义弹框,此装饰器内进行自定义内容(也就是弹框内容)。

@CustomDialog

struct CustomDialogExample {

 controller: CustomDialogController

 build() {

   Column() {

     Text('我是内容')

     .fontSize(20)

     .margin({ top: 10, bottom: 10 }) } } }

『江鸟中原』鸿蒙---CustomDialog_栅格

『江鸟中原』鸿蒙---CustomDialog_栅格_02

自定义弹窗中有两个需要我们写的:

controller:控制我们的自定义弹窗的

build函数:和我们主界面是一模一样的,没有区别

1.2 创建构造器,与装饰器呼应相连

在创建好自定义弹窗之后,我们肯定要弄出他的对象来吧,他的类型是CustomDialogController

dialogController: CustomDialogController = new CustomDialogController({

   builder: CustomDialogExample({}),

})

其中builder: CustomDialogExample({})为指定哪个自定义窗口

CustomDialogExample的参数为自定义窗口的参数

CustomDialogController参数详解

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

参数如下:

下面参数分别是参数名,参数类型,是否必填,参数作用

1、builder,CustomDialog,是,自定义弹窗内容构造器。

2、cancel,() => void,否,点击遮障层退出时的回调。

3、autoCancel,boolean,否,是否允许点击遮障层退出。默认值:true

4、alignment,DialogAlignment,否,弹窗在竖直方向上的对齐方式。默认值:DialogAlignment.Default

5、offset,Offset,否,弹窗相对alignment所在位置的偏移量。

6、customStyle,boolean,否,弹窗容器样式是否自定义。默认值:false,弹窗容器的宽度根据栅格系统自适应,不跟随子节点;高度自适应子节点,最大为窗口高度的90%;圆角为24vp。

7、gridCount,number,否,弹窗宽度占栅格宽度的个数。默认为按照窗口大小自适应,异常值按默认值处理,最大栅格数为系统最大栅格数。

1.3 点击与onClick事件绑定的组件使弹窗弹出

Button('click me')

 .onClick(() => {

   this.dialogController.open()

 })

使用open()函数即可打开自定义窗口

二、示例代码

// xxx.ets

@CustomDialog

struct CustomDialogExample {

 @Link textValue: string

 @Link inputValue: string

 controller: CustomDialogController

 // 若尝试在CustomDialog中传入多个其他的Controller,以实现在CustomDialog中打开另一个或另一些CustomDialog,那么此处需要将指向自己的controller放在最后

 cancel: () => void

 confirm: () => void

build() {

   Column() {

     Text('Change text').fontSize(20).margin({ top: 10, bottom: 10 })

     TextInput({ placeholder: '', text: this.textValue }).height(60).width('90%')

       .onChange((value: string) => {

         this.textValue = value

       })

     Text('Whether to change a text?').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.inputValue = this.textValue

           this.controller.close()

           this.confirm()

         }).backgroundColor(0xffffff).fontColor(Color.Red)

     }.margin({ bottom: 10 })

   }

   // dialog默认的borderRadius为24vp,如果需要使用border属性,请和borderRadius属性一起使用。

 }

} @Entry

@Component

struct CustomDialogUser {

 @State textValue: string = ''

 @State inputValue: string = 'click me'

 dialogController: CustomDialogController = new CustomDialogController({

   builder: CustomDialogExample({

     cancel: this.onCancel,

     confirm: this.onAccept,

     textValue: $textValue,

     inputValue: $inputValue

   }),

   cancel: this.existApp,

   autoCancel: true,

   alignment: DialogAlignment.Bottom,

   offset: { dx: 0, dy: -20 },

   gridCount: 4,

   customStyle: false

 })

// 在自定义组件即将析构销毁时将dialogControlle删除和置空

 aboutToDisappear() {

   delete this.dialogController, // 删除dialogController

   this.dialogController = undefined // 将dialogController置空

 }

onCancel() {

   console.info('Callback when the first button is clicked')

 }

onAccept() {

   console.info('Callback when the second button is clicked')

 }

existApp() {

   console.info('Click the callback in the blank area')

 }

 build() {

   Column() {

     Button(this.inputValue)

       .onClick(() => {

         if (this.dialogController != undefined) {

           this.dialogController.open()

         }

       }).backgroundColor(0x317aff)

   }.width('100%').margin({ top: 5 })

 }

}

标签:江鸟,自定义,CustomDialogController,---,CustomDialog,dialogController,textValue,弹窗
From: https://blog.51cto.com/u_16424040/8747166

相关文章

  • SQL PRIMARY KEY 约束- 唯一标识表中记录的关键约束
    SQLNOTNULL约束SQLNOTNULL约束用于强制确保列不接受NULL值。这意味着该字段始终包含一个值,而不允许插入新记录或更新记录时不提供此字段的值。在CREATETABLE时使用SQLNOTNULL以下SQL确保在创建"Persons"表时,“ID”、“LastName”和“FirstName”列将不接受......
  • 2023振兴杯-Crypto wp
    crypto1题目fromflagimportflagdefencrypt(x,y):key='zxb'result=''foriinrange(len(x)):result+=chr(ord(x[i])^ord(y[i])^ord(key[i%3]))returnresultx=flagy=flag[1:]+flag[......
  • SQL PRIMARY KEY 约束- 唯一标识表中记录的关键约束
    SQLNOTNULL约束SQLNOTNULL约束用于强制确保列不接受NULL值。这意味着该字段始终包含一个值,而不允许插入新记录或更新记录时不提供此字段的值。在CREATETABLE时使用SQLNOTNULL以下SQL确保在创建"Persons"表时,“ID”、“LastName”和“FirstName”列将不接......
  • 关于$ 1 - (cosx)^n$ ~ $\frac{n}{2}x^2$
    原理区先看例子:\(\lim_{{x\to0}}\frac{{1-\sqrt{\cosx}}}{{x^2}}\)\(=-\lim_{{x\to0}}\frac{{ln(1+(\sqrt{\cosx}-1)}}{{x^2}}\)//逆向代等价无穷小\(=-\lim_{{x\to0}}\frac{{ln(\sqrt{\cosx)}}}{{x^2}}\)\(=-\frac{1}{2}\lim_{{x\to0}}......
  • 2023-2024-1 20232327《网络空间安全导论》第五周学习总结
    2023-2024-120232327《网络空间安全导论》第五周学习总结教材学习内容总结1.信息内容安全是研究利用计算机从包含海量信息并且迅速变化的网络中对特定安全主题相关信息进行自动获取、识别和分析的技术;2.网络爬虫是按照一定规则,自动抓取有互联网信息的程序或脚本;3.信息过滤是......
  • P1854-DP【绿】
    首先通过这道题我收获了一个知识,那就是deque可以直接赋值,作用和vector类似就是复制一个一摸一样的deque,很好用,越来越发现deque眉清目秀了起来。以后deque可能是我最常用的STL结构了。毕竟queue、stack都用deque来实现明显更方便而且不会多占用什么空间的。一眼便能看出,这道题用记......
  • WGCLOUD常见问答 - 可以监测MAC地址信息吗
    可以的......
  • VUE框架CLI组件化列表信息完整功能实现------VUE框架
    CLI组件化列表信息完整功能实现<template><div><BugHeader:bugList="bugList":saveBugCallBack="saveBugCallBack"></BugHeader><BugList:updateDescCallBack="updateDescCallBack":selectAllRollbac......
  • JavaScript-history对象
    概述window.history属性指向History对象,它表示当前窗口的浏览历史。History对象保存了当前窗口访问过的所有页面网址。下面代码表示当前窗口一共访问过3个网址。window.history.length//3由于安全原因,浏览器不允许脚本读取这些地址,但是允许在地址之间导航。//后退到前一个网......
  • VUE框架CLI组件化组件的自定义事件和子组件向父组件传递数据的实现------VUE框架
    <template> <div> <!--内置函数的实现步骤--> <!--提供事件源,给事件源绑定事件,编写回调函数,将回调函数和事件进行绑定--> <!--等待事件的触发,事件触发执行回调函数--> <!--组件的自定义事件实现步骤--> <button@click="Hello()">你好</button> <!--给Us......