เปลี่ยนเนื้อหาองค์ประกอบแบบไดนามิก
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
แม้ว่า 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>
สิ่งที่ควรทราบเกี่ยวกับเงื่อนไขมีดังนี้
- ระบบจะใช้องค์ประกอบ
Compare
รายการแรกที่มี expression
เป็น true
และละเว้นรายการอื่นๆ
- เนื่องจากรูปแบบ XML มักทำให้การรวมคําจํากัดความของนิพจน์ในองค์ประกอบ
CDATA
ดังที่แสดงที่นี่เป็นวิธีที่ง่ายที่สุด เนื่องจากวิธีนี้ช่วยให้ไม่ต้องใช้การหนีค่า XML โดยใช้องค์ประกอบเอนทิตี เช่น >
และ &
- โครงสร้าง
Condition
สามารถฝังได้
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","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 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."]]