|
| 1 | +import { hilog } from '@kit.PerformanceAnalysisKit' |
| 2 | + |
| 3 | +@Entry |
| 4 | +@Component |
| 5 | +struct Index { |
| 6 | + @State currentTime: string = '' |
| 7 | + @State isConnected: boolean = false |
| 8 | + @State systemInfo: SystemInfo | null = null |
| 9 | + |
| 10 | + aboutToAppear() { |
| 11 | + this.updateTime() |
| 12 | + this.checkNetworkStatus() |
| 13 | + this.getSystemInfo() |
| 14 | + |
| 15 | + setInterval(() => { |
| 16 | + this.updateTime() |
| 17 | + }, 1000) |
| 18 | + } |
| 19 | + |
| 20 | + build() { |
| 21 | + Column() { |
| 22 | + // 顶部状态栏 |
| 23 | + this.buildStatusBar() |
| 24 | + |
| 25 | + // 主要内容区域 |
| 26 | + this.buildMainContent() |
| 27 | + |
| 28 | + // 底部操作区域 |
| 29 | + this.buildActionArea() |
| 30 | + } |
| 31 | + .width('100%') |
| 32 | + .height('100%') |
| 33 | + .backgroundColor('#F5F5F5') |
| 34 | + .padding(16) |
| 35 | + } |
| 36 | + |
| 37 | + @Builder |
| 38 | + buildStatusBar() { |
| 39 | + Row() { |
| 40 | + Text('My App') |
| 41 | + .fontSize(18) |
| 42 | + .fontWeight(FontWeight.Bold) |
| 43 | + .fontColor('#333333') |
| 44 | + |
| 45 | + Blank() |
| 46 | + |
| 47 | + Row() { |
| 48 | + Image("icon.png") |
| 49 | + .width(16) |
| 50 | + .height(16) |
| 51 | + .fillColor(this.isConnected ? '#00C851' : '#FF4444') |
| 52 | + |
| 53 | + Text(this.isConnected ? 'Connected' : 'Disconnected') |
| 54 | + .fontSize(12) |
| 55 | + .fontColor(this.isConnected ? '#00C851' : '#FF4444') |
| 56 | + .margin({ left: 4 }) |
| 57 | + } |
| 58 | + } |
| 59 | + .width('100%') |
| 60 | + .padding({ bottom: 16 }) |
| 61 | + } |
| 62 | + |
| 63 | + @Builder |
| 64 | + buildMainContent() { |
| 65 | + Column() { |
| 66 | + // 时间显示 |
| 67 | + Text(this.currentTime) |
| 68 | + .fontSize(32) |
| 69 | + .fontWeight(FontWeight.Bold) |
| 70 | + .fontColor('#007AFF') |
| 71 | + .margin({ bottom: 24 }) |
| 72 | + |
| 73 | + // 系统信息卡片 |
| 74 | + if (this.systemInfo) { |
| 75 | + this.buildSystemInfoCard() |
| 76 | + } |
| 77 | + |
| 78 | + // 功能按钮区域 |
| 79 | + this.buildFeatureButtons() |
| 80 | + } |
| 81 | + .width('100%') |
| 82 | + .layoutWeight(1) |
| 83 | + .justifyContent(FlexAlign.Center) |
| 84 | + } |
| 85 | + |
| 86 | + @Builder |
| 87 | + buildSystemInfoCard() { |
| 88 | + Column() { |
| 89 | + Text('System information') |
| 90 | + .fontSize(16) |
| 91 | + .fontWeight(FontWeight.Medium) |
| 92 | + .margin({ bottom: 12 }) |
| 93 | + |
| 94 | + Row() { |
| 95 | + Text('Device model:') |
| 96 | + .fontSize(14) |
| 97 | + .fontColor('#666666') |
| 98 | + |
| 99 | + Text(this.systemInfo?.deviceModel || 'Unknown') |
| 100 | + .fontSize(14) |
| 101 | + .fontColor('#333333') |
| 102 | + .margin({ left: 8 }) |
| 103 | + } |
| 104 | + .width('100%') |
| 105 | + .justifyContent(FlexAlign.SpaceBetween) |
| 106 | + .margin({ bottom: 8 }) |
| 107 | + |
| 108 | + Row() { |
| 109 | + Text('System version:') |
| 110 | + .fontSize(14) |
| 111 | + .fontColor('#666666') |
| 112 | + |
| 113 | + Text(this.systemInfo?.osVersion || 'Unknown') |
| 114 | + .fontSize(14) |
| 115 | + .fontColor('#333333') |
| 116 | + .margin({ left: 8 }) |
| 117 | + } |
| 118 | + .width('100%') |
| 119 | + .justifyContent(FlexAlign.SpaceBetween) |
| 120 | + } |
| 121 | + .width('100%') |
| 122 | + .backgroundColor(Color.White) |
| 123 | + .borderRadius(12) |
| 124 | + .padding(16) |
| 125 | + .margin({ bottom: 24 }) |
| 126 | + } |
| 127 | + |
| 128 | + @Builder |
| 129 | + buildFeatureButtons() { |
| 130 | + Row() { |
| 131 | + Button('Refresh status') |
| 132 | + .onClick(() => { |
| 133 | + this.checkNetworkStatus() |
| 134 | + this.getSystemInfo() |
| 135 | + }) |
| 136 | + .backgroundColor('#007AFF') |
| 137 | + .borderRadius(8) |
| 138 | + .layoutWeight(1) |
| 139 | + .margin({ right: 8 }) |
| 140 | + |
| 141 | + Button('Settings') |
| 142 | + .onClick(() => { |
| 143 | + hilog.info(0x0000, 'testTag', 'Navigate to settings...') |
| 144 | + }) |
| 145 | + .backgroundColor(Color.Transparent) |
| 146 | + .border({ width: 1, color: '#007AFF' }) |
| 147 | + .borderRadius(8) |
| 148 | + .layoutWeight(1) |
| 149 | + } |
| 150 | + .width('100%') |
| 151 | + } |
| 152 | + |
| 153 | + @Builder |
| 154 | + buildActionArea() { |
| 155 | + Row() { |
| 156 | + Text('My footer') |
| 157 | + .fontSize(12) |
| 158 | + .fontColor('#999999') |
| 159 | + } |
| 160 | + .width('100%') |
| 161 | + .justifyContent(FlexAlign.Center) |
| 162 | + } |
| 163 | + |
| 164 | + private updateTime() { |
| 165 | + const now = new Date() |
| 166 | + this.currentTime = now.toLocaleTimeString('zh-CN', { |
| 167 | + hour12: false, |
| 168 | + hour: '2-digit', |
| 169 | + minute: '2-digit', |
| 170 | + second: '2-digit' |
| 171 | + }) |
| 172 | + } |
| 173 | + |
| 174 | + private checkNetworkStatus() { |
| 175 | + this.isConnected = Math.random() > 0.3 |
| 176 | + } |
| 177 | + |
| 178 | + private getSystemInfo() { |
| 179 | + this.systemInfo = { |
| 180 | + deviceModel: 'HarmonyOS Device', |
| 181 | + osVersion: 'HarmonyOS 6.0.0', |
| 182 | + apiLevel: 20 |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +interface SystemInfo { |
| 188 | + deviceModel: string |
| 189 | + osVersion: string |
| 190 | + apiLevel: number |
| 191 | +} |
0 commit comments