动态更改元素内容
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
虽然 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 和/或其关联公司的注册商标。
最后更新时间 (UTC):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"]],["最后更新时间 (UTC):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."]]