HarmonyOS系统开发ArkTS入门案例及组件(三)

下一章

目录

一、声明式UI

二、ArkTs 快速入门案例

三、组件

四、渲染控制


一、声明式UI

  声明式UI就是一种编写用户界面的范式或方式、
  ArArkTS 在继承了Typescript语法的基础上,主要扩展了声明式UI开发相关的能力。
  声明式UI开发范式大致流程:定义页面状态、描述页面效果、改变状态

1、定义页面状态
   分析和定义页面的各种状态,声明状态变量表示不同的状态。如显示开灯关灯状态
   @State isOn:boolean =false; //默认关灯   
   @State 表示是状态变量,只有状态变量修改,页面才会更新。   
   
2、描述界面显示效果
   描述界面在不同状态下的显示效果,如开灯效果和关灯效果
   关灯显示关灯照片
   开灯显示开灯照片


3、改变状态
   点击按钮改变状态,如开灯状态和关灯状态
   点击关灯isOn:boolean =false,显示关灯照片
   点击开灯isOn:boolean =true ,显示开灯照片
   

二、ArkTs 快速入门案例


    @Entry
    @Component
    struct LightPage {
      @State isOn: boolean = false;
      build() {
        Column({ space: 20 }) {
          if (this.isOn) {
            Image('pages/helloworld/light/solution/images/img_light.png')
              .height(300)
              .width(300)
              .borderRadius(20)
          } else {
            Image('pages/helloworld/light/solution/images/img_dark.png')
              .height(300)
              .width(300)
              .borderRadius(20)
          }
          Row({ space: 50 }) {
            Button('关灯')
              .onClick(() => {
                this.isOn = false
              })
            Button('开灯')
              .onClick(() => {
                this.isOn = true;
              })
          }
        }
        .height('100%')
        .width('100%')
        .justifyContent(FlexAlign.Center)
      }
    }

三、组件

1、声明式组件
    组件由 组件名称、组件参数、组件属性方法、组件事件方法、子组件组成。
    组件名称(组件参数){ 子组件 }.组件属性方法().组件属性方法().组件事件方法
    ============================================================

      组件 Button

      Button({ type: ButtonType.Circle }) {
        子组件
        Image('pages/light/images/img_light.png')
          .width(25)
          .height(25)
      }
      .witdh(50)
      .height(50)
      .backgroundColor(Color.Red)
      .onclick(() => {
        console.log('创建组件 onclick 测试');
      })

组件案例
===============================================================

  @Entry
      @Component
      struct Index {
        @State isOn: boolean = false;
        @State isDel: boolean = false;
      
        build() {
          Column({ space: 10 }) {
            if (this.isDel) {
              Image('pages/delete/images/man.jpg').width(300).height(300);
            } else {
              Image('pages/delete/images/girl.jpg').width(300).height(300);
            }
            Row({ space: 20 }) {
              Button('还原')
                .onClick(() => {
                  this.isDel = false;
                });
              Button() {
                Image('pages/delete/images/ic_delete.png')
                  .width(25).height(25)
              }
              .type(ButtonType.Circle)
              .width(50).height(50).backgroundColor(Color.Red)
              .onClick(() => {
                this.isDel = true;
              });
            }
          }
          .width('100%')
          .height("100%")
          .justifyContent(FlexAlign.Center)
      
        }
      }

      
2、自定义组件
   提高代码复用性
   @Component 装饰器:装饰 struct 关键字声明的数据结构
   @Entry 装饰器:标识该组件为组件树的根节点,也就是一个页面入口组件
   struct:ArkTS用于自定义组件或者自定义定义弹窗的关键字,与结构类相似
   build() build() 用于声明自定义组件的UI结构
   组件属性:定义组件的属性

自定义组件案例
============================================================
    SwitchButton.ets文件:

    @Component
    export struct SwitchButton {
      color: Color = Color.Blue
    
      build() {
        Button() {
          Image('pages/on_off/images/icon_switch.png')
            .width(25).height(25)
        }
        .type(ButtonType.Circle)
        .width(50)
        .height(50)
        .backgroundColor(this.color)
      }
    }

   ============================================================
    on_off.etc文件:  

  import { SwitchButton } from './SwitchButton';
    @Entry
    @Component
    struct on_off {
      @State isOn: boolean = false;
      @State isDel: boolean = false;
    
      build() {
        Column({ space: 30 }) {
          if (this.isDel) {
            Image('pages/on_off/images/man.jpg').width(300).height(300);
          } else {
            Image('pages/on_off/images/girl.jpg').width(300).height(300);
          }
          Row({ space: 30 }) {
            SwitchButton({ color: Color.Green })
              .onClick(() => {
                this.isDel = false;
              });
    
            SwitchButton({ color: Color.Red })
              .onClick(() => {
                this.isDel = true;
              });
          }
        }
        .width('100%')
        .height("100%")
        .justifyContent(FlexAlign.Center)
    
      }
    }

   

四、渲染控制


if...else  和 Foreach循环


if...else案例    =========================================================================
    PlayButton.ets文件:
    ---------------------

    @Component
    export struct PlayButton {
      color: Color = Color.White;
      isShow: boolean = true;
      build() {
        Button() {
          Image('pages/condition/images/' + (this.isShow ? "ic_play.png" : "ic_pause.png"))
            .width(50).height(50)
        }
        .width(100)
        .height(100)
        .backgroundColor(this.color)
      }
    }

    paly.ets文件:
    ---------------------

    import { PlayButton } from './playButton';
    
    @Entry
    @Component
    struct Play {
      @State isRunning: boolean = true;
    
      build() {
        Column({ space: 10 }) {
          if (this.isRunning) {
            Image('pages/condition/images/girl.jpg').width(300).height(300);
          } else {
            Image('pages/condition/images/man.jpg').width(300).height(300);
          }
    
          Row() {
            if (this.isRunning) {
              PlayButton({ isShow: this.isRunning })
                .onClick(() => {
                  this.isRunning = false;
                })
            }else {
              PlayButton({ isShow: this.isRunning })
                .onClick(() => {
                  this.isRunning = true;
                })
            }
          }
        }
        .width('100%')
        .height("100%")
        .justifyContent(FlexAlign.Center)
      }
    }

    Foreach循环案例  
    =======================================================================================
    FruitButton.ets文件:
    ---------------------

    @Component
    export struct PlayButton {
      color: Color = Color.White;
      isShow: boolean = true;
      build() {
        Button() {
          Image('pages/condition/images/' + (this.isShow ? "ic_play.png" : "ic_pause.png"))
            .width(50).height(50)
        }
        .width(100)
        .height(100)
        .backgroundColor(this.color)
      }
    }


    
    fruit.ets文件:
    ---------------------
 

   import { PlayButton } from './playButton';
    
    @Entry
    @Component
    struct Fruit {
      @State options: string[] = ['桃子', '苹果', '香蕉', '香梨', '荔枝'];
      @State answer: string = '_______?';
      @State color: Color = Color.Black;
      @State fontSize: number = 25;
    
      build() {
        Column({ space: 23 }) {
          Row({ space: 15 }) {
            Text('你最喜欢的水果是')
              .fontColor(Color.Black)
              .fontSize(25)
              .fontWeight(FontWeight.Bold)
            Text(this.answer)
              .fontColor(this.color)
              .fontSize(this.fontSize)
              .fontWeight(FontWeight.Bold)
          }
    
          ForEach(this.options, (item: string) => {
            Column({ space: 40 }) {
              Button(item)
                .fontSize(32)
                .width(180)
                .height(90)
                .backgroundColor(Color.Orange)
                .onClick(() => {
                  this.answer = item;
                  this.color = Color.Red;
                  this.fontSize=44;
                })
            }
    
          })
        }
        .width('100%')
        .height("100%")
        .justifyContent(FlexAlign.Center)
      }
    }


    
    
    

  • 25
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ARKTS温度控制界面是一种用于控制和监测温度的用户界面。它可以通过图形化显示温度数据和各种控制选项来帮助用户轻松管理和调整温度设置。 首先,ARKTS温度控制界面提供了实时温度监测功能。用户可以在界面上实时查看当前温度,并根据需要进行相应的调整。这使得用户能够及时了解到温度变化,从而采取有效的控制措施。 其次,界面上还提供了温度调节选项。用户可以通过界面上的控制按钮或滑块来调整相应的温度设置。比如,用户可以通过界面上的调整按钮来提高或降低温度、设置温度范围等。这样,用户可以根据自身需求和环境要求进行灵活的温度调节。 此外,ARKTS温度控制界面还具备报警功能。当温度超出设定的范围时,界面会提醒用户及时采取措施。这种报警功能对于保障设备和材料的安全性至关重要。用户可以在界面上设置报警阈值,并选择接收报警通知的方式,如声音、短信或电子邮件等。 最后,ARKTS温度控制界面具有可视化的数据显示功能。它可以将温度数据以图表或曲线的形式展示出来,使用户能够直观地了解温度的变化趋势。这样,用户可以更好地判断温度是否稳定,是否需要进行调整。 总之,ARKTS温度控制界面通过实时监测、温度调节、报警和数据可视化等功能,为用户提供了便捷、灵活和高效的温度控制方式。它在工业、实验室和家庭等多个领域都有广泛应用,帮助用户实现温度的精确控制和调节。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值