HarmonyOS之时钟FA卡片开发样例实践

开发 前端 OpenHarmony
文章由鸿蒙社区产出,想要了解更多内容请前往:51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com

[[407156]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

时钟FA卡片开发样例

介绍

服务卡片是FA的一种主要信息呈现形式,开发者可以在卡片中展示用户最关心的FA数据,并可以通过点击卡片内容直接打开FA。例如,天气类FA,可以在服务卡片中展示当前的基本天气信息,点击卡片启动天气FA,展示详细的天气数据。

同时,服务卡片支持不同的规格尺寸,开发者可以根据展示的不同内容和布局效果,选用不同的卡片尺寸,支持的尺寸包括:1x2、2x2、2x4 和 4x4。

知识点:

1.对象关系型数据库的使用,如何查询、创建卡片、删除卡片

2.如何更新卡片数据

搭建环境

安装DevEco Studio,详情请参考DevEco Studio下载。

设置DevEco Studio开发环境,DevEco Studio开发环境需要依赖于网络环境,需要连接上网络才能确保工具的正常使用,可以根据如下两种情况来配置开发环境:

如果可以直接访问Internet,只需进行下载HarmonyOS SDK操作。

如果网络不能直接访问Internet,需要通过代理服务器才可以访问,请参考配置开发环境。

代码结构解读

  1. ├─config.json #项目配置文件 
  2. ├─java 
  3. │  └─com 
  4. │      └─huawei 
  5. │          └─learnharmony 
  6. │              │  MainAbility.java 
  7. │              │  MyApplication.java 
  8. │              │  TimerAbility.java  #用于定时更新卡片的服务 
  9. │              │ 
  10. │              ├─database 
  11. │              │      Form.java #卡片表,extends OrmObject  
  12. │              │      FormDatabase.java #卡片数据库,extends OrmDatabase 
  13. │              │ 
  14. │              ├─slice 
  15. │              │      ClockCardSlice.java #时钟卡片主能力页,extends AbilitySlice 
  16. │              │ 
  17. │              └─utils 
  18. │                      ComponentProviderUtils.java #ComponentProvider工具类 
  19. │                      DatabaseUtils.java #数据库工具类,实现Form的添加和删除 
  20. │                      DateUtils.java  #日期工具类 
  21. │                      LogUtils.java #日志工具类 
  22. │ 
  23. └─resources 
  24.     └─base 
  25.         ├─element 
  26.         │      string.json 
  27.         │ 
  28.         ├─graphic 
  29.         │      background_ability_main.xml 
  30.         │      background_week.xml 
  31.         │ 
  32.         ├─layout 
  33.         │      ability_main.xml #主能力页,默认 
  34.         │      form_image_with_info_date_card_1_2.xml #1x2规格的卡片 
  35.         │      form_image_with_info_date_card_2_2.xml #2x2规格的卡片 
  36.         │      form_image_with_info_date_card_2_4.xml #2x4规格的卡片 
  37.         │ 
  38.         └─media 
  39.                 form_image_with_info_datecard_default_image_2.png 
  40.                 form_image_with_info_form_card_default_image_2.png 
  41.                 icon.png 

卡片布局

form_image_with_info_date_card_1_2.xml #1x2规格的卡片

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DependentLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:background_element="#FFFFFFFF" 
  7.     ohos:remote="true"
  8.  
  9.     <DependentLayout 
  10.         ohos:height="match_content" 
  11.         ohos:width="match_parent" 
  12.         ohos:orientation="vertical" 
  13.         ohos:vertical_center="true" 
  14.         > 
  15.         <Text 
  16.             ohos:id="$+id:date" 
  17.             ohos:height="match_content" 
  18.             ohos:width="match_content" 
  19.             ohos:center_in_parent="true" 
  20.             ohos:text="2021-03-24" 
  21.             ohos:text_alignment="vertical_center" 
  22.             ohos:text_font="sans-serif-medium" 
  23.             ohos:text_size="20fp" 
  24.             /> 
  25.  
  26.     </DependentLayout> 
  27. </DependentLayout> 

form_image_with_info_date_card_2_2.xml #2x2规格的卡片

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DependentLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:background_element="#FFFFFFFF" 
  7.     ohos:remote="true"
  8.  
  9.     <DirectionalLayout 
  10.         ohos:height="match_content" 
  11.         ohos:width="match_parent" 
  12.         ohos:orientation="vertical" 
  13.         > 
  14.  
  15.         <Text 
  16.             ohos:id="$+id:date" 
  17.             ohos:height="match_content" 
  18.             ohos:width="match_parent" 
  19.             ohos:text_alignment="horizontal_center" 
  20.             ohos:margin="12fp" 
  21.             ohos:text="2021-03-24" 
  22.             ohos:text_font="sans-serif-medium" 
  23.             ohos:text_size="10fp" 
  24.             /> 
  25.     </DirectionalLayout> 
  26.  
  27.     <DirectionalLayout 
  28.         ohos:id="$+id:title" 
  29.         ohos:height="match_content" 
  30.         ohos:width="match_parent" 
  31.         ohos:alignment="horizontal_center" 
  32.         ohos:orientation="horizontal" 
  33.         ohos:top_margin="35fp" 
  34.         > 
  35.  
  36.         <Text 
  37.             ohos:height="match_content" 
  38.             ohos:width="match_content" 
  39.             ohos:margin="8vp" 
  40.             ohos:text="HOUR" 
  41.             ohos:text_size="10fp" 
  42.             /> 
  43.  
  44.         <Text 
  45.             ohos:height="match_content" 
  46.             ohos:width="match_content" 
  47.             ohos:margin="8vp" 
  48.             ohos:text="MIN" 
  49.             ohos:text_size="10fp" 
  50.             /> 
  51.  
  52.         <Text 
  53.             ohos:height="match_content" 
  54.             ohos:width="match_content" 
  55.             ohos:margin="8vp" 
  56.             ohos:text="SEC" 
  57.             ohos:text_size="10fp" 
  58.             /> 
  59.     </DirectionalLayout> 
  60.  
  61.     <DirectionalLayout 
  62.         ohos:id="$+id:time" 
  63.         ohos:height="match_content" 
  64.         ohos:width="match_parent" 
  65.         ohos:alignment="horizontal_center" 
  66.         ohos:below="$id:title" 
  67.         ohos:orientation="horizontal" 
  68.         ohos:top_margin="0.5fp" 
  69.         > 
  70.  
  71.         <Text 
  72.             ohos:id="$+id:hour" 
  73.             ohos:height="match_content" 
  74.             ohos:width="match_content" 
  75.             ohos:left_margin="10vp" 
  76.             ohos:right_margin="5vp" 
  77.             ohos:text="00" 
  78.             ohos:text_font="HwChinese-medium" 
  79.             ohos:text_size="20fp" 
  80.             ohos:top_margin="2vp" 
  81.             /> 
  82.  
  83.         <Text 
  84.             ohos:height="match_content" 
  85.             ohos:width="match_content" 
  86.             ohos:text=":" 
  87.             ohos:text_font="bold" 
  88.             ohos:text_size="20fp" 
  89.             ohos:top_margin="2vp" 
  90.             /> 
  91.  
  92.         <Text 
  93.             ohos:id="$+id:min" 
  94.             ohos:height="match_content" 
  95.             ohos:width="match_content" 
  96.             ohos:left_margin="5vp" 
  97.             ohos:right_margin="5vp" 
  98.             ohos:text="00" 
  99.             ohos:text_font="HwChinese-medium" 
  100.             ohos:text_size="20fp" 
  101.             ohos:top_margin="2vp" 
  102.             /> 
  103.  
  104.         <Text 
  105.             ohos:height="match_content" 
  106.             ohos:width="match_content" 
  107.             ohos:text=":" 
  108.             ohos:text_font="bold" 
  109.             ohos:text_size="20fp" 
  110.             ohos:top_margin="2vp" 
  111.             /> 
  112.  
  113.         <Text 
  114.             ohos:id="$+id:sec" 
  115.             ohos:height="match_content" 
  116.             ohos:width="match_content" 
  117.             ohos:left_margin="5vp" 
  118.             ohos:right_margin="10vp" 
  119.             ohos:text="00" 
  120.             ohos:text_font="HwChinese-medium" 
  121.             ohos:text_size="20fp" 
  122.             ohos:top_margin="2vp" 
  123.             /> 
  124.     </DirectionalLayout> 
  125.  
  126.     <DirectionalLayout 
  127.         ohos:height="match_content" 
  128.         ohos:width="match_parent" 
  129.         ohos:alignment="center" 
  130.         ohos:below="$id:time" 
  131.         ohos:margin="16fp" 
  132.         ohos:orientation="horizontal" 
  133.         > 
  134.         <Text 
  135.             ohos:id="$+id:sun" 
  136.             ohos:height="match_content" 
  137.             ohos:width="match_content" 
  138.             ohos:text="日" 
  139.             ohos:text_color="#C0C0C0" 
  140.             ohos:text_size="10fp" 
  141.             ohos:weight="1" 
  142.             /> 
  143.         <Text 
  144.             ohos:id="$+id:mon" 
  145.             ohos:height="match_content" 
  146.             ohos:width="match_content" 
  147.             ohos:text="一" 
  148.             ohos:text_color="#C0C0C0" 
  149.             ohos:text_size="10fp" 
  150.             ohos:weight="1" 
  151.             /> 
  152.  
  153.         <Text 
  154.             ohos:id="$+id:tue" 
  155.             ohos:height="match_content" 
  156.             ohos:width="match_content" 
  157.             ohos:text="二" 
  158.             ohos:text_color="#C0C0C0" 
  159.             ohos:text_size="10fp" 
  160.             ohos:weight="1" 
  161.             /> 
  162.  
  163.         <Text 
  164.             ohos:id="$+id:wed" 
  165.             ohos:height="match_content" 
  166.             ohos:width="match_content" 
  167.             ohos:text="三" 
  168.             ohos:text_color="#C0C0C0" 
  169.             ohos:text_size="10fp" 
  170.             ohos:weight="1" 
  171.             /> 
  172.  
  173.         <Text 
  174.             ohos:id="$+id:thu" 
  175.             ohos:height="match_content" 
  176.             ohos:width="match_content" 
  177.             ohos:text="四" 
  178.             ohos:text_color="#C0C0C0" 
  179.             ohos:text_size="10fp" 
  180.             ohos:weight="1" 
  181.             /> 
  182.  
  183.         <Text 
  184.             ohos:id="$+id:fri" 
  185.             ohos:height="match_content" 
  186.             ohos:width="match_content" 
  187.             ohos:text="五" 
  188.             ohos:text_color="#C0C0C0" 
  189.             ohos:text_size="10fp" 
  190.             ohos:weight="1" 
  191.             /> 
  192.  
  193.         <Text 
  194.             ohos:id="$+id:sat" 
  195.             ohos:height="match_content" 
  196.             ohos:width="match_content" 
  197.             ohos:text="六" 
  198.             ohos:text_color="#C0C0C0" 
  199.             ohos:text_size="10fp" 
  200.             ohos:weight="1" 
  201.             /> 
  202.     </DirectionalLayout> 
  203. </DependentLayout> 

form_image_with_info_date_card_2_4.xml #2x4规格的卡片

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DependentLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:background_element="#FFFFFFFF" 
  7.     ohos:remote="true"
  8.  
  9.     <DependentLayout 
  10.         ohos:height="match_content" 
  11.         ohos:width="match_parent" 
  12.         ohos:orientation="horizontal" 
  13.         > 
  14.  
  15.         <Text 
  16.             ohos:id="$+id:date" 
  17.             ohos:height="match_content" 
  18.             ohos:width="match_content" 
  19.             ohos:align_parent_left="true" 
  20.             ohos:margin="10fp" 
  21.             ohos:text="2021-03-24" 
  22.             ohos:text_font="sans-serif-medium" 
  23.             ohos:text_size="20fp" 
  24.             /> 
  25.  
  26.         <DirectionalLayout 
  27.             ohos:height="match_content" 
  28.             ohos:width="match_content" 
  29.             ohos:align_parent_right="true" 
  30.             ohos:orientation="horizontal" 
  31.             ohos:top_margin="10fp" 
  32.             > 
  33.  
  34.             <Text 
  35.                 ohos:id="$+id:sun" 
  36.                 ohos:height="match_content" 
  37.                 ohos:width="match_content" 
  38.                 ohos:text="日" 
  39.                 ohos:text_color="#C0C0C0" 
  40.                 ohos:text_size="20fp" 
  41.                 ohos:weight="1" 
  42.                 /> 
  43.  
  44.             <Text 
  45.                 ohos:id="$+id:mon" 
  46.                 ohos:height="match_content" 
  47.                 ohos:width="match_content" 
  48.                 ohos:text="一" 
  49.                 ohos:text_color="#C0C0C0" 
  50.                 ohos:text_size="20fp" 
  51.                 ohos:weight="1" 
  52.                 /> 
  53.  
  54.             <Text 
  55.                 ohos:id="$+id:tue" 
  56.                 ohos:height="match_content" 
  57.                 ohos:width="match_content" 
  58.                 ohos:text="二" 
  59.                 ohos:text_color="#C0C0C0" 
  60.                 ohos:text_size="20fp" 
  61.                 ohos:weight="1" 
  62.                 /> 
  63.  
  64.             <Text 
  65.                 ohos:id="$+id:wed" 
  66.                 ohos:height="match_content" 
  67.                 ohos:width="match_content" 
  68.                 ohos:text="三" 
  69.                 ohos:text_color="#C0C0C0" 
  70.                 ohos:text_size="20fp" 
  71.                 ohos:weight="1" 
  72.                 /> 
  73.  
  74.             <Text 
  75.                 ohos:id="$+id:thu" 
  76.                 ohos:height="match_content" 
  77.                 ohos:width="match_content" 
  78.                 ohos:text="四" 
  79.                 ohos:text_color="#C0C0C0" 
  80.                 ohos:text_size="20fp" 
  81.                 ohos:weight="1" 
  82.                 /> 
  83.  
  84.             <Text 
  85.                 ohos:id="$+id:fri" 
  86.                 ohos:height="match_content" 
  87.                 ohos:width="match_content" 
  88.                 ohos:text="五" 
  89.                 ohos:text_color="#C0C0C0" 
  90.                 ohos:text_size="20fp" 
  91.                 ohos:weight="1" 
  92.                 /> 
  93.  
  94.             <Text 
  95.                 ohos:id="$+id:sat" 
  96.                 ohos:height="match_content" 
  97.                 ohos:width="match_content" 
  98.                 ohos:text="六" 
  99.                 ohos:text_color="#C0C0C0" 
  100.                 ohos:text_size="20fp" 
  101.                 ohos:weight="1" 
  102.                 ohos:right_margin="10fp" 
  103.                 /> 
  104.         </DirectionalLayout> 
  105.     </DependentLayout> 
  106.  
  107.     <DirectionalLayout 
  108.         ohos:id="$+id:title" 
  109.         ohos:height="match_content" 
  110.         ohos:width="match_parent" 
  111.         ohos:alignment="horizontal_center" 
  112.         ohos:orientation="horizontal" 
  113.         ohos:top_margin="35fp" 
  114.         > 
  115.  
  116.         <Text 
  117.             ohos:height="match_content" 
  118.             ohos:width="match_content" 
  119.  
  120.             ohos:top_margin="20vp" 
  121.             ohos:left_margin="20vp" 
  122.             ohos:right_margin="20vp" 
  123.             ohos:text="HOUR" 
  124.             ohos:text_size="20fp" 
  125.             /> 
  126.  
  127.         <Text 
  128.             ohos:height="match_content" 
  129.             ohos:width="match_content" 
  130.  
  131.             ohos:top_margin="20vp" 
  132.             ohos:left_margin="20vp" 
  133.             ohos:right_margin="20vp" 
  134.             ohos:text="MIN" 
  135.             ohos:text_size="20fp" 
  136.             /> 
  137.  
  138.         <Text 
  139.             ohos:height="match_content" 
  140.             ohos:width="match_content" 
  141.             ohos:top_margin="20vp" 
  142.             ohos:left_margin="20vp" 
  143.             ohos:right_margin="20vp" 
  144.             ohos:text="SEC" 
  145.             ohos:text_size="20fp" 
  146.             /> 
  147.     </DirectionalLayout> 
  148.  
  149.     <DirectionalLayout 
  150.         ohos:id="$+id:time" 
  151.         ohos:height="match_content" 
  152.         ohos:width="match_parent" 
  153.         ohos:alignment="horizontal_center" 
  154.         ohos:below="$id:title" 
  155.         ohos:orientation="horizontal" 
  156.  
  157.         > 
  158.  
  159.         <Text 
  160.             ohos:id="$+id:hour" 
  161.             ohos:height="match_content" 
  162.             ohos:width="match_content" 
  163.             ohos:left_margin="20vp" 
  164.             ohos:right_margin="10vp" 
  165.             ohos:text="00" 
  166.             ohos:text_font="HwChinese-medium" 
  167.             ohos:text_size="40fp" 
  168.             ohos:top_margin="2vp" 
  169.  
  170.             /> 
  171.  
  172.         <Text 
  173.             ohos:height="match_content" 
  174.             ohos:width="match_content" 
  175.             ohos:text=":" 
  176.             ohos:text_font="bold" 
  177.             ohos:text_size="40fp" 
  178.             ohos:top_margin="2vp" 
  179.             /> 
  180.  
  181.         <Text 
  182.             ohos:id="$+id:min" 
  183.             ohos:height="match_content" 
  184.             ohos:width="match_content" 
  185.             ohos:left_margin="10vp" 
  186.             ohos:right_margin="10vp" 
  187.             ohos:text="00" 
  188.             ohos:text_font="HwChinese-medium" 
  189.             ohos:text_size="40fp" 
  190.             ohos:top_margin="2vp" 
  191.             /> 
  192.  
  193.         <Text 
  194.             ohos:height="match_content" 
  195.             ohos:width="match_content" 
  196.             ohos:text=":" 
  197.             ohos:text_font="bold" 
  198.             ohos:text_size="40fp" 
  199.             ohos:top_margin="2vp" 
  200.             /> 
  201.  
  202.         <Text 
  203.             ohos:id="$+id:sec" 
  204.             ohos:height="match_content" 
  205.             ohos:width="match_content" 
  206.             ohos:left_margin="10vp" 
  207.             ohos:right_margin="10vp" 
  208.             ohos:text="00" 
  209.             ohos:text_font="HwChinese-medium" 
  210.             ohos:text_size="40fp" 
  211.             ohos:top_margin="2vp" 
  212.             /> 
  213.     </DirectionalLayout> 
  214.  
  215.  
  216. </DependentLayout> 

知识点讲解

1.对象关系映射型数据库的使用,如何查询、创建卡片、删除卡片

首先使用注解定义一张Form表,继承自 OrmObject

定义对象关系型数据库FormDatabase,继承自OrmDatabase

使用DatabaseHelper类获取数据库连接(上下文)OrmContext

操作数据库,使用OrmPredicates组装条件查询卡片,创建Form卡片、删除Form卡片

在MainAbility中,创建卡片和删除卡片的回调函数中onCreateForm/onDeleteForm 构建Form操作卡片

要用到ProviderFormInfo/ComponentProvider。

2.如何更新卡片数据

定时器-----查询卡片,遍历(可能有多个)----使用ComponentProvider封装数据-----调用updateForm方法执行更新。

归纳总结

Q1.服务卡片的规格,也就是22/24 是由layout布局文件决定的吗,有啥区别?

规格主要由config.json 的配置决定,有三处landscapeLayouts、supportDimensions、portraitLayouts,新增规格也要增加对应规格的布局文件,但是规格并不是由布局文件本身决定的。

*说明:三处规格的顺序要一致,否则预览时显示也是有问题的。

config.json

  1.   "landscapeLayouts": [ 
  2.     "$layout:form_image_with_info_date_card_1_2"
  3.     "$layout:form_image_with_info_date_card_2_2"
  4.     "$layout:form_image_with_info_date_card_2_4" 
  5.  
  6.   ], 
  7.   "isDefault"true
  8.   "scheduledUpdateTime""10:30"
  9.   "defaultDimension""2*4"
  10.   "name""DateCard"
  11.   "description""This is a form card"
  12.   "colorMode""light"
  13.   "type""Java"
  14.   "supportDimensions": [ 
  15.     "1*2"
  16.     "2*2"
  17.     "2*4" 
  18.   ], 
  19.   "portraitLayouts": [ 
  20.     "$layout:form_image_with_info_date_card_1_2"
  21.     "$layout:form_image_with_info_date_card_2_2"
  22.     "$layout:form_image_with_info_date_card_2_4" 
  23.   ], 
  24.   "updateEnabled"true
  25.   "updateDuration": 1, 
  26.   "formVisibleNotify"true 

布局文件的命名也不影响规格,但是建议命名统一采用 xxxxx_card_1_2.xml/xxxxx_card_2_2.xml ,支持的规格是个枚举值,参照进行定义就好。

  1. "defaultDimension": { 
  2. "description""This label identifies the default appearance specifications of the card."
  3. "type""string"
  4. "enum": [ 
  5.   "1*2"
  6.   "2*2"
  7.   "2*4"
  8.   "4*4" 

Q2.为什么要更新卡片到DB ?

1.卡片信息需要保存在数据库中,使用OrmContext的delete/insert来操作db中的卡片

2.卡片的数据不需要更新到db,只是更新到显示,使用updateForm方法更新卡片数据的显示

Q3.如何新增一个规格的卡片?

下面以增加一个1*2规格的卡片为例:

1.config.json ,有三处 landscapeLayouts、supportDimensions、portraitLayouts 要增加

  1. "forms": [ 
  2.   { 
  3.     "landscapeLayouts": [ 
  4.       "$layout:form_image_with_info_date_card_1_2"
  5.       "$layout:form_image_with_info_date_card_2_2"
  6.       "$layout:form_image_with_info_date_card_2_4" 
  7.  
  8.     ], 
  9.     "isDefault"true
  10.     "scheduledUpdateTime""10:30"
  11.     "defaultDimension""2*4"
  12.     "name""DateCard"
  13.     "description""This is a form card"
  14.     "colorMode""dark"
  15.     "type""Java"
  16.     "supportDimensions": [ 
  17.       "1*2"
  18.       "2*2"
  19.       "2*4" 
  20.     ], 
  21.     "portraitLayouts": [ 
  22.       "$layout:form_image_with_info_date_card_1_2"
  23.       "$layout:form_image_with_info_date_card_2_2"
  24.       "$layout:form_image_with_info_date_card_2_4" 
  25.     ], 
  26.     "updateEnabled"true
  27.     "updateDuration": 1, 
  28.     "formVisibleNotify"true 
  29.   } 

2.layout

layout下新增form_image_with_info_date_card_1_2.xml 布局文件

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DependentLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:background_element="#FFFFFFFF" 
  7.     ohos:remote="true"
  8.  
  9.     <DependentLayout 
  10.         ohos:height="match_content" 
  11.         ohos:width="match_parent" 
  12.         ohos:orientation="vertical" 
  13.         ohos:vertical_center="true" 
  14.         > 
  15.         <Text 
  16.             ohos:id="$+id:date" 
  17.             ohos:height="match_content" 
  18.             ohos:width="match_content" 
  19.             ohos:center_in_parent="true" 
  20.             ohos:text="2021-03-24" 
  21.             ohos:text_alignment="vertical_center" 
  22.             ohos:text_font="sans-serif-medium" 
  23.             ohos:text_size="20fp" 
  24.             /> 
  25.  
  26.     </DependentLayout> 
  27. </DependentLayout> 

3.ComponentProviderUtils.java

更新几处与规格相关的代码

  1. /** 
  2.  * 获取对应规格的布局文件 
  3.  * @param dimension 
  4.  * @return 
  5.  */ 
  6. public static int getLayoutId(int dimension){ 
  7.     int layoutId = 0; 
  8.     if (dimension == DEFAULT_DIMENSION_1X2) { 
  9.         HiLog.info(LABEL,"获取1*2的布局文件"); 
  10.         layoutId = ResourceTable.Layout_form_image_with_info_date_card_1_2; 
  11.     } 
  12.     if (dimension == DEFAULT_DIMENSION_2X2) { 
  13.         HiLog.info(LABEL,"获取1*2的布局文件"); 
  14.         layoutId = ResourceTable.Layout_form_image_with_info_date_card_2_2; 
  15.     } 
  16.     if (dimension == DEFAULT_DIMENSION_2X4) { 
  17.         HiLog.info(LABEL,"获取2*4的布局文件"); 
  18.         layoutId = ResourceTable.Layout_form_image_with_info_date_card_2_4; 
  19.     } 
  20.     return  layoutId; 
  21.  
  22. /** 
  23.  * Set the value of componentProvider 
  24.  * 根据规格来更新卡片信息 
  25.  * @param componentProvider component provider 
  26.  */ 
  27. private static void setComponentProviderValue(ComponentProvider componentProvider,int layoutId) { 
  28.     Calendar now = Calendar.getInstance(); 
  29.     int hour = now.get(Calendar.HOUR_OF_DAY); 
  30.     int min = now.get(Calendar.MINUTE); 
  31.     int second = now.get(Calendar.SECOND); 
  32.     String hourString = int2String(hour); 
  33.     String minString = int2String(min); 
  34.     String secondString = int2String(second); 
  35.  
  36.     switch (layoutId){ 
  37.  
  38.         //1*2布局只有个日期组件 
  39.         case ResourceTable.Layout_form_image_with_info_date_card_1_2: 
  40.             componentProvider.setText(ResourceTable.Id_date, DateUtils.getCurrentDate("yyyy-MM-dd")); 
  41.             componentProvider.setTextColor(ResourceTable.Id_date,nowWeekColor); 
  42.         default
  43.             componentProvider.setText(ResourceTable.Id_date, DateUtils.getCurrentDate("yyyy-MM-dd")); 
  44.             componentProvider.setText(ResourceTable.Id_hour, hourString); 
  45.             componentProvider.setText(ResourceTable.Id_min, minString); 
  46.             componentProvider.setText(ResourceTable.Id_sec, secondString); 
  47.             // 获取当前星期 
  48.             int weekDayId = getWeekDayId(); 
  49.             componentProvider.setTextColor(weekDayId, nowWeekColor); 
  50.  
  51.             // 将前一天的星期改回原色 
  52.             int lastWeekId = getLastWeekDayId(); 
  53.             componentProvider.setTextColor(lastWeekId, primaryWeekColor); 
  54.  
  55.     } 

Q4.ClockCardSlice.java和TimerAbility.java 区别?

1.ClockCardSlice.java 代表的是主能力页,是一个FA,负责主能力页的组件的初始化以及数据的定时更新,使用了form_image_with_info_date_card_2_4.xml 的布局。

2.TimerAbility.java 是一个PA,是一个后台服务,负责不同规格卡片上组件的初始化以及数据的定时更新,点击卡片会打开主能力页。

样例中二者都是实现了组件的初始化和数据定时更新,但相互独立。

效果展示

2x4规格2x2规格1x2规格

文章相关附件可以点击下面的原文链接前往下载

原文链接:https://harmonyos.51cto.com/posts/4776

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-06-21 15:21:52

鸿蒙HarmonyOS应用开发

2021-12-15 07:24:56

分布式系统时钟

2023-08-04 17:24:43

2023-09-04 17:48:06

2023-06-09 15:24:50

UiTest接口鸿蒙

2021-09-18 09:57:20

鸿蒙HarmonyOS应用

2013-07-29 04:57:47

iOS开发iOS开发学习时间设置和格式输出

2009-02-04 15:43:45

敏捷开发PHPFleaPHP

2021-08-23 10:12:41

鸿蒙HarmonyOS应用

2023-06-20 15:45:15

服务卡片鸿蒙

2021-08-18 10:06:33

鸿蒙HarmonyOS应用

2022-07-20 15:32:25

时钟翻页Text组件

2021-07-28 14:59:08

鸿蒙HarmonyOS应用

2022-06-16 14:41:50

APP应用鸿蒙

2010-05-21 16:24:05

Windows Pho

2022-04-24 15:26:38

服务卡鸿蒙

2021-09-15 10:19:15

鸿蒙HarmonyOS应用

2020-12-04 09:52:22

鸿蒙OS

2023-05-11 14:59:16

鸿蒙开源

2020-12-08 09:45:33

鸿蒙OS应用开发
点赞
收藏

51CTO技术栈公众号