文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

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

2024-12-03 03:24

关注

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

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

https://harmonyos.51cto.com

时钟FA卡片开发样例

介绍

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

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

知识点:

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

如何更新卡片数据

搭建环境

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

form_image_with_info_date_card_2_2.xml #2x2规格的卡片

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

form_image_with_info_date_card_2_4.xml #2x4规格的卡片

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

知识点讲解

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 ?

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

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

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

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

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.   } 

layout

layout下新增form_image_with_info_date_card_1_2.xml 布局文件

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

ComponentProviderUtils.java

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

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

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

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

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

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

效果展示

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

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

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

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

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

https://harmonyos.51cto.com

 

来源:鸿蒙社区内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯