当前位置: 首页 > news >正文

学习鸿蒙基础(2)

arkts是声名式UI

DevEcoStudio的右侧预览器可以预览。有个TT的图标可以看布局的大小。和html的布局浏览很像。

上图布局对应的代码:


@Entry //入口
@Component
struct Index {@State message: string = 'Hello Harmonyos' //@State 数据改变了也刷新的标签build() {Row() {Column() {Text(this.message).fontSize(30).margin(10).padding(20).backgroundColor("#333333").fontColor(Color.White).border({width:3,color:Color.Blue}).borderRadius(10).onClick(() => {console.log("点击了text")this.message = "text"}).fontWeight(FontWeight.Bold)Divider().margin(10)Button("click").width(100).height(50).onClick(this.read.bind(this))}.width('100%').height('50%')}.height('100%').width('90%')}// 方法多的话写到这里read() {console.log("我是button的点击事件")this.message = "button"}
}

新建页面的时候选择page。就会主动把该页面添加在路由中。

此处就是新建的页面的路由。和微信小程序是一样一样的。要加到这个page上。

1、自定义组件内,自定义构件函数。

@Builder 注释来实现

@Entry //入口
@Component
struct PageB {@State message: string = 'Hello World' //@State 数据改变了也刷新的标签build() {Row() {Column() {this.TextLabel("账号")this.TextLabel("密码")Divider().margin(10)Button("click").width(100).height(50).onClick(this.read.bind(this))}.width('100%').height('50%')}.height('100%').width('90%')}// 方法多的话写到这里read() {console.log("我是button的点击事件")this.message = "button"}@Builder//自定义组件内,自定义构件函数TextLabel(title:string ){Text(title+this.message).fontSize(16).margin(10).padding(10).width(200).height(50).backgroundColor("#333333").fontColor(Color.White).border({width:3,color:Color.Blue}).borderRadius(10).onClick(() => {this.message ="admin"}).fontWeight(FontWeight.Bold)}
}

2.全局自定义构建函数

@Entry //入口
@Component
struct PageB {@State message: string = 'Hello World' //@State 数据改变了也刷新的标签build() {Row() {Column() {TextLabel("账号")TextLabel("密码")Divider().margin(10)Button("click").width(100).height(50).onClick(this.read.bind(this))}.width('100%').height('50%')}.height('100%').width('90%')}// 方法多的话写到这里read() {console.log("我是button的点击事件")this.message = "button"}
}
@Builder//全局自定义构件函数
function TextLabel(title:string ){Text(title+this.message).fontSize(16).margin(10).padding(10).width(200).height(50).backgroundColor("#333333").fontColor(Color.White).border({width:3,color:Color.Blue}).borderRadius(10).onClick(() => {this.message ="admin"//在全局不建议去修改message}).fontWeight(FontWeight.Bold)
}

3、全局自定义函数实现简单的登录功能。采用引用传值,函数回调的方法。

@Entry //入口
@Component
struct PageB_build_param {@State message: string = 'Hello World' //@State 数据改变了也刷新的标签@State username: string = ''@State password: string = ''build() {Row() {Column() {text({ title: "用户", valueStr:this.username, cb: (value:string) => {this.username=value} })text({ title:"密码",valueStr:this.password,cb:(value:string)=>{this.password=value} })Divider().margin(10)Row() {Button("登录").fontSize(16).width(100).height(50).margin({ right: 10, left: 10 }).onClick(this.login.bind(this))Button("重置").fontSize(16).width(100).height(50).margin({ left: 10, right: 10 }).onClick(this.reset.bind(this))}}.width('100%').height('50%')}.height('100%').width('100%')}//登录login() {console.log(this.username+"----"+this.password)}reset() {this.username = ""this.password = ""}
}@Builder //全局自定义构件函数
function text($$: { title: string,valueStr: string,cb: (value: string) => void }) {Row() {Text($$.title).fontSize(16).margin(10).padding(10).width(80).textAlign(TextAlign.Center).height(50).backgroundColor("#333333").fontColor(Color.White).border({width: 3,color: Color.Blue}).borderRadius(10).fontWeight(FontWeight.Bold)TextInput({ text: $$.valueStr }).width(200).height(50).fontSize(16).onChange((value: string) => {$$.cb(value)})}.alignItems(VerticalAlign.Center)}

http://www.lryc.cn/news/292018.html

相关文章:

  • 2024年美国大学生数学建模竞赛思路与源代码【2024美赛C题】
  • Windows11搭建GPU版本PyTorch环境详细过程
  • Springboot项目基础配置:小白也能快速上手!
  • 20240127在ubuntu20.04.6下配置whisper
  • C# 递归执行顺序
  • go 实现暴力破解数独
  • go语言-字符串处理常用函数
  • DevOps落地笔记-05|非功能需求:如何有效关注非功能需求
  • vs 撤销本地 commit 并保留更改
  • 深度解读NVMe计算存储协议-1
  • CHS_06.2.3.4_2+用信号量实现进程互斥、同步、前驱关系
  • Web实战丨基于Django的简单网页计数器
  • mysql8安装基础操作(一)
  • MIT6.5830 实验0
  • 【简便方法和积累】pytest 单元测试框架中便捷安装插件和执行问题
  • Zabbix数据库分离与邮件报警
  • mybatisplus-多数据源配置
  • 微信小程序(二十八)网络请求数据进行列表渲染
  • ubuntu22.04 安装conda
  • W801学习笔记十:HLK-W801制作学习机/NES游戏机(总结)
  • 《HTML 简易速速上手小册》第6章:HTML 语义与结构(2024 最新版)
  • 分析HarmonyOS应用/服务的CPU活动性能
  • Linux:理解信号量以及内核中的三种通信方式
  • 【ArcGIS微课1000例】0100:ArcGIS for CAD软件下载与安装(附安装包)
  • Django模型(一)
  • 【ChatGPT】文本向量化与余弦相似度:揭开文本处理的神秘面纱(5)
  • 计算机中丢失mfc100u.dll怎么解决,详细解析mfc100u.dll丢失的解决方法
  • 代码随想录 Leetcode617.合并二叉树
  • 知识价值1-github站点域名
  • 【PyRestTest】进行Benchmarking测试