当前位置:   article > 正文

鸿蒙开发常用自定义组件:TextPicker_鸿蒙textpicker

鸿蒙textpicker

本篇详细介绍基于常用组件 TextPicker 上,扩展一些样式自定义组件

这里先来了解一下自定义组件成员变量初始化,TextPicker 组件的属性和方法,下面先显示此帖子 Demo 的效果:

图片

自定义组件

组件的成员变量可以通过两种方式初始化

本地初始化,例如:

@State counter: Counter = new Counter()

在构造组件时通过构造参数初始化,例如:

MyComponent({counter$myCounter})

具体允许哪种方式取决于状态变量的装饰器:

图片

通过构造函数方法初始化成员变量,需要遵循如下规则:

图片

TextPicker 组件

参数:

图片

事件:

图片

Demo 介绍

创建一个新的 eTS 文件,用来编写自定义 TextPicker 组件,代码如下:

  1. @Component
  2. export struct CustomTextPicker {
  3.   @State selectnumber = 0
  4.   @State ranges: string[] | Resource = undefined
  5.   private changeCallback: (valuestringindexnumber=> void
  6.   build() {
  7.     TextPicker({ range: this.ranges, selected: this.select })
  8.       .layoutWeight(1)
  9.       .linearGradient({
  10.         angle: 0,
  11.         direction: GradientDirection.Top,
  12.         colors: [[0xfdfdfd, 0.0], [0xe0e0e00.5], [0xfdfdfd, 1]],
  13.       })
  14.       .onChange((valuestringindexnumber=> {
  15.         this.changeCallback(valueindex)
  16.       })
  17.   }
  18. }

通过 import 引用自定义 TextPicker 组件到要使用的页面,创建 Sample 页面,里面显示一个按钮和一个文本,按钮点击打开一个对话框。

对话框里面有四个自定义 TextPicker 组件,用餐数据源是通过资源文件引用,其它数据源是通过定义变量或直接给出。

TextPicker 组件 rang 参数支持字符串数组和 Resource 资源文件,Sample 代码如下:

首先引用自定义组件:

import { CustomTextPicker } from '../components/CustomTextPicker'

在自定义对话框使用:

  1. @CustomDialog
  2. struct Record {
  3.   private controller: CustomDialogController
  4.   private mileTimeLabels:Resource = $r('app.strarray.mealTime_labels')
  5.   private foodWeights: string[] = ['150''200''250''300''350']
  6.   @Link mileTime: string
  7.   @Link foodWeight: string
  8.   @Link gender: string
  9.   @Link age: number
  10.   onChangeMileTimeCallback(valuestringindexnumber) {
  11.     console.log('xx '+value + " index: " + index)
  12.     AppStorage.SetOrCreate<string>("mileTime"value)
  13.   }
  14.   onChangeFoodWeightCallback(valuestringindexnumber) {
  15.     console.log('xx '+value + " index: " + index)
  16.     AppStorage.SetOrCreate<string>("foodWeight"value)
  17.   }
  18.   onChangeGenderCallback(valuestringindexnumber) {
  19.     console.log('xx '+value + " index: " + index)
  20.     AppStorage.SetOrCreate<string>("gender"value)
  21.   }
  22.   onChangeAgeCallback(valuestringindexnumber) {
  23.     console.log('xx '+value + " index: " + index)
  24.     AppStorage.SetOrCreate<string>("age"value)
  25.   }
  26.   build() {
  27.     Column({space10}) {
  28.       Row({space5}) {
  29.         Text('用餐:').fontSize(20)
  30.         CustomTextPicker({select0, ranges: this.mileTimeLabels,
  31.           changeCallback: this.onChangeMileTimeCallback})
  32.         Text('重量:').fontSize(20)
  33.         CustomTextPicker({select1, ranges: this.foodWeights,
  34.           changeCallback: this.onChangeFoodWeightCallback})
  35.       }.height(140)
  36.       Row({space5}) {
  37.         Text('性别:').fontSize(20)
  38.         CustomTextPicker({
  39.             select0, ranges: ['保留','男''女'],
  40.             changeCallback: this.onChangeGenderCallback
  41.             })
  42.         Text('年龄:').fontSize(20)
  43.         CustomTextPicker({
  44.           select0, ranges: ['16','17''18','19''20','21''22','23''24','25''26'],
  45.           changeCallback: this.onChangeAgeCallback
  46.         })
  47.       }.height(140)
  48.       Button('完成', { type: ButtonType.Capsule, stateEffect: true })
  49.         .height(43)
  50.         .width('100%')
  51.         .margin({ top33left72right72 })
  52.         .backgroundColor('#73CD57')
  53.         .onClick(() => {
  54.           this.mileTime = AppStorage.Get("mileTime")
  55.           this.foodWeight = AppStorage.Get("foodWeight")
  56.           this.gender = AppStorage.Get("gender")
  57.           this.age = AppStorage.Get("age")
  58.           this.controller.close()
  59.         })
  60.     }
  61.     .cardStyle()
  62.     .height(420)
  63.     .width('90%')
  64.   }
  65. }
  66. @Styles function cardStyle() {
  67.   .height('100%')
  68.   .padding({ top20right20left20 })
  69.   .backgroundColor(Color.White)
  70.   .borderRadius(12)
  71. }

Sample 页面完整代码如下:

  1. import { CustomTextPicker } from '../components/CustomTextPicker'
  2. @Entry
  3. @Component
  4. struct SampleCustomTextPicker {
  5.   @State mileTime: string = '?'
  6.   @State foodWeight: string = '?'
  7.   @State gender: string = '?'
  8.   @State age: number = 16
  9.   dialogController: CustomDialogController = new CustomDialogController({
  10.     builder: Record({mileTime: $mileTime, foodWeight: $foodWeight, gender: $gender, age: $age}),
  11.     autoCancel: true,
  12.     alignment: DialogAlignment.Bottom,
  13.     offset: { dx: 0, dy: -20 },
  14.     customStyle: true
  15.   })
  16.   build() {
  17.     Column({ space10 }) {
  18.       Button('打开对话框', { type: ButtonType.Capsule, stateEffect: true })
  19.         .height(42)
  20.         .width('80%')
  21.         .margin({ top32bottom32 })
  22.         .backgroundColor('#73CD57')
  23.         .onClick(() => {
  24.           this.dialogController.open()
  25.         })
  26.       Text('用餐时间:' + this.mileTime)
  27.         .fontSize(18)
  28.       Text('重量:' + this.foodWeight)
  29.         .fontSize(18)
  30.       Row({space10}) {
  31.         Text('性别:' + this.gender)
  32.           .fontSize(18)
  33.         Text('年龄:' + this.age)
  34.           .fontSize(18)
  35.       }
  36.     }
  37.     .width('100%')
  38.   }
  39. }
  40. @CustomDialog
  41. struct Record {
  42.   private controller: CustomDialogController
  43.   private mileTimeLabels:Resource = $r('app.strarray.mealTime_labels')
  44.   private foodWeights: string[] = ['150''200''250''300''350']
  45.   @Link mileTime: string
  46.   @Link foodWeight: string
  47.   @Link gender: string
  48.   @Link age: number
  49.   onChangeMileTimeCallback(valuestringindexnumber) {
  50.     console.log('xx '+value + " index: " + index)
  51.     AppStorage.SetOrCreate<string>("mileTime"value)
  52.   }
  53.   onChangeFoodWeightCallback(valuestringindexnumber) {
  54.     console.log('xx '+value + " index: " + index)
  55.     AppStorage.SetOrCreate<string>("foodWeight"value)
  56.   }
  57.   onChangeGenderCallback(valuestringindexnumber) {
  58.     console.log('xx '+value + " index: " + index)
  59.     AppStorage.SetOrCreate<string>("gender"value)
  60.   }
  61.   onChangeAgeCallback(valuestringindexnumber) {
  62.     console.log('xx '+value + " index: " + index)
  63.     AppStorage.SetOrCreate<string>("age"value)
  64.   }
  65.   build() {
  66.     Column({space10}) {
  67.       Row({space5}) {
  68.         Text('用餐:').fontSize(20)
  69.         CustomTextPicker({select0, ranges: this.mileTimeLabels,
  70.           changeCallback: this.onChangeMileTimeCallback})
  71.         Text('重量:').fontSize(20)
  72.         CustomTextPicker({select1, ranges: this.foodWeights,
  73.           changeCallback: this.onChangeFoodWeightCallback})
  74.       }.height(140)
  75.       Row({space5}) {
  76.         Text('性别:').fontSize(20)
  77.         CustomTextPicker({
  78.             select0, ranges: ['保留','男''女'],
  79.             changeCallback: this.onChangeGenderCallback
  80.             })
  81.         Text('年龄:').fontSize(20)
  82.         CustomTextPicker({
  83.           select0, ranges: ['16','17''18','19''20','21''22','23''24','25''26'],
  84.           changeCallback: this.onChangeAgeCallback
  85.         })
  86.       }.height(140)
  87.       Button('完成', { type: ButtonType.Capsule, stateEffect: true })
  88.         .height(43)
  89.         .width('100%')
  90.         .margin({ top33left72right72 })
  91.         .backgroundColor('#73CD57')
  92.         .onClick(() => {
  93.           this.mileTime = AppStorage.Get("mileTime")
  94.           this.foodWeight = AppStorage.Get("foodWeight")
  95.           this.gender = AppStorage.Get("gender")
  96.           this.age = AppStorage.Get("age")
  97.           this.controller.close()
  98.         })
  99.     }
  100.     .cardStyle()
  101.     .height(420)
  102.     .width('90%')
  103.   }
  104. }
  105. @Styles function cardStyle() {
  106.   .height('100%')
  107.   .padding({ top20right20left20 })
  108.   .backgroundColor(Color.White)
  109.   .borderRadius(12)
  110. }

总结

通过此帖,可以简单学会自定义组件,如何引用使用,在健康饮食 APP 里,已经完成了一小部分内容,下来继续一个个组件完成,然后拼装就出来一个健康饮食 APP 了。

最后,为了能够让大家跟上互联网时代的技术迭代,赶上互联网开发人员寒冬期间一波红利,在这里跟大家分享一下我自己近期学习心得以及参考网上资料整理出的一份最新版的鸿蒙学习提升资料,有需要的小伙伴自行领取,限时开源,先到先得~无套路领取!!!

 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/920659

推荐阅读
相关标签