Wear OS 中的动态表达式
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Wear OS 支持对功能块和复杂功能中显示的信息进行动态更新。
通过动态表达式,您可以将显示在应用 surface 上的数据(例如功能块或复杂功能)绑定到特定数据源。平台可读取的心率数据就是此类数据源的一个例子。创建此绑定后,系统会自动更新功能块和复杂功能中的数据。
创建动态数据绑定
如需创建动态数据绑定,请定义一个使用动态数据类型的变量。将此变量与您要使用的数据流相关联。
例如,您可以提取与系统时钟和健康信息相关的值,如以下代码段所示。
Kotlin
val systemTime = DynamicInstant.platformTimeWithSecondsPrecision()
val steps: DynamicInt32 = PlatformHealthSources.dailySteps()
Java
DynamicInstant systemTime = DynamicInstant.platformTimeWithSecondsPrecision();
DynamicInt32 steps = PlatformHealthSources.dailySteps();
您还可以通过常量表达式创建动态值,并针对所有动态值执行算术运算,如以下代码段所示:
Kotlin
val dynamicValue = DynamicInt32.constant(1).plus(2)
Java
DynamicInt32 dynamicValue = DynamicInt32.constant(1).plus(2)
可能的动态数据类型列表
Wear OS 支持以下动态数据类型:
此外,您还可以使用内置功能转换数据类型,例如:
DynamicInt32
支持使用 format()
转换为 DynamicString
。
DynamicDuration
允许您将特定部分(例如持续时间的秒数部分)提取为 DynamicInt32
对象。
限制每个界面上的动态表达式数量
系统对特定界面上可同时处理的动态表达式数量设有限制,因而会将任何额外的动态表达式转换为静态值。
Wear OS 也将常量表达式视为动态表达式。例如,以下代码段包含 4 个动态表达式:
plus()
运算。
animate()
运算。
constant(1)
表达式。
constant(2)
表达式,隐含在 plus()
动态表达式的值 2
中。
DynamicInt32.constant(1).plus(2).animate()
为您推荐
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Dynamic expressions in Wear OS\n\nWear OS supports dynamic updates to information that appears in your [tiles](/training/wearables/tiles)\nand [complications](/training/wearables/tiles/complications).\n\nUsing dynamic expressions, you can bind data that appears on a surface of your\napp--such as a tile or complication--to a particular data source. An example of\nsuch a data source is heart rate data that the platform can read. After you've\nestablished this binding, the system updates the data in your tiles and\ncomplications automatically.\n\nCreate dynamic data bindings\n----------------------------\n\nTo create a dynamic data binding, define a variable that uses a\n[dynamic data type](#data-types). Associate this variable with the data stream that you\nwant to use.\n\nFor example, you can fetch values related to the system clock and health\ninformation, as shown in the following code snippet.\n| **Note:** To access the data in [`PlatformHealthSources`](/reference/androidx/wear/protolayout/expression/PlatformHealthSources), you must [request a\nruntime permission](/training/permissions/requesting) in your app. \n\n### Kotlin\n\n```kotlin\nval systemTime = DynamicInstant.platformTimeWithSecondsPrecision()\nval steps: DynamicInt32 = PlatformHealthSources.dailySteps()\n```\n\n### Java\n\n```java\nDynamicInstant systemTime = DynamicInstant.platformTimeWithSecondsPrecision();\nDynamicInt32 steps = PlatformHealthSources.dailySteps();\n```\n\nYou can also create dynamic values from constant expressions and perform\narithmetic operations on any dynamic value, as shown in the following snippet: \n\n### Kotlin\n\n```kotlin\nval dynamicValue = DynamicInt32.constant(1).plus(2)\n```\n\n### Java\n\n```java\nDynamicInt32 dynamicValue = DynamicInt32.constant(1).plus(2)\n```\n\n### List of possible dynamic data types\n\nWear OS supports the following dynamic data types:\n\n- [`DynamicBool`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicBool)\n- [`DynamicColor`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicColor)\n- [`DynamicDuration`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicDuration)\n- [`DynamicFloat`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicFloat)\n- [`DynamicInstant`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInstant)\n- [`DynamicInt32`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInt32)\n- [`DynamicString`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicString)\n\nIn addition, you can transform the data type using built-in capabilities, such\nas the following:\n\n- `DynamicInt32` supports conversion to a `DynamicString` using [`format()`](/reference/androidx/wear/protolayout/expression/DynamicBuilders.DynamicInt32#format()).\n- `DynamicDuration` lets you extract specific parts, such as the seconds part of a duration, as `DynamicInt32` objects.\n\nUse a limited number of dynamic expressions on each screen\n----------------------------------------------------------\n\nThe system has a limit on the number of dynamic expressions that it can process\nsimultaneously on a particular screen. The system converts any additional\ndynamic expressions to static values.\n\nWear OS considers constant expressions to be dynamic expressions, too. For\nexample, the following code snippet contains 4 dynamic expressions:\n\n1. The `plus()` operation.\n2. The `animate()` operation.\n3. The `constant(1)` expression.\n4. The `constant(2)` expression, which is implied by the value `2` in the `plus()` dynamic expression.\n\n DynamicInt32.constant(1).plus(2).animate()\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Migrate to ProtoLayout namespaces](/training/wearables/tiles/migrate-to-protolayout)\n- [Side-effects in Compose](/develop/ui/compose/side-effects)\n- [AGSL Quick Reference](/develop/ui/views/graphics/agsl/agsl-quick-reference)"]]