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) }
猜你喜欢
- 1月前(兰州旅游文化产业发展有限公司)甘肃省兰州市2023年乡村旅游暨A级旅游景区管理工作培训班开班
- 1月前(中旅酒店 维景)中旅酒店首次AI数字人直播亮相南京维景
- 1月前(安徽民航君澜大饭店装饰设计招标)集东方文化气息,品徽派隽美风韵----安徽民航君澜大饭店静待绽放
- 1月前(东北地区全域旅游)东北三省一区宣传贯彻研学旅游行业标准
- 1月前(纳米比亚旅游报价)纳米比亚旅游局2024年中国推介会圆满落幕
- 1月前(澳涞山庄见证北欧零碳到中国实践,世界十佳环境保护城市榜单发布)澳涞山庄见证北欧零碳到中国实践,世界十佳环境保护城市榜单发布
- 1月前(安岚度假村及酒店推出"山海之约"目的地婚礼计划)安岚度假村及酒店推出"山海之约"目的地婚礼计划
- 1月前(大黄山景区高质量发展联盟成立多少年)大黄山景区高质量发展联盟成立
- 1月前(筑格集团有限公司)洲际酒店集团旗下筑格酒店品牌正式亮相大中华区
- 1月前(冬日生活还没安排?上抖音一键打包北方花式过冬精彩)冬日生活还没安排?上抖音一键打包北方花式过冬精彩
网友评论
- 搜索
- 最新文章
- (2020广州车展哈弗)你的猛龙 独一无二 哈弗猛龙广州车展闪耀登场
- (哈弗新能源suv2019款)智能科技颠覆出行体验 哈弗重塑新能源越野SUV价值认知
- (2021款全新哈弗h5自动四驱报价)新哈弗H5再赴保障之旅,无惧冰雪护航哈弗全民电四驱挑战赛
- (海南航空现况怎样)用一场直播找到市场扩张新渠道,海南航空做对了什么?
- (visa jcb 日本)优惠面面俱到 JCB信用卡邀您畅玩日本冰雪季
- (第三届“堡里有年味·回村过大年”民俗花灯会活动)第三届“堡里有年味·回村过大年”民俗花灯会活动
- (展示非遗魅力 长安启源助力铜梁龙舞出征)展示非遗魅力 长安启源助力铜梁龙舞出征
- (阿斯塔纳航空公司)阿斯塔纳航空机队飞机数量增至50架
- (北京香港航班动态查询)香港快运航空北京大兴新航线今日首航
- (我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉)我在港航“呵护”飞机 每一次安全着陆就是最好的荣誉
- 热门文章