1e41f4b71Sopenharmony_ci# TextClock 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciTextClock组件通过文本将当前系统时间显示在设备上。支持不同时区的时间显示,最高精度到秒级。 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci在组件不可见时时间变动将停止,组件的可见状态基于[onVisibleAreaChange](./ts-universal-component-visible-area-change-event.md#onvisibleareachange)处理,可见阈值ratios大于0即视为可见状态。 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci>**说明:** 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci>该组件从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci## 子组件 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci无 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## 接口 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ciTextClock(options?: TextClockOptions) 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci**参数:** 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 29e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 30e41f4b71Sopenharmony_ci| options | [TextClockOptions](#textclockoptions14对象说明)| 否 | 通过文本显示当前系统时间的组件参数。 | 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci## TextClockOptions<sup>14+</sup>对象说明 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**卡片能力:** 从API version 14开始,该接口支持在ArkTS卡片中使用。 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 41e41f4b71Sopenharmony_ci| -------------- | -------- | ------ | --------------------------------------------------------------------------- | 42e41f4b71Sopenharmony_ci| timeZoneOffset | number | 否 | 设置时区偏移量。<br>取值范围为[-14, 12],表示东十二区到西十二区,其中负值表示东时区,正值表示西时区,比如东八区为-8。设置值为该取值范围内的浮点数时会进行取整,舍弃小数部分。<br>对横跨国际日界线的国家或地区,用-13(UTC+13)和-14(UTC+14)来保证整个国家或者区域处在相同的时间,当设置的值不在取值范围内时,将使用当前系统的时区偏移量。<br/>默认值:当前系统的时区偏移量 <br/>设置值为{ 9.5, 3.5, -3.5, -4.5, -5.5, -5.75, -6.5, -9.5, -10.5, -12.75 }集合中的浮点数时不进行取整。| 43e41f4b71Sopenharmony_ci| controller | [TextClockController](#textclockcontroller) | 否 | 绑定一个控制器,用来控制文本时钟的状态。| 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci## 属性 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci除支持[通用属性](ts-universal-attributes-size.md)外,还支持以下属性: 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci### format 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ciformat(value: string) 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci设置显示时间格式,如“yyyy/MM/dd”、“yyyy-MM-dd”。 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ciy:年(yyyy表示完整年份,yy表示年份后两位)<br />M:月(若想使用01月则使用MM)<br />d:日(若想使用01日则使用dd)<br />E:星期(若想使用星期六则使用EEEE,若想使用周六则使用E、EE、EEE)H:小时(24小时制) h:小时(12小时制) <br/>m:分钟<br/>s:秒<br/>SS:厘秒(format中S个数<3,全部按厘秒处理)<br />SSS:毫秒(format中S个数>=3,全部按毫秒处理)<br/>a:上午/下午(当设置小时制式为H时,该参数不生效) 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci日期间隔符:"年月日"、“/”、"-"、"."(可以自定义间隔符样式,间隔符不可以为字母,汉字则作为间隔符处理) 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci允许自行拼接组合显示格式,即:年、月、日、星期、时、分、秒、毫秒可拆分为子元素,可自行排布组合。时间更新频率最高为一秒一次,不建议单独设置厘秒和毫秒格式。 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci当设置无效字母时(非上述字母被认为是无效字母),该字母会被忽略。如果format全是无效字母时,显示格式跟随系统语言和系统小时制。例如系统语言为中文时,12小时制显示格式为yyyy/MM/dd aa hh:mm:ss.SSS,24小时制显示格式为yyyy/MM/dd HH:mm:ss.SSS。 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci若format为空或者undefined,则使用默认值。 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci非卡片中默认值:12小时制:aa hh:mm:ss,24小时制:HH:mm:ss。<br />卡片中默认值:12小时制:hh:mm,24小时制:HH:mm 。<br />卡片中使用时,最小时间单位为分钟。如果设置格式中有秒或厘秒按默认值处理。 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci**参数:** 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 76e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------- | 77e41f4b71Sopenharmony_ci| value | string | 是 | 显示时间格式。 | 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ci以下是format输入的格式样式及对应的显示效果: 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci| 输入格式 | 显示效果 | 82e41f4b71Sopenharmony_ci| ----------------------- | ------------------- | 83e41f4b71Sopenharmony_ci| yyyy年M月d日 EEEE | 2023年2月4日 星期六 | 84e41f4b71Sopenharmony_ci| yyyy年M月d日 | 2023年2月4日 | 85e41f4b71Sopenharmony_ci| M月d日 EEEE | 2月4日 星期六 | 86e41f4b71Sopenharmony_ci| M月d日 | 2月4日 | 87e41f4b71Sopenharmony_ci| MM/dd/yyyy | 02/04/2023 | 88e41f4b71Sopenharmony_ci| EEEE MM月dd日 | 星期六 02月04日 | 89e41f4b71Sopenharmony_ci| yyyy(完整年份) | 2023年 | 90e41f4b71Sopenharmony_ci| yy(年份后两位) | 23年 | 91e41f4b71Sopenharmony_ci| MM(完整月份) | 02月 | 92e41f4b71Sopenharmony_ci| M(月份) | 2月 | 93e41f4b71Sopenharmony_ci| dd(完整日期) | 04日 | 94e41f4b71Sopenharmony_ci| d(日期) | 4日 | 95e41f4b71Sopenharmony_ci| EEEE(完整星期) | 星期六 | 96e41f4b71Sopenharmony_ci| E、EE、EEE(简写星期) | 周六 | 97e41f4b71Sopenharmony_ci| yyyy年M月d日 | 2023年2月4日 | 98e41f4b71Sopenharmony_ci| yyyy/M/d | 2023/2/4 | 99e41f4b71Sopenharmony_ci| yyyy-M-d | 2023-2-4 | 100e41f4b71Sopenharmony_ci| yyyy.M.d | 2023.2.4 | 101e41f4b71Sopenharmony_ci| HH:mm:ss(时:分:秒) | 17:00:04 | 102e41f4b71Sopenharmony_ci| aa hh:mm:ss(时:分:秒) | 上午 5:00:04 | 103e41f4b71Sopenharmony_ci| hh:mm:ss(时:分:秒) | 5:00:04 | 104e41f4b71Sopenharmony_ci| HH:mm(时:分) | 17:00 | 105e41f4b71Sopenharmony_ci| aa hh:mm(时:分) | 上午 5:00 | 106e41f4b71Sopenharmony_ci| hh:mm(时:分) | 5:00 | 107e41f4b71Sopenharmony_ci| mm:ss(分:秒) | 00:04 | 108e41f4b71Sopenharmony_ci| mm:ss.SS(分:秒.厘秒) | 00:04.91 | 109e41f4b71Sopenharmony_ci| mm:ss.SSS(分:秒.毫秒) | 00:04.536 | 110e41f4b71Sopenharmony_ci| hh:mm:ss aa | 5:00:04 上午 | 111e41f4b71Sopenharmony_ci| HH | 17 | 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci### fontColor 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_cifontColor(value: ResourceColor) 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_ci设置字体颜色。 118e41f4b71Sopenharmony_ci 119e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci**参数:** 126e41f4b71Sopenharmony_ci 127e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 128e41f4b71Sopenharmony_ci| ------ | ------------------------------------------ | ---- | ---------- | 129e41f4b71Sopenharmony_ci| value | [ResourceColor](ts-types.md#resourcecolor) | 是 | 字体颜色。 | 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci### fontSize 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_cifontSize(value: Length) 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci设置字体大小。 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 140e41f4b71Sopenharmony_ci 141e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci**参数:** 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 146e41f4b71Sopenharmony_ci| ------ | ---------------------------- | ---- | ------------------------------------------------------------ | 147e41f4b71Sopenharmony_ci| value | [Length](ts-types.md#length) | 是 | 字体大小。fontSize为number类型时,使用fp单位。字体默认大小16fp。不支持设置百分比字符串。 | 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci### fontStyle 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_cifontStyle(value: FontStyle) 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ci设置字体样式。 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci**参数:** 162e41f4b71Sopenharmony_ci 163e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 164e41f4b71Sopenharmony_ci| ------ | ------------------------------------------- | ---- | --------------------------------------- | 165e41f4b71Sopenharmony_ci| value | [FontStyle](ts-appendix-enums.md#fontstyle) | 是 | 字体样式。<br/>默认值:FontStyle.Normal | 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci### fontWeight 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_cifontWeight(value: number | FontWeight | string) 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ci设置文本的字体粗细,设置过大可能会在不同字体下有截断。 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 174e41f4b71Sopenharmony_ci 175e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 176e41f4b71Sopenharmony_ci 177e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci**参数:** 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 182e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 183e41f4b71Sopenharmony_ci| value | number \| [FontWeight](ts-appendix-enums.md#fontweight) \| string | 是 | 文本的字体粗细,number类型取值[100, 900],取值间隔为100,默认为400,取值越大,字体越粗。string类型仅支持number类型取值的字符串形式,例如"400",以及"bold"、"bolder"、"lighter"、"regular"、"medium",分别对应FontWeight中相应的枚举值。<br/>默认值:FontWeight.Normal | 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci### fontFamily 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_cifontFamily(value: ResourceStr) 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci设置字体列表。 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 196e41f4b71Sopenharmony_ci 197e41f4b71Sopenharmony_ci**参数:** 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 200e41f4b71Sopenharmony_ci| ------ | -------------------------------------- | ---- | ------------------------------------------------------------ | 201e41f4b71Sopenharmony_ci| value | [ResourceStr](ts-types.md#resourcestr) | 是 | 字体列表。默认字体'HarmonyOS Sans'。<br>应用当前支持'HarmonyOS Sans'字体和[注册自定义字体](../js-apis-font.md)。<br>卡片当前仅支持'HarmonyOS Sans'字体。 | 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ci### textShadow<sup>11+</sup> 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_citextShadow(value: ShadowOptions | Array<ShadowOptions>) 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci设置文字阴影效果。该接口支持以数组形式入参,实现多重文字阴影。不支持fill字段, 不支持智能取色模式。 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 214e41f4b71Sopenharmony_ci 215e41f4b71Sopenharmony_ci**参数:** 216e41f4b71Sopenharmony_ci 217e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 218e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | ---- | -------------- | 219e41f4b71Sopenharmony_ci| value | [ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions对象说明) \| Array<[ShadowOptions](ts-universal-attributes-image-effect.md#shadowoptions对象说明)> | 是 | 文字阴影效果。 | 220e41f4b71Sopenharmony_ci 221e41f4b71Sopenharmony_ci### fontFeature<sup>11+</sup> 222e41f4b71Sopenharmony_ci 223e41f4b71Sopenharmony_cifontFeature(value: string) 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci设置文字特性效果,比如数字等宽的特性。 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci格式为:normal \| \<feature-tag-value\> 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_ci\<feature-tag-value\>的格式为:\<string\> \[ \<integer\> \| on \| off ] 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ci\<feature-tag-value\>的个数可以有多个,中间用','隔开。 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci例如,使用等宽时钟数字的输入格式为:"ss01" on。 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 238e41f4b71Sopenharmony_ci 239e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci**参数:** 242e41f4b71Sopenharmony_ci 243e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 244e41f4b71Sopenharmony_ci| ------ | ------ | ---- | -------------- | 245e41f4b71Sopenharmony_ci| value | string | 是 | 文字特性效果。 | 246e41f4b71Sopenharmony_ci 247e41f4b71Sopenharmony_ci### contentModifier<sup>12+</sup> 248e41f4b71Sopenharmony_ci 249e41f4b71Sopenharmony_cicontentModifier(modifier: ContentModifier\<TextClockConfiguration>) 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci定制TextClock内容区的方法。 252e41f4b71Sopenharmony_ci 253e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci**参数:** 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 260e41f4b71Sopenharmony_ci| ------ | --------------------------------------------- | ---- | ------------------------------------------------ | 261e41f4b71Sopenharmony_ci| modifier | [ContentModifier\<TextClockConfiguration>](#textclockconfiguration12对象说明) | 是 | 在TextClock组件上,定制内容区的方法。<br/>modifier: 内容修改器,开发者需要自定义class实现ContentModifier接口。 | 262e41f4b71Sopenharmony_ci 263e41f4b71Sopenharmony_ci### dateTimeOptions<sup>12+</sup> 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_cidateTimeOptions(dateTimeOptions: Optional\<DateTimeOptions>) 266e41f4b71Sopenharmony_ci 267e41f4b71Sopenharmony_ci设置小时是否显示前导0。 268e41f4b71Sopenharmony_ci 269e41f4b71Sopenharmony_ci**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 272e41f4b71Sopenharmony_ci 273e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci**参数:** 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 278e41f4b71Sopenharmony_ci| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 279e41f4b71Sopenharmony_ci| dateTimeOptions | Optional<[DateTimeOptions](../../apis-localization-kit/js-apis-intl.md#datetimeoptions)> | 是 | 设置小时是否显示前导0,只支持设置hour参数,参数值为{hour: "2-digit"}时表示显示前导0,参数值为{hour: "numeric"}时表示不显示前导0。<br/>默认值:undefined,由组件根据应用设置格式自行判断是否显示前导0。| 280e41f4b71Sopenharmony_ci 281e41f4b71Sopenharmony_ci## 事件 282e41f4b71Sopenharmony_ci 283e41f4b71Sopenharmony_ci除支持[通用事件](ts-universal-events-click.md)外,还支持以下事件: 284e41f4b71Sopenharmony_ci 285e41f4b71Sopenharmony_ci### onDateChange 286e41f4b71Sopenharmony_ci 287e41f4b71Sopenharmony_cionDateChange(event: (value: number) => void) 288e41f4b71Sopenharmony_ci 289e41f4b71Sopenharmony_ci提供时间变化回调,该事件回调间隔为秒。 290e41f4b71Sopenharmony_ci 291e41f4b71Sopenharmony_ci组件不可见时不回调。 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci非卡片中使用时,该事件回调间隔为秒。 294e41f4b71Sopenharmony_ci 295e41f4b71Sopenharmony_ci卡片中使用时,该事件回调间隔为分钟。 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 300e41f4b71Sopenharmony_ci 301e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 302e41f4b71Sopenharmony_ci 303e41f4b71Sopenharmony_ci**参数:** 304e41f4b71Sopenharmony_ci 305e41f4b71Sopenharmony_ci| 参数名 | 类型 | 必填 | 说明 | 306e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------------------------------------------------------ | 307e41f4b71Sopenharmony_ci| value | number | 是 | Unix Time Stamp,即自1970年1月1日(UTC)起经过的秒数。 | 308e41f4b71Sopenharmony_ci 309e41f4b71Sopenharmony_ci## TextClockController 310e41f4b71Sopenharmony_ci 311e41f4b71Sopenharmony_ciTextClock容器组件的控制器,可以将该控制器绑定到TextClock组件,通过它控制文本时钟的启动与停止。一个TextClock组件仅支持绑定一个控制器。 312e41f4b71Sopenharmony_ci 313e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 314e41f4b71Sopenharmony_ci 315e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 316e41f4b71Sopenharmony_ci 317e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 318e41f4b71Sopenharmony_ci 319e41f4b71Sopenharmony_ci### 导入对象 320e41f4b71Sopenharmony_ci 321e41f4b71Sopenharmony_ci```ts 322e41f4b71Sopenharmony_cicontroller: TextClockController = new TextClockController(); 323e41f4b71Sopenharmony_ci``` 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ci### constructor 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ciconstructor() 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ciTextClockController的构造函数。 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 336e41f4b71Sopenharmony_ci 337e41f4b71Sopenharmony_ci### start 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_cistart() 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ci启动文本时钟。 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 344e41f4b71Sopenharmony_ci 345e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ci### stop 350e41f4b71Sopenharmony_ci 351e41f4b71Sopenharmony_cistop() 352e41f4b71Sopenharmony_ci 353e41f4b71Sopenharmony_ci停止文本时钟。 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci**卡片能力:** 从API version 11开始,该接口支持在ArkTS卡片中使用。 356e41f4b71Sopenharmony_ci 357e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 358e41f4b71Sopenharmony_ci 359e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci## TextClockConfiguration<sup>12+</sup>对象说明 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci开发者需要自定义class实现ContentModifier接口。 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。 366e41f4b71Sopenharmony_ci 367e41f4b71Sopenharmony_ci**系统能力:** SystemCapability.ArkUI.ArkUI.Full 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci| 名称 | 类型 | 必填 | 说明 | 370e41f4b71Sopenharmony_ci| ------ | ------ | ------ |-------------------------------- | 371e41f4b71Sopenharmony_ci| timeZoneOffset | number | 是 | 当前文本时钟时区偏移量。 | 372e41f4b71Sopenharmony_ci| started | boolean | 是 | 指示文本时钟是否启动。<br>默认值:true。 | 373e41f4b71Sopenharmony_ci| timeValue | number | 是 | 当前文本时钟时区的UTC秒数。 | 374e41f4b71Sopenharmony_ci 375e41f4b71Sopenharmony_ci## 示例 376e41f4b71Sopenharmony_ci### 示例1 377e41f4b71Sopenharmony_ci```ts 378e41f4b71Sopenharmony_ci@Entry 379e41f4b71Sopenharmony_ci@Component 380e41f4b71Sopenharmony_cistruct Second { 381e41f4b71Sopenharmony_ci @State accumulateTime: number = 0 382e41f4b71Sopenharmony_ci // 导入对象 383e41f4b71Sopenharmony_ci controller: TextClockController = new TextClockController() 384e41f4b71Sopenharmony_ci build() { 385e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 386e41f4b71Sopenharmony_ci Text('Current milliseconds is ' + this.accumulateTime) 387e41f4b71Sopenharmony_ci .fontSize(20) 388e41f4b71Sopenharmony_ci // 以12小时制显示东八区的系统时间,精确到秒。 389e41f4b71Sopenharmony_ci TextClock({ timeZoneOffset: -8, controller: this.controller }) 390e41f4b71Sopenharmony_ci .format('aa hh:mm:ss') 391e41f4b71Sopenharmony_ci .onDateChange((value: number) => { 392e41f4b71Sopenharmony_ci this.accumulateTime = value 393e41f4b71Sopenharmony_ci }) 394e41f4b71Sopenharmony_ci .margin(20) 395e41f4b71Sopenharmony_ci .fontSize(30) 396e41f4b71Sopenharmony_ci Button("start TextClock") 397e41f4b71Sopenharmony_ci .margin({ bottom: 10 }) 398e41f4b71Sopenharmony_ci .onClick(() => { 399e41f4b71Sopenharmony_ci // 启动文本时钟 400e41f4b71Sopenharmony_ci this.controller.start() 401e41f4b71Sopenharmony_ci }) 402e41f4b71Sopenharmony_ci Button("stop TextClock") 403e41f4b71Sopenharmony_ci .onClick(() => { 404e41f4b71Sopenharmony_ci // 停止文本时钟 405e41f4b71Sopenharmony_ci this.controller.stop() 406e41f4b71Sopenharmony_ci }) 407e41f4b71Sopenharmony_ci } 408e41f4b71Sopenharmony_ci .width('100%') 409e41f4b71Sopenharmony_ci .height('100%') 410e41f4b71Sopenharmony_ci } 411e41f4b71Sopenharmony_ci} 412e41f4b71Sopenharmony_ci``` 413e41f4b71Sopenharmony_ci 414e41f4b71Sopenharmony_ci 415e41f4b71Sopenharmony_ci### 示例2 416e41f4b71Sopenharmony_ci``` ts 417e41f4b71Sopenharmony_ci@Entry 418e41f4b71Sopenharmony_ci@Component 419e41f4b71Sopenharmony_cistruct TextClockExample { 420e41f4b71Sopenharmony_ci @State textShadows : ShadowOptions | Array<ShadowOptions> = [{ radius: 10, color: Color.Red, offsetX: 10, offsetY: 0 },{ radius: 10, color: Color.Black, offsetX: 20, offsetY: 0 }, 421e41f4b71Sopenharmony_ci { radius: 10, color: Color.Brown, offsetX: 30, offsetY: 0 },{ radius: 10, color: Color.Green, offsetX: 40, offsetY: 0 }, 422e41f4b71Sopenharmony_ci { radius: 10, color: Color.Yellow, offsetX: 100, offsetY: 0 }] 423e41f4b71Sopenharmony_ci build() { 424e41f4b71Sopenharmony_ci Column({ space: 8 }) { 425e41f4b71Sopenharmony_ci TextClock().fontSize(50).textShadow(this.textShadows) 426e41f4b71Sopenharmony_ci } 427e41f4b71Sopenharmony_ci } 428e41f4b71Sopenharmony_ci} 429e41f4b71Sopenharmony_ci``` 430e41f4b71Sopenharmony_ci 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ci### 示例3 433e41f4b71Sopenharmony_ci该示例实现了自定义文本时钟样式的功能,自定义样式实现了一个时间选择器组件:通过文本时钟的时区偏移量与UTC秒数,来动态改变时间选择器的选中值,实现时钟效果。同时,根据文本时钟的启动状态,实现文本选择器的12小时制与24小时制的切换。 434e41f4b71Sopenharmony_ci 435e41f4b71Sopenharmony_ci``` ts 436e41f4b71Sopenharmony_ciclass MyTextClockStyle implements ContentModifier<TextClockConfiguration> { 437e41f4b71Sopenharmony_ci currentTimeZoneOffset: number = new Date().getTimezoneOffset() / 60 438e41f4b71Sopenharmony_ci title: string = '' 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci constructor(title: string) { 441e41f4b71Sopenharmony_ci this.title = title 442e41f4b71Sopenharmony_ci } 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci applyContent(): WrappedBuilder<[TextClockConfiguration]> { 445e41f4b71Sopenharmony_ci return wrapBuilder(buildTextClock) 446e41f4b71Sopenharmony_ci } 447e41f4b71Sopenharmony_ci} 448e41f4b71Sopenharmony_ci 449e41f4b71Sopenharmony_ci@Builder 450e41f4b71Sopenharmony_cifunction buildTextClock(config: TextClockConfiguration) { 451e41f4b71Sopenharmony_ci Row() { 452e41f4b71Sopenharmony_ci Column() { 453e41f4b71Sopenharmony_ci Text((config.contentModifier as MyTextClockStyle).title) 454e41f4b71Sopenharmony_ci .fontSize(20) 455e41f4b71Sopenharmony_ci .margin(20) 456e41f4b71Sopenharmony_ci TimePicker({ 457e41f4b71Sopenharmony_ci selected: (new Date(config.timeValue * 1000 + ((config.contentModifier as MyTextClockStyle).currentTimeZoneOffset - config.timeZoneOffset) * 60 * 60 * 1000)), 458e41f4b71Sopenharmony_ci format: TimePickerFormat.HOUR_MINUTE_SECOND 459e41f4b71Sopenharmony_ci }) 460e41f4b71Sopenharmony_ci .useMilitaryTime(!config.started) 461e41f4b71Sopenharmony_ci } 462e41f4b71Sopenharmony_ci } 463e41f4b71Sopenharmony_ci} 464e41f4b71Sopenharmony_ci 465e41f4b71Sopenharmony_ci@Entry 466e41f4b71Sopenharmony_ci@Component 467e41f4b71Sopenharmony_cistruct TextClockExample { 468e41f4b71Sopenharmony_ci @State accumulateTime1: number = 0 469e41f4b71Sopenharmony_ci @State timeZoneOffset: number = -8 470e41f4b71Sopenharmony_ci controller1: TextClockController = new TextClockController() 471e41f4b71Sopenharmony_ci controller2: TextClockController = new TextClockController() 472e41f4b71Sopenharmony_ci 473e41f4b71Sopenharmony_ci build() { 474e41f4b71Sopenharmony_ci Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 475e41f4b71Sopenharmony_ci Text('Current milliseconds is ' + this.accumulateTime1) 476e41f4b71Sopenharmony_ci .fontSize(20) 477e41f4b71Sopenharmony_ci .margin({ top: 20 }) 478e41f4b71Sopenharmony_ci TextClock({ timeZoneOffset: this.timeZoneOffset, controller: this.controller1 }) 479e41f4b71Sopenharmony_ci .format('aa hh:mm:ss') 480e41f4b71Sopenharmony_ci .onDateChange((value: number) => { 481e41f4b71Sopenharmony_ci this.accumulateTime1 = value 482e41f4b71Sopenharmony_ci }) 483e41f4b71Sopenharmony_ci .margin(20) 484e41f4b71Sopenharmony_ci .fontSize(30) 485e41f4b71Sopenharmony_ci TextClock({ timeZoneOffset: this.timeZoneOffset, controller: this.controller2 }) 486e41f4b71Sopenharmony_ci .format('aa hh:mm:ss') 487e41f4b71Sopenharmony_ci .fontSize(30) 488e41f4b71Sopenharmony_ci .contentModifier(new MyTextClockStyle('ContentModifier:')) 489e41f4b71Sopenharmony_ci Button("start TextClock") 490e41f4b71Sopenharmony_ci .margin({ top: 20, bottom: 10 }) 491e41f4b71Sopenharmony_ci .onClick(() => { 492e41f4b71Sopenharmony_ci // 启动文本时钟 493e41f4b71Sopenharmony_ci this.controller1.start() 494e41f4b71Sopenharmony_ci this.controller2.start() 495e41f4b71Sopenharmony_ci }) 496e41f4b71Sopenharmony_ci Button("stop TextClock") 497e41f4b71Sopenharmony_ci .margin({ bottom: 30 }) 498e41f4b71Sopenharmony_ci .onClick(() => { 499e41f4b71Sopenharmony_ci // 停止文本时钟 500e41f4b71Sopenharmony_ci this.controller1.stop() 501e41f4b71Sopenharmony_ci this.controller2.stop() 502e41f4b71Sopenharmony_ci }) 503e41f4b71Sopenharmony_ci 504e41f4b71Sopenharmony_ci } 505e41f4b71Sopenharmony_ci .width('100%') 506e41f4b71Sopenharmony_ci .height('100%') 507e41f4b71Sopenharmony_ci } 508e41f4b71Sopenharmony_ci} 509e41f4b71Sopenharmony_ci``` 510e41f4b71Sopenharmony_ci 511e41f4b71Sopenharmony_ci 512e41f4b71Sopenharmony_ci### 示例4 513e41f4b71Sopenharmony_ci该示例演示了dateTimeOptions属性为小时字段增加或去除前导0的功能。24小时制的小时字段默认带有前导0,可通过dateTimeOptions属性去除前导0,12小时制的小时字段默认不带有前导0,可通过dateTimeOptions属性增加前导0。 514e41f4b71Sopenharmony_ci``` ts 515e41f4b71Sopenharmony_ci@Entry 516e41f4b71Sopenharmony_ci@Component 517e41f4b71Sopenharmony_cistruct TextClockExample { 518e41f4b71Sopenharmony_ci build() { 519e41f4b71Sopenharmony_ci Column({ space: 8 }) { 520e41f4b71Sopenharmony_ci Row() { 521e41f4b71Sopenharmony_ci Text("24小时制去除前导0:") 522e41f4b71Sopenharmony_ci .fontSize(20) 523e41f4b71Sopenharmony_ci TextClock() 524e41f4b71Sopenharmony_ci .fontSize(20) 525e41f4b71Sopenharmony_ci .format("HH:mm:ss") 526e41f4b71Sopenharmony_ci .dateTimeOptions({hour: "numeric"}) 527e41f4b71Sopenharmony_ci } 528e41f4b71Sopenharmony_ci Row() { 529e41f4b71Sopenharmony_ci Text("12小时制增加前导0:") 530e41f4b71Sopenharmony_ci .fontSize(20) 531e41f4b71Sopenharmony_ci TextClock() 532e41f4b71Sopenharmony_ci .fontSize(20) 533e41f4b71Sopenharmony_ci .format("aa hh:mm:ss") 534e41f4b71Sopenharmony_ci .dateTimeOptions({hour: "2-digit"}) 535e41f4b71Sopenharmony_ci } 536e41f4b71Sopenharmony_ci } 537e41f4b71Sopenharmony_ci .alignItems(HorizontalAlign.Start) 538e41f4b71Sopenharmony_ci } 539e41f4b71Sopenharmony_ci} 540e41f4b71Sopenharmony_ci``` 541e41f4b71Sopenharmony_ci