HarmonyOS APP组件分享(四)

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

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

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

https://harmonyos.51cto.com

HarmonyOS APP - RadioButton+RadioContainer体验与分享

这里敲了RadioButton+RadioContainer的一个小案例,简单做了一个多选和单选的选择题。

RadioButton我们可以用在多重选择的时候使用该组件,方便用户做出多种选择。

RadioContainer这个是用于单选,可以用于唯一答案的选择例如男女选项。

代码如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:orientation="vertical"
  7.  
  8.     <DirectionalLayout 
  9.         ohos:height="500px" 
  10.         ohos:width="match_parent" 
  11.         ohos:orientation="vertical"
  12.         <Text 
  13.             ohos:height="80px" 
  14.             ohos:width="match_parent" 
  15.             ohos:text="周末都在家干些啥呢(多选)。" 
  16.             ohos:text_size="18fp"/> 
  17.         <DirectionalLayout 
  18.             ohos:height="400px" 
  19.             ohos:width="match_parent" 
  20.             ohos:orientation="vertical"
  21.             <DirectionalLayout 
  22.                 ohos:height="100px" 
  23.                 ohos:width="match_parent" 
  24.                 ohos:orientation="horizontal"
  25.                 <RadioButton 
  26.                     ohos:id="$+id:rb_1" 
  27.                     ohos:height="40vp" 
  28.                     ohos:width="0px" 
  29.                     ohos:weight="1" 
  30.                     ohos:text="A.吃饭" 
  31.                     ohos:text_size="20fp" 
  32.                     ohos:text_color_on="#00BFFF" 
  33.                     ohos:text_color_off="#808080" 
  34.                     /> 
  35.                 <RadioButton 
  36.                     ohos:id="$+id:rb_2" 
  37.                     ohos:height="40vp" 
  38.                     ohos:width="0px" 
  39.                     ohos:weight="1" 
  40.                     ohos:text="B.睡觉" 
  41.                     ohos:text_size="20fp" 
  42.                     ohos:text_color_on="#00BFFF" 
  43.                     ohos:text_color_off="#808080"/> 
  44.             </DirectionalLayout> 
  45.             <DirectionalLayout 
  46.                 ohos:height="100px" 
  47.                 ohos:width="match_parent" 
  48.                 ohos:orientation="horizontal"
  49.                 <RadioButton 
  50.                     ohos:id="$+id:rb_3" 
  51.                     ohos:height="40vp" 
  52.                     ohos:width="0px" 
  53.                     ohos:weight="1" 
  54.                     ohos:text="C.打豆豆" 
  55.                     ohos:text_size="20fp" 
  56.                     ohos:text_color_on="#00BFFF" 
  57.                     ohos:text_color_off="#808080" 
  58.                     /> 
  59.                 <RadioButton 
  60.                     ohos:id="$+id:rb_4" 
  61.                     ohos:height="40vp" 
  62.                     ohos:width="0px" 
  63.                     ohos:weight="1" 
  64.                     ohos:text="D.学习" 
  65.                     ohos:text_size="20fp" 
  66.                     ohos:text_color_on="#00BFFF" 
  67.                     ohos:text_color_off="#808080"/> 
  68.             </DirectionalLayout> 
  69.             <DirectionalLayout 
  70.                 ohos:height="100px" 
  71.                 ohos:width="match_parent" 
  72.                 ohos:orientation="horizontal"
  73.                 <RadioButton 
  74.                     ohos:id="$+id:rb_5" 
  75.                     ohos:height="40vp" 
  76.                     ohos:width="0px" 
  77.                     ohos:weight="1" 
  78.                     ohos:text="E.看电视" 
  79.                     ohos:text_size="20fp" 
  80.                     ohos:text_color_on="#00BFFF" 
  81.                     ohos:text_color_off="#808080" 
  82.                     /> 
  83.                 <RadioButton 
  84.                     ohos:id="$+id:rb_6" 
  85.                     ohos:height="40vp" 
  86.                     ohos:width="0px" 
  87.                     ohos:weight="1" 
  88.                     ohos:text="F.敲代码" 
  89.                     ohos:text_size="20fp" 
  90.                     ohos:text_color_on="#00BFFF" 
  91.                     ohos:text_color_off="#808080"/> 
  92.             </DirectionalLayout> 
  93.         </DirectionalLayout> 
  94.     </DirectionalLayout> 
  95.     <DirectionalLayout 
  96.         ohos:height="700px" 
  97.         ohos:width="match_parent" 
  98.         ohos:orientation="vertical"
  99.         <DirectionalLayout 
  100.             ohos:height="200px" 
  101.             ohos:width="match_parent"
  102.             <Text 
  103.                 ohos:height="100px" 
  104.                 ohos:width="match_parent" 
  105.                 ohos:text="单选题(红错,蓝对)" 
  106.                 ohos:text_size="20fp"/> 
  107.             <Text 
  108.                 ohos:height="100px" 
  109.                 ohos:width="match_parent" 
  110.                 ohos:text="下面那个是属于动物?" 
  111.                 ohos:text_size="20fp"/> 
  112.         </DirectionalLayout> 
  113.         <RadioContainer 
  114.             ohos:height="500px" 
  115.             ohos:width="match_content"
  116.             <RadioButton 
  117.                 ohos:id="$+id:radio_button_1" 
  118.                 ohos:height="40vp" 
  119.                 ohos:width="match_content" 
  120.                 ohos:text="A.苹果" 
  121.                 ohos:text_size="20fp" 
  122.                 ohos:text_color_on="red" 
  123.                 ohos:text_color_off="#808080"/> 
  124.             <RadioButton 
  125.                 ohos:id="$+id:radio_button_2" 
  126.                 ohos:height="40vp" 
  127.                 ohos:width="match_content" 
  128.                 ohos:text="B.香蕉" 
  129.                 ohos:text_size="20fp" 
  130.                 ohos:text_color_on="red" 
  131.                 ohos:text_color_off="#808080"/> 
  132.             <RadioButton 
  133.                 ohos:id="$+id:radio_button_3" 
  134.                 ohos:height="40vp" 
  135.                 ohos:width="match_content" 
  136.                 ohos:text="C.小狗" 
  137.                 ohos:text_size="20fp" 
  138.                 ohos:text_color_on="#00BFFF" 
  139.                 ohos:text_color_off="#808080"/> 
  140.             <RadioButton 
  141.                 ohos:id="$+id:radio_button_4" 
  142.                 ohos:height="40vp" 
  143.                 ohos:width="match_content" 
  144.                 ohos:text="D.葡萄" 
  145.                 ohos:text_size="20fp" 
  146.                 ohos:text_color_on="red" 
  147.                 ohos:text_color_off="#808080"/> 
  148.  
  149.         </RadioContainer> 
  150.     </DirectionalLayout> 
  151.     <DirectionalLayout 
  152.         ohos:height="600px" 
  153.         ohos:width="match_parent" 
  154.         ohos:orientation="vertical"
  155.         <DirectionalLayout 
  156.             ohos:height="100px" 
  157.             ohos:width="match_parent"
  158.             <Text 
  159.                 ohos:height="match_parent" 
  160.                 ohos:width="match_parent" 
  161.                 ohos:text="下面那个是属于水果?" 
  162.                 ohos:text_size="20fp"/> 
  163.         </DirectionalLayout> 
  164.         <RadioContainer 
  165.             ohos:height="500px" 
  166.             ohos:width="match_content"
  167.             <RadioButton 
  168.                 ohos:height="40vp" 
  169.                 ohos:width="match_content" 
  170.                 ohos:text="A.小狗" 
  171.                 ohos:text_size="20fp" 
  172.                 ohos:text_color_on="red" 
  173.                 ohos:text_color_off="#808080"/> 
  174.             <RadioButton 
  175.                 ohos:height="40vp" 
  176.                 ohos:width="match_content" 
  177.                 ohos:text="B.黑凤梨" 
  178.                 ohos:text_size="20fp" 
  179.                 ohos:text_color_on="#00BFFF" 
  180.                 ohos:text_color_off="#808080"/> 
  181.             <RadioButton 
  182.                 ohos:height="40vp" 
  183.                 ohos:width="match_content" 
  184.                 ohos:text="C.小猫" 
  185.                 ohos:text_size="20fp" 
  186.                 ohos:text_color_on="red" 
  187.                 ohos:text_color_off="#808080"/> 
  188.             <RadioButton 
  189.                 ohos:height="40vp" 
  190.                 ohos:width="match_content" 
  191.                 ohos:text="D.小马" 
  192.                 ohos:text_size="20fp" 
  193.                 ohos:text_color_on="red" 
  194.                 ohos:text_color_off="#808080"/> 
  195.  
  196.         </RadioContainer> 
  197.     </DirectionalLayout> 
  198.  
  199. </DirectionalLayout> 

完整代码下载地址:

https://gitee.com/jltfcloudcn/jump_to/tree/master/jltf_RadioButton+RadioContainer_component

HarmonyOS APP - Switch体验与分享

Switch是切换单个设置开/关两种状态的组件。我们体验效果如下。

显示效果:

滑动按钮:

代码如下:

布局中

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:orientation="vertical"
  7.  
  8.     <Switch 
  9.         ohos:id="$+id:btn_switch" 
  10.         ohos:height="30vp" 
  11.         ohos:width="60vp" 
  12.         ohos:top_margin="40px" 
  13.         ohos:left_margin="400px" 
  14.         ohos:text_state_off="OFF" 
  15.         ohos:text_state_on="ON"/> 
  16.  
  17. </DirectionalLayout> 

完整源码地址:

https://gitee.com/jltfcloudcn/jump_to/tree/master/jltf_switch_component

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

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

https://harmonyos.51cto.com

 

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

2021-03-31 15:49:34

鸿蒙HarmonyOS应用

2021-03-30 09:45:07

鸿蒙HarmonyOS应用开发

2021-03-17 09:35:09

鸿蒙HarmonyOS应用开发

2022-02-10 15:14:50

HarmonyOS操作系统鸿蒙

2021-06-01 14:32:44

鸿蒙HarmonyOS应用

2021-06-22 09:44:56

鸿蒙HarmonyOS应用

2021-01-11 11:36:23

鸿蒙HarmonyOSApp开发

2021-08-12 14:59:15

鸿蒙HarmonyOS应用

2022-05-19 15:59:23

组件焦点鸿蒙

2021-10-26 15:22:52

鸿蒙HarmonyOS应用

2021-03-22 09:48:32

鸿蒙HarmonyOS应用开发

2021-07-23 08:57:32

鸿蒙HarmonyOS应用

2021-03-18 09:36:02

鸿蒙HarmonyOS应用

2021-08-05 09:49:44

鸿蒙HarmonyOS应用

2021-10-14 15:14:36

鸿蒙HarmonyOS应用

2021-08-26 15:28:05

鸿蒙HarmonyOS应用

2012-04-28 21:25:58

APP

2021-11-01 10:21:36

鸿蒙HarmonyOS应用

2022-04-24 15:17:56

鸿蒙操作系统

2021-09-06 15:31:01

鸿蒙HarmonyOS应用
点赞
收藏

51CTO技术栈公众号