Biểu thức động trong Wear OS
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Wear OS hỗ trợ cập nhật nội dung động cho thông tin xuất hiện trong các thẻ thông tin và chức năng.
Khi sử dụng biểu thức động, bạn có thể liên kết dữ liệu xuất hiện trên một nền tảng của ứng dụng (chẳng hạn như thẻ thông tin hoặc chức năng) với một nguồn dữ liệu cụ thể. Ví dụ minh hoạ cho một nguồn dữ liệu như vậy là dữ liệu nhịp tim mà nền tảng có thể đọc. Sau khi bạn thiết lập liên kết này, hệ thống sẽ tự động cập nhật dữ liệu trong các thẻ thông tin và chức năng của bạn.
Tạo liên kết dữ liệu động
Để tạo liên kết dữ liệu động, hãy xác định một biến sử dụng loại dữ liệu động. Liên kết biến này với luồng dữ liệu mà bạn muốn dùng.
Ví dụ: bạn có thể tìm nạp các giá trị liên quan đến đồng hồ hệ thống và thông tin sức khoẻ, như trong đoạn mã sau.
Kotlin
val systemTime = DynamicInstant.platformTimeWithSecondsPrecision()
val steps: DynamicInt32 = PlatformHealthSources.dailySteps()
Java
DynamicInstant systemTime = DynamicInstant.platformTimeWithSecondsPrecision();
DynamicInt32 steps = PlatformHealthSources.dailySteps();
Bạn cũng có thể tạo giá trị động từ các biểu thức không đổi và thực hiện các phép tính số học đối với bất kỳ giá trị động nào, như trong đoạn mã sau:
Kotlin
val dynamicValue = DynamicInt32.constant(1).plus(2)
Java
DynamicInt32 dynamicValue = DynamicInt32.constant(1).plus(2)
Danh sách các loại dữ liệu động có thể có
Wear OS hỗ trợ các loại dữ liệu động sau đây:
Ngoài ra, bạn có thể chuyển đổi loại dữ liệu bằng các chức năng tích hợp sẵn, chẳng hạn như các chức năng sau:
DynamicInt32
hỗ trợ chuyển đổi sang DynamicString
bằng cách sử dụng format()
.
DynamicDuration
cho phép bạn trích xuất các phần cụ thể, chẳng hạn như phần giây của thời lượng, dưới dạng các đối tượng DynamicInt32
.
Sử dụng một số lượng hạn chế các biểu thức động trên mỗi màn hình
Hệ thống có đưa ra giới hạn về số lượng biểu thức động mà hệ thống có thể xử lý đồng thời trên một màn hình cụ thể. Hệ thống sẽ chuyển đổi mọi biểu thức động khác sang giá trị tĩnh.
Wear OS cũng coi biểu thức không đổi là biểu thức động. Ví dụ: đoạn mã sau đây chứa 4 biểu thức động:
- Toán tử
plus()
.
- Toán tử
animate()
.
- Biểu thức
constant(1)
.
- Biểu thức
constant(2)
ngụ ý giá trị 2
trong biểu thức động plus()
.
DynamicInt32.constant(1).plus(2).animate()
Đề xuất cho bạn
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],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)"]]