HarmonyOS 是否有个人信息页示例

HarmonyOS
2024-12-25 13:35:00
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

参考示例:

1、Index.ets

import { Mine } from './Mine'

@Entry
@Component
struct Index {
  private controller: TabsController = new TabsController()
  @State currentIndex: number = 0

  @Builder
  tabBuilder(index: number, img: Resource | string, name: string) {
    Column() {
      Image(img)
        .width(25)
        .height(25)
        .margin({ top: 4, bottom: 8 })
        .fillColor(this.currentIndex === index ? "#ff6089e3" : $r("sys.color.ohos_id_color_secondary"))
      Text(name)
        .fontSize(12)
        .fontColor(this.currentIndex === index ? "#ff6089e3" : $r('sys.color.ohos_id_color_text_secondary'))
    }
    .width('100%')
  }

  build() {
    Column() {
      Tabs({ barPosition: BarPosition.End, controller: this.controller }) {
        TabContent() {
          Column() {
            Text("首页")
              .fontSize(16)
              .fontColor($r('sys.color.ohos_id_color_text_primary'))
          }
          .width('100%')
          .height('100%')
        }.tabBar(this.tabBuilder(0, $r('app.media.ic_public_home'), '待办'))

        TabContent() {
          Column() {
            Column() {
              Text("基础服务")
                .fontSize(16)
                .fontColor($r('sys.color.ohos_id_color_text_primary'))
            }
            .width('100%')
            .height(50)
            .justifyContent(FlexAlign.Center)

            Divider()
              .color($r('sys.color.ohos_id_color_list_separator'))

            Column() {

            }
            .width('100%')
            .height('100%')
            .layoutWeight(1)
            .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
          }
          .width('100%')
          .height('100%')
        }.tabBar(this.tabBuilder(1, $r('app.media.ic_public_highlight'), '基础服务'))

        TabContent() {
          Column() {
            Column() {
              Text("工作台")
                .fontSize(16)
                .fontColor($r('sys.color.ohos_id_color_text_primary'))
            }
            .width('100%')
            .height(50)
            .justifyContent(FlexAlign.Center)

            Divider()
              .color($r('sys.color.ohos_id_color_list_separator'))

            Column() {

            }
            .width('100%')
            .height('100%')
            .layoutWeight(1)
            .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
          }
          .width('100%')
          .height('100%')
        }.tabBar(this.tabBuilder(2, $r('app.media.ic_public_home'), '工作台'))

        TabContent() {
          Column() {
            Column() {
              Text("通讯录")
                .fontSize(16)
                .fontColor($r('sys.color.ohos_id_color_text_primary'))
            }
            .width('100%')
            .height(50)
            .justifyContent(FlexAlign.Center)

            Divider()
              .color($r('sys.color.ohos_id_color_list_separator'))

            Column() {

            }
            .width('100%')
            .height('100%')
            .layoutWeight(1)
            .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
          }
          .width('100%')
          .height('100%')
        }.tabBar(this.tabBuilder(3, $r('app.media.ic_public_highlight'), '通讯录'))

        TabContent() {
          Column() {
            Mine()
          }
          .width('100%')
          .height('100%')
        }.tabBar(this.tabBuilder(4, $r('app.media.ic_public_contacts'), '我的'))
      }
      .barHeight(60)
      .onChange((index: number) => {
        this.currentIndex = index
      })
      .onAnimationStart((index: number, targetIndex: number, event: TabsAnimationEvent) => {
        this.currentIndex = targetIndex
      })
    }
    .height('100%')
    .width('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.

2、Mine.ets

import { ArrayDescItem, DescItem, PafList } from '../components/List'

@Observed
class UserNameInfo {
  userAvatar: Resource | PixelMap
  userName: ResourceStr
  userInfo: ResourceStr

  constructor(userAvatar: Resource | PixelMap, userName: ResourceStr, userInfo: ResourceStr) {
    this.userAvatar = userAvatar
    this.userName = userName
    this.userInfo = userInfo
  }
}


@Component
export struct Mine {
  @State userNameInfo: UserNameInfo = new UserNameInfo($r('app.media.ic_appassist_gift'), "用户名", "后端开发")
  @State userOperateInfo: Array<ArrayDescItem> = []

  aboutToAppear(): void {
    this.userOperateInfo = [
      [
        new DescItem('个人信息', '', $r('app.media.ic_appassist_gift')).onClick(() => {
          console.log(`个人信息`)
        })
      ],
      [new DescItem('退出登录', '', $r('app.media.ic_appassist_ranking')).onClick(() => {
        console.log(`退出登录`)
      })
      ],
    ]
  }

  build() {
    Column() {
      Row() {
        Row() {
          Image(this.userNameInfo.userAvatar)
            .width(60)
            .height(60)
          Column() {
            Text(this.userNameInfo.userName)
              .fontSize(16)
              .fontColor($r("sys.color.ohos_id_color_text_primary"))
            Text(this.userNameInfo.userInfo)
              .fontSize(16)
              .fontColor($r("sys.color.ohos_id_color_text_primary"))
          }
          .margin({ left: 12 })
          .alignItems(HorizontalAlign.Start)
        }

        Row() {
          Image($r('app.media.ic_public_collect'))
            .width(15)
            .height(15)
            .fillColor(Color.White)
          Text("已验证")
            .fontSize(12)
            .fontColor(Color.White)
            .margin({ left: 8 })
        }
        .width(80)
        .height(24)
        .borderRadius(20)
        .justifyContent(FlexAlign.Center)
        .backgroundColor("#6fd8d3")
      }
      .width('100%')
      .height(120)
      .backgroundColor(Color.White)
      .alignItems(VerticalAlign.Center)
      .padding({ left: 25, right: 25 })
      .justifyContent(FlexAlign.SpaceBetween)

      Column() {
        PafList({
          items: this.userOperateInfo,
          isMultiCard: true
        })
      }
      .margin({ top: 12 })
    }
    .height('100%')
    .width('100%')
    .backgroundColor($r('sys.color.ohos_id_color_sub_background'))
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.

3、component/List.ets

import { Util } from '../common/Util'

@Observed
export class DescItem {
  public label: string | Resource
  public desc: string | Resource
  public icon: string | PixelMap | Resource
  public isDisabled?: boolean = false
  public clickEvent: (event?: ClickEvent) => void = () => {
  }
  public hasRightArrow: boolean = false
  public rightDesc: ResourceStr

  constructor(label: string | Resource, desc: string | Resource = '', icon: string | PixelMap | Resource = '',
    isDisabled: boolean = false,
    rightDesc: ResourceStr = '') {
    this.label = label
    this.desc = desc
    this.icon = icon
    this.isDisabled = isDisabled
    this.rightDesc = rightDesc
  }

  public onClick(event: (event?: ClickEvent) => void): DescItem {
    this.clickEvent = event
    this.hasRightArrow = true
    return this
  }
}

@Observed
export class ArrayDescItem extends Array<DescItem> {
}

@Component
export struct PafList {
  @Link items: Array<Array<DescItem>> | Array<DescItem>
  isMultiCard: boolean = false
  @State private leftMargin: number = 12
  @State hoverBackgroundColor: Array<object> = []
  @State private leftTextWidthEnums: Record<string, number> = {}
  @State private listItemWidth: number = 0

  @Styles
  pressedStyles() {
    .backgroundColor($r("sys.color.ohos_id_color_click_effect"))
    .borderWidth(0)
    .borderColor(null)
  }

  @Styles
  disabledStyles() {
    .opacity($r('sys.float.ohos_id_alpha_disabled'))
  }

  @Styles
  focusedStyles() {
    .backgroundColor(null)
    .borderWidth(2)
    .borderColor($r("sys.color.ohos_id_color_focused_outline"))
  }

  @Styles
  clickedStyles() {
    .backgroundColor($r("sys.color.ohos_id_color_emphasize"))
    .borderWidth(0)
    .borderColor(null)
  }

  aboutToAppear(): void {
    if (this.isMultiCard) {
      this.items.forEach(() => {
        this.hoverBackgroundColor.push(new Object())
      });
    } else {
      this.hoverBackgroundColor.push(new Object())
    }
  }

  @Builder
  buildItemWithIcon(item: DescItem, index: number) {
    RelativeContainer() {
      Row() {
        Image(item.icon)
          .width(24)
          .height(24)
          .margin({ right: 16 })
          .draggable(false)
          .autoResize(false)
          .interpolation(ImageInterpolation.High)
          .alignRules({
            center: { anchor: "__container__", align: VerticalAlign.Center },
            left: { anchor: "__container__", align: HorizontalAlign.Start }
          })
          .id('icon')

        Text(item.label)
          .fontColor($r('sys.color.ohos_id_color_text_primary'))
          .fontSize($r('sys.float.ohos_id_text_size_sub_title2'))
          .fontFamily($r('sys.string.ohos_id_text_font_family_medium'))
          .fontWeight(FontWeight.Medium)
          .maxLines(2)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
          .height(21)
          .constraintSize({
            minHeight: 48,
            maxWidth: Util.notEmpty(item.rightDesc) ? 'calc(66% - 56vp)' :
              item.hasRightArrow ? 'calc(100% - 48vp)' : '100%'
          })
          .margin({ top: 8, bottom: 8, })
          .alignRules({
            center: { anchor: '__container__', align: VerticalAlign.Center },
            left: { anchor: 'icon', align: HorizontalAlign.End },
          })
          .id('label')
      }
      .onAreaChange((oldValue: Area, newValue: Area) => {
        this.leftTextWidthEnums[index] = (newValue.width as number)
      })
      .margin({
        right: Util.notEmpty(item.rightDesc) ? 16 : item.hasRightArrow ? 8 : 0
      })
      .id('leftSide')
      .alignRules({
        center: { anchor: '__container__', align: VerticalAlign.Center },
        left: { anchor: "__container__", align: HorizontalAlign.Start }
      })

      if (Util.notEmpty(item.rightDesc) || item.hasRightArrow) {
        Row() {
          this.IconAndText(item)
        }
        .width(this.listItemWidth - this.leftTextWidthEnums[index] -
          (Util.notEmpty(item.rightDesc) ? 16 : item.hasRightArrow ? 8 : 0))
        .constraintSize({
          minWidth: Util.notEmpty(item.rightDesc) ? '33%' : ''
        })
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          right: { anchor: "__container__", align: HorizontalAlign.End },
          left: { anchor: "label", align: HorizontalAlign.End },
        })
        .id('rightSide')
      }
    }
    .width('100%')
    .height(56)
  }

  @Builder
  buildItemNoIcon(item: DescItem, index: number) {
    RelativeContainer() {
      Column() {
        Text(item.label)
          .fontColor($r('sys.color.ohos_id_color_text_primary'))
          .fontSize($r('sys.float.ohos_id_text_size_sub_title2'))
          .fontFamily($r('sys.string.ohos_id_text_font_family_medium'))
          .fontWeight(FontWeight.Medium)
          .lineHeight(22)
          .maxLines(1)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
          .margin({ top: item.desc ? 10 : 13, bottom: item.desc ? 0 : 14 })
          .alignRules({
            top: { anchor: "__container__", align: VerticalAlign.Top },
            left: { anchor: "__container__", align: HorizontalAlign.Start }
          })
          .id('icon')
        if (Util.notEmpty(item.desc)) {
          Text(item.desc)
            .fontColor($r('sys.color.ohos_id_color_text_secondary'))
            .fontSize($r('sys.float.ohos_id_text_size_sub_title3'))
            .fontFamily($r('sys.string.ohos_id_text_font_family_regular'))
            .fontWeight(FontWeight.Regular)
            .height(19)
            .margin({ top: 2, bottom: 11 })
            .alignRules({
              top: { anchor: 'icon', align: VerticalAlign.Bottom },
              left: { anchor: "__container__", align: HorizontalAlign.Start }
            })
            .id('desc')
        }
      }
      .onAreaChange((oldValue: Area, newValue: Area) => {
        this.leftTextWidthEnums[index] = (newValue.width as number + 16)
      })
      .constraintSize({
        minHeight: 48,
        maxWidth: Util.notEmpty(item.rightDesc) ? 'calc(66% - 16vp)' : item.hasRightArrow ? 'calc(100% - 12vp)' : '100%'
      })
      .margin({ right: Util.notEmpty(item.rightDesc) || item.hasRightArrow ? 16 : 0 })
      .alignItems(HorizontalAlign.Start)
      .alignRules({
        top: { anchor: "__container__", align: VerticalAlign.Top },
        left: { anchor: "__container__", align: HorizontalAlign.Start }
      })
      .id('leftSide')

      if (Util.notEmpty(item.rightDesc) || item.hasRightArrow) {
        Column() {
          this.IconAndText(item)
        }
        .width(this.listItemWidth - this.leftTextWidthEnums[index])
        .constraintSize({
          minWidth: Util.notEmpty(item.rightDesc) ? '33%' : ''
        })
        .id('rightSide')
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          right: { anchor: "__container__", align: HorizontalAlign.End },
          left: { anchor: "icon", align: HorizontalAlign.End },
        })
      }
    }
    .width('100%')
    .height(item.desc ? 64 : 48)
  }

  @Builder
  IconAndText(item: DescItem) {
    RelativeContainer() {
      if (Util.notEmpty(item.rightDesc)) {
        Text(item.rightDesc)
          .fontColor($r('sys.color.ohos_id_color_text_secondary'))
          .fontSize($r('sys.float.ohos_id_text_size_sub_title3'))
          .fontFamily($r('sys.string.ohos_id_text_font_family_regular'))
          .fontWeight(FontWeight.Regular)
          .maxLines(2)
          .textOverflow({ overflow: TextOverflow.Ellipsis })
          .textAlign(TextAlign.End)
          .constraintSize({
            minHeight: 48
          })
          .lineHeight(19)
          .alignRules({
            center: { anchor: '__container__', align: VerticalAlign.Center },
            left: { anchor: '__container__', align: HorizontalAlign.Start },
            right: item.hasRightArrow ? { anchor: "rightArrow", align: HorizontalAlign.Start } :
              { anchor: '__container__', align: HorizontalAlign.End }
          })
          .id('rightText')
      }
      if (item.hasRightArrow) {
        Image($r('app.media.ic_public_arrow_right'))
          .fillColor($r('sys.color.ohos_id_color_fourth'))
          .width(12)
          .height(24)
          .margin({ left: item.rightDesc ? 4 : 0 })
          .draggable(false)
          .autoResize(false)
          .interpolation(ImageInterpolation.High)
          .alignRules({
            center: { anchor: '__container__', align: VerticalAlign.Center },
            right: { anchor: "__container__", align: HorizontalAlign.End }
          })
          .id('rightArrow')
      }
    }
    .width('100%')
    .height(item.desc ? 64 : 48)
  }

  judge(item: DescItem): boolean {
    if (Util.notEmpty(item.icon)) {
      this.leftMargin = 52
      return true
    } else {
      this.leftMargin = 12
      return false
    }
  }

  @Builder
  buildItem(item: DescItem, index: number) {
    Row() {
      if (this.judge(item)) {
        this.buildItemWithIcon(item, index)
      } else {
        this.buildItemNoIcon(item, index)
      }
    }
    .onAreaChange((oldValue: Area, newValue: Area) => {
      this.listItemWidth = newValue.width as number
    })
    .margin({ left: 8, right: 8 })
    .alignItems(VerticalAlign.Center)
  }

  @Builder
  buildCard(card: Array<DescItem> | Array<Array<DescItem>>, cardIndex: number) {
    ListItemGroup() {
      ForEach(card, (item: DescItem, index) => {
        ListItem() {
          this.buildItem(item, index)
        }
        .onClick((event: ClickEvent) => {
          if (item.clickEvent && item.hasRightArrow) {
            item.clickEvent(event)
          }
        })
        .margin({ left: 4, right: 4 })
        .constraintSize({ minHeight: Util.notEmpty(item.icon) ? 56 : Util.notEmpty(item.desc) ? 64 : 48 })
        .borderRadius(14)
        .enabled(!item.isDisabled)
        .backgroundColor(this.hoverBackgroundColor[cardIndex][index])
        .stateStyles({
          pressed: this.pressedStyles,
          focused: this.focusedStyles,
          disabled: this.disabledStyles,
        })
        .onHover((isHover: boolean) => {
          let temp: Array<object> = [...this.hoverBackgroundColor]
          if (isHover) {
            temp[cardIndex][index] = $r("sys.color.ohos_id_color_hover")
          } else {
            temp[cardIndex][index] = $r('sys.color.ohos_id_color_list_card_bg')
          }
          this.hoverBackgroundColor = [...temp]
        })
      })
    }
    .backgroundColor($r('sys.color.ohos_id_color_list_card_bg'))
    .padding({ top: 4, bottom: 4 })
    .divider({
      strokeWidth: '1px',
      color: $r('sys.color.ohos_id_color_list_separator'),
    })
    .width("100%")
  }

  build() {
    Column() {
      List({ space: this.isMultiCard ? 12 : 0 }) {
        if (this.isMultiCard) {
          ForEach(this.items, (item: Array<DescItem>, index: number) => {
            this.buildCard(item, index)
          })
        } else {
          this.buildCard(this.items, 0)
        }
      }
      .listDirection(Axis.Vertical)
      .scrollBar(BarState.Off)
      .edgeEffect(EdgeEffect.None)
      .chainAnimation(false)
      .width("100%")
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
  • 270.
  • 271.
  • 272.
  • 273.
  • 274.
  • 275.
  • 276.
  • 277.
  • 278.
  • 279.
  • 280.
  • 281.
  • 282.
  • 283.
  • 284.
  • 285.
  • 286.
  • 287.
  • 288.
  • 289.
  • 290.
  • 291.
  • 292.
  • 293.
  • 294.
  • 295.
  • 296.
  • 297.
  • 298.
  • 299.
  • 300.
  • 301.
  • 302.
  • 303.
  • 304.
  • 305.
  • 306.
  • 307.
  • 308.
  • 309.
  • 310.
  • 311.
  • 312.
  • 313.
  • 314.
  • 315.
  • 316.
  • 317.
  • 318.
  • 319.
  • 320.
  • 321.
  • 322.
  • 323.
  • 324.
  • 325.
  • 326.
  • 327.
  • 328.
  • 329.
  • 330.
  • 331.
  • 332.
  • 333.
  • 334.
  • 335.
  • 336.
  • 337.
  • 338.
  • 339.
  • 340.
  • 341.
  • 342.
  • 343.
  • 344.
  • 345.
  • 346.
  • 347.
  • 348.

4、common/Util.ets

export class Util {
  static notEmpty(param: Color | number | string | Resource | PixelMap | ResourceStr): boolean {
    if (!param) {
      return false
    }
    if (typeof param === 'string') {
      return param.length > 0
    }
    return true
  }

  // hex转rgba
  static hexToRgba(hex: string, alpha: number): string {
    let rgb = hex.slice(1)
    if (rgb.length == 8) {
      rgb = rgb.slice(2, 8)
    }
    let rgba = [
      Number.parseInt("0x" + rgb.slice(0, 2)),
      Number.parseInt("0x" + rgb.slice(2, 4)),
      Number.parseInt("0x" + rgb.slice(4, 6)),
      alpha
    ]
    return "rgba(" + rgba.toString() + ")"
  }

  // 十进制转rgba
  static decimalToRgba(decimal: number, alpha: number): string {
    let bitThree = decimal & 0xff;
    let bitTwo = decimal >> 8 & 0xff;
    let bitOne = decimal >> 16 & 0xff;
    decimal = decimal >> 24 & 0xff;
    return `rgba(${bitOne}, ${bitTwo}, ${bitThree}, ${alpha})`
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
分享
微博
QQ
微信
回复
2024-12-25 15:27:30
相关问题
HarmonyOS 是否navigation示例
476浏览 • 1回复 待解决
HarmonyOS 是否区号选择示例
574浏览 • 1回复 待解决
HarmonyOS 是否图片滑块验证示例
723浏览 • 1回复 待解决
证书锁定功能示例哪些?
1426浏览 • 1回复 待解决
HarmonyOS 输入框的dialog示例
747浏览 • 1回复 待解决