動態變更元素內容
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
雖然 Transform
可讓您變更元素或元素群組的外觀,但在某些情況下,您可能會想要根據某些條件在行為清單之間切換。這類似於其他語言中的 switch
陳述式或 if…else
陳述式。
舉例來說,您可能會想針對清晨、上午、午餐、下午、晚上和夜晚顯示不同的背景。
錶面格式中的 Condition
陳述式可讓您根據運算式的評估結果,加入錶面場景的不同部分,例如:
<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>
關於條件,請注意以下幾點:
- 系統會使用
expression
為 true
的第一個 Compare
元素,並忽略其他元素。
- 由於 XML 格式,通常最簡單的方法是將運算式定義包在
CDATA
元素中,如上圖所示,這樣就能避免使用 >
和 &
等實體元素進行 XML 轉義。
Condition
結構可嵌套。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],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."]]