Thay đổi nội dung phần tử một cách linh động
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.
Mặc dù Transform
cho phép bạn thay đổi giao diện của các phần tử hoặc nhóm phần tử, nhưng có thể có trường hợp bạn muốn chuyển đổi giữa danh sách các hành vi dựa trên một số điều kiện. Điều này tương tự như câu lệnh switch
hoặc câu lệnh if…else
trong các ngôn ngữ khác.
Ví dụ: bạn có thể muốn hiển thị một nền khác nhau cho buổi sáng sớm, buổi sáng, bữa trưa, buổi chiều, buổi tối và ban đêm.
Câu lệnh Condition
trong Định dạng mặt đồng hồ cho phép bạn đưa nhiều phần vào cảnh mặt đồng hồ tuỳ thuộc vào việc đánh giá biểu thức, ví dụ:
<Condition>
<Expressions>
<Expression name="is_early_morning">
<![CDATA[[HOUR_0_23] >= 6 && [HOUR_0_23] < 8]]
</Expression>
<Expression name="is_morning">
<![CDATA[[HOUR_0_23] < 12]]
</Expression>
...
</Expressions>
<Compare expression="is_early_morning">
<!-- Early morning content here -->
<Group ... />
</Compare>
<Compare expression="is_morning">
<!-- Morning content here -->
<Group ... />
</Compare>
...
<!-- The "else" case -->
<Default>
<!-- content -->
</Default>
</Condition>
Một số điều cần lưu ý về điều kiện:
- Phần tử
Compare
đầu tiên có expression
là true
sẽ được sử dụng và các phần tử khác sẽ bị bỏ qua.
- Do định dạng XML, cách dễ nhất thường là gói định nghĩa biểu thức trong phần tử
CDATA
như minh hoạ ở đây, vì điều này giúp bạn không cần phải thoát XML bằng các phần tử thực thể như >
và &
.
- Bạn có thể lồng các cấu trúc
Condition
.
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,["# Dynamically change element content\n\nWhile `Transform` lets you change the appearance of elements or groups of\nelements, there might be occasions where you want to switch between a list of\nbehaviors based on some condition. This is analogous to a `switch` statement\nor `if...else` statement in other languages.\n\nFor example, you might want to show a different background for early morning,\nmorning, lunch, afternoon, evening, and night.\n\n`Condition` statements in Watch Face Format allow you to include different parts\nof your watch face scene depending on the evaluation of expressions, for\nexample: \n\n \u003cCondition\u003e\n \u003cExpressions\u003e\n \u003cExpression name=\"is_early_morning\"\u003e\n \u003c![CDATA[[HOUR_0_23] \u003e= 6 && [HOUR_0_23] \u003c 8]]\n \u003c/Expression\u003e\n \u003cExpression name=\"is_morning\"\u003e\n \u003c![CDATA[[HOUR_0_23] \u003c 12]]\n \u003c/Expression\u003e\n ...\n \u003c/Expressions\u003e\n \u003cCompare expression=\"is_early_morning\"\u003e\n \u003c!-- Early morning content here --\u003e\n \u003cGroup ... /\u003e\n \u003c/Compare\u003e\n \u003cCompare expression=\"is_morning\"\u003e\n \u003c!-- Morning content here --\u003e\n \u003cGroup ... /\u003e\n \u003c/Compare\u003e\n ...\n \u003c!-- The \"else\" case --\u003e\n \u003cDefault\u003e\n \u003c!-- content --\u003e\n \u003c/Default\u003e\n \u003c/Condition\u003e\n\nA few things to note about conditions:\n\n1. The first `Compare` element where the `expression` is `true` is used, and others are ignored.\n2. Owing to the XML format, it can often be easiest to wrap the expression definition in a `CDATA` element as shown here, as this avoids the need for XML escaping using entity elements such as `>` and `&`.\n3. `Condition` structures can be nested."]]