鸿蒙应用开发 | 揭开神秘 开关(Switch)的功能与用法

鸿蒙应用开关(Switch)功能与用法揭秘

原文链接
下面我们开始今天的文章,还是老规矩,通过如下几点来说:
 

1,简介 
2,用到的属性 
3,实战

简介
Switch是一个可以在两种状态切换的开关控件。只有开关两种状态。

一般引用在手机应用的设置界面 做一些开关操作,或者某一个功能的开关操作。

用到的属性
Switch的共有XML属性还是继承自:Text

属性中 有两个比较重要 :
text_state_on // 开启显示的文本
text_state_off //关闭显示的文本

实战

先来创建一个基本的开关试试把。
1,开关默认效果

 
  1. <Switch

  2. ohos:id="$+id:switch_opt"

  3. ohos:height="match_content"

  4. ohos:width="match_content"

  5. ohos:text_state_off="关闭"

  6. ohos:text_state_on="开启"

  7. ohos:padding="5vp"

  8. ohos:text_size="16fp"

  9. />

1,默认效果 添加了 几个属性,开启和关闭文字,字体大小

查看一下效果:

2,设置背景样式

ShapeElement elementThumbOn = new ShapeElement();
elementThumbOn.setShape(ShapeElement.OVAL);
elementThumbOn.setRgbColor(RgbColor.fromArgbInt(0xFF1E90FF));
elementThumbOn.setCornerRadius(50);

1,设置背景演示和圆角

3,设置背景状态

 
  1. // 滑块样式的设置

  2. private StateElement trackElementInit(ShapeElement on, ShapeElement off){

  3. StateElement trackElement = new StateElement();

  4. trackElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, on);

  5. trackElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, off);

  6. return trackElement;

  7. }

1. 设置状态,状态是两种背景样式设置的切换

完整代码:

 
  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:alignment="center"

  7. ohos:orientation="vertical">

  8. <Switch

  9. ohos:id="$+id:switch_opt"

  10. ohos:height="match_content"

  11. ohos:width="match_content"

  12. ohos:text_state_off="关闭"

  13. ohos:text_state_on="开启"

  14. ohos:padding="5vp"

  15. ohos:text_size="16fp"

  16. />

  17. </DirectionalLayout>

代码逻辑:

 
  1. package com.example.aswitch.slice;

  2. import com.example.aswitch.ResourceTable;

  3. import ohos.aafwk.ability.AbilitySlice;

  4. import ohos.aafwk.content.Intent;

  5. import ohos.agp.colors.RgbColor;

  6. import ohos.agp.components.ComponentState;

  7. import ohos.agp.components.Switch;

  8. import ohos.agp.components.element.ShapeElement;

  9. import ohos.agp.components.element.StateElement;

  10. public class MainAbilitySlice extends AbilitySlice {

  11. @Override

  12. public void onStart(Intent intent) {

  13. super.onStart(intent);

  14. super.setUIContent(ResourceTable.Layout_ability_main);

  15. // 开启状态下滑块的样式

  16. ShapeElement elementThumbOn = new ShapeElement();

  17. elementThumbOn.setShape(ShapeElement.OVAL);

  18. elementThumbOn.setRgbColor(RgbColor.fromArgbInt(0xFF1E90FF));

  19. elementThumbOn.setCornerRadius(50);

  20. // 关闭状态下滑块的样式

  21. ShapeElement elementThumbOff = new ShapeElement();

  22. elementThumbOff.setShape(ShapeElement.OVAL);

  23. elementThumbOff.setRgbColor(RgbColor.fromArgbInt(0xFFFFFFFF));

  24. elementThumbOff.setCornerRadius(50);

  25. // 开启状态下轨迹样式

  26. ShapeElement elementTrackOn = new ShapeElement();

  27. elementTrackOn.setShape(ShapeElement.RECTANGLE);

  28. elementTrackOn.setRgbColor(RgbColor.fromArgbInt(0xFF87CEFA));

  29. elementTrackOn.setCornerRadius(50);

  30. // 关闭状态下轨迹样式

  31. ShapeElement elementTrackOff = new ShapeElement();

  32. elementTrackOff.setShape(ShapeElement.RECTANGLE);

  33. elementTrackOff.setRgbColor(RgbColor.fromArgbInt(0xFF808080));

  34. elementTrackOff.setCornerRadius(50);

  35. // 设置切换属性

  36. Switch btnSwitch = (Switch) findComponentById(ResourceTable.Id_switch_opt);

  37. btnSwitch.setTrackElement(trackElementInit(elementTrackOn, elementTrackOff));

  38. btnSwitch.setThumbElement(thumbElementInit(elementThumbOn, elementThumbOff));

  39. }

  40. // 滑块样式的设置

  41. private StateElement trackElementInit(ShapeElement on, ShapeElement off){

  42. StateElement trackElement = new StateElement();

  43. trackElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, on);

  44. trackElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, off);

  45. return trackElement;

  46. }

  47. // 轨迹样式的设置

  48. private StateElement thumbElementInit(ShapeElement on, ShapeElement off) {

  49. StateElement thumbElement = new StateElement();

  50. thumbElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, on);

  51. thumbElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, off);

  52. return thumbElement;

  53. }

  54. @Override

  55. public void onActive() {

  56. super.onActive();

  57. }

  58. @Override

  59. public void onForeground(Intent intent) {

  60. super.onForeground(intent);

  61. }

  62. }

源码:
Switch : https://gitee.com/codegrowth/haomony-develop/tree/master/Switch

————————————————

                            版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
                        
原文链接:https://blog.youkuaiyun.com/jianpengxuexikaifa/article/details/118399361

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值