当前位置:   article > 正文

学习鸿蒙基础(3)

学习鸿蒙基础(3)

1.组件重用样式
如果每个组件的样式都需要单独设置,在开发过程中会出现大量代码在进行重复样式设置,虽然可以复制粘贴,但为了代码简洁性和后续方便维护,可以采用公共样式进行复用的装饰器@Styles。
@Styles装饰器可以将多条样式设置提炼成一个方法,直接在组件声明的位置调用。通过@Styles装饰器可以快速定义并复用自定义样式。用于快速定义并复用自定义样式。
当前@Styles仅支持[通用属性]和[通用事件]。
 @styles方法不支持参数
@Styles可以定义在组件内或全局,在全局定义时需在方法名前面添加function关键字,组件内定义时则不需要添加function关键字。
注意:
组件内@Styles的优先级高于全局@Styles.
框架优先找当前组件内的@Styles,如果找不到,则会全局查找。

  1. @Entry //入口
  2. @Component
  3. struct PageB_build_param {
  4. @State message: string = 'Hello World' //@State 数据改变了也刷新的标签
  5. @State username: string = ''
  6. @State password: string = ''
  7. @State widths:number =90
  8. build() {
  9. Row() {
  10. Column() {
  11. text({ title: "用户", valueStr:this.username, cb: (value:string) => {
  12. this.username=value
  13. } })
  14. text({ title:"密码",valueStr:this.password,cb:(value:string)=>{
  15. this.password=value
  16. } })
  17. Divider().margin(10)
  18. Row() {
  19. Button("登录")
  20. .fontSize(16)
  21. .myprivateStyle()
  22. .margin({ right: 10, left: 10 })
  23. .onClick(this.login.bind(this))
  24. Button("重置")
  25. .fontSize(16)
  26. .myprivateStyle()
  27. .margin({ left: 10, right: 10 })
  28. .onClick(this.reset.bind(this))
  29. }
  30. }
  31. .width('100%')
  32. .height('50%')
  33. }
  34. .height('100%')
  35. .width('100%')
  36. }
  37. @Styles//提取共有的属性 在struct内
  38. myprivateStyle(){
  39. .width(this.widths).height(50)
  40. }
  41. //登录
  42. login() {
  43. console.log(this.username+"----"+this.password)
  44. }
  45. reset() {
  46. this.username = ""
  47. this.password = ""
  48. this.widths=180
  49. }
  50. }
  51. @Styles//全局样式 提取
  52. function Mystyle(){
  53. .margin(10)
  54. .padding(10)
  55. .width(80)
  56. .height(50)
  57. .backgroundColor("#333333")
  58. }
  59. @Builder //全局自定义构件函数
  60. function text($$: { title: string,valueStr: string,cb: (value: string) => void }) {
  61. Row() {
  62. Text($$.title)
  63. .Mystyle()
  64. .fontSize(16)
  65. .fontColor(Color.White)
  66. .textAlign(TextAlign.Center)
  67. .border({
  68. width: 3,
  69. color: Color.Blue
  70. })
  71. .borderRadius(10)
  72. .fontWeight(FontWeight.Bold)
  73. TextInput({ text: $$.valueStr }).width(200).height(50)
  74. .fontSize(16).onChange((value: string) => {
  75. $$.cb(value)
  76. })
  77. }.alignItems(VerticalAlign.Center)
  78. }

2.扩展组件样式
@Extend(UIcomponentName) function functionName { ... }
·和@Styles不同,@Extend仅支持定义在全局,不支持在组件内部定义。
·和@Styles不同,@Extend支持封装指定的组件的私有属性和私有事件和预定义相同组件的@Extend的方法。·和@Styles不同,@Extend装饰的方法支持参数,开发者可以在调用时传递参数,调用遵循TS方法传值调用·@Extend装饰的方法的参数可以为function,作为Event事件的句柄。
@Extend的参数可以为状态变量,当状态变量改变时,UI可以正常的被刷新渲染。

  1. @Extend(Text)//全局对组件-Text进行扩展
  2. function MyTextExtend(text:string){
  3. .margin(10)
  4. .padding(10)
  5. .width(80)
  6. .height(50)
  7. .backgroundColor(text==="用户"? "#666666" : "#333333")
  8. .fontSize(16)
  9. .fontColor(Color.White)
  10. .textAlign(TextAlign.Center)
  11. .border({
  12. width: 3,
  13. color: Color.Blue
  14. })
  15. .borderRadius(10)
  16. .fontWeight(FontWeight.Bold)
  17. }
  18. @Extend(TextInput)//对组件textinput进行扩展
  19. function myTextInput(cb:(value :string )=> void){
  20. .width(200)
  21. .height(50)
  22. .fontSize(16)
  23. .onChange((value: string) => {
  24. cb(value)
  25. })
  26. }
  27. @Builder //全局自定义构件函数
  28. function text($$: { title: string,valueStr: string,cb: (value: string) => void }) {
  29. Row() {
  30. Text($$.title)
  31. .MyTextExtend($$.title)
  32. TextInput({ text: $$.valueStr })
  33. .myTextInput($$.cb)
  34. }.alignItems(VerticalAlign.Center)
  35. }

3.多态样式
stateStyles是属性方法,可以根据UI内部状态来设置样式,类似于css伪类,但语法不同。ArkUI提供以 下四种状态:
focused:获焦态。
normal: 正常态。
pressed: 按压态。
disabled: 不可用态。

  1. @Extend(TextInput)//对组件textinput进行扩展
  2. function myTextInput(cb:(value :string )=> void){
  3. .width(200)
  4. .height(50)
  5. .fontSize(16)
  6. .stateStyles({
  7. normal:normalStyle,
  8. pressed:pressStyle,
  9. focused:foucsStyle
  10. })
  11. .onChange((value: string) => {
  12. cb(value)
  13. })
  14. }
  15. @Styles
  16. function pressStyle(){
  17. .backgroundColor(Color.White)
  18. }
  19. @Styles
  20. function foucsStyle(){
  21. .backgroundColor(Color.Red)
  22. }
  23. @Styles
  24. function normalStyle(){
  25. .backgroundColor(Color.Green)
  26. }

4.循环渲染
ForEach(
arr: Array,
itemGenerator: (item: any, index?: number) => void,
keyGenerator?: (item: any, index?: number): string => string
)
在ForEach循环渲染过程中,系统会为每个数组元素生成一个唯一且持久的键值, 用于标识对应的组件。当这个键值变化时,ArkUI框架将视为该数组元素已被替换或修改,并会基于新的键值创建一个新的组件。
ForEach提供了一个名为keyGenerator的参数,这是一个函数,开发者可以通过它自定义键值的生成规则。如果开发者没有定义keyGenerator函数,则ArkUI框架会使用默认的键值生成函数,即(item: any, index: number)=> { return index+ '_' + JSON.stringify(item); }。

  1. @Entry
  2. @Component
  3. struct PageForEach {
  4. @State list: string [] = ["Hello World", "hello harmonyos", "hello my"]
  5. @State list1: Object [] = [{ id: 1, name: "房子" }, { id: 2, name: "车子" }, { id: 3, name: "票子" }]
  6. build() {
  7. Row() {
  8. Column() {
  9. ForEach(
  10. this.list, (item: string) => {
  11. Text(item).fontSize(26)
  12. })
  13. ForEach(this.list1, (item1: Object) => {
  14. Text(item1["name"]).fontSize(26)
  15. },item=>JSON.stringify(item))//用这个去作为唯一的key
  16. // List组件配合 ForEach超出屏幕可以滚动
  17. List(){
  18. ForEach(this.list1,(item1:Object)=>{
  19. ListItem(){
  20. Text(item1["name"]).fontSize(26).margin(10)
  21. }
  22. },item=>JSON.stringify(item))
  23. }.height(60).divider({
  24. startMargin:10,
  25. endMargin:10,
  26. strokeWidth:1,
  27. color:Color.Green
  28. })
  29. Button("切换").onClick(()=>{
  30. this.list1[1]["name"]="银山"
  31. this.list1.splice(0,1,{
  32. id:1,name:"金山"
  33. })
  34. })
  35. }
  36. .width('100%')
  37. }
  38. .height('100%')
  39. }
  40. }

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号