赞
踩
开关组件(Switch)几乎在每个APP的设置界面都有开关组件的身影,开关组件有 开启 和 关闭两种状态
默认情况下,滑块是圆形白色的,滑轨是灰色的
- 滑块设置属性:thumb_element 设置颜色
- 滑轨设置属性:track_element 设置滑轨的颜色
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:alignment="center"
- ohos:orientation="vertical">
- <!-- text_state_on:开启时的提示文字 -->
- <!-- text_state_off:关闭时的提示文字 -->
- <Switch
- ohos:height="40vp"
- ohos:width="100vp"
- ohos:text_state_on="开"
- ohos:text_state_off="关"
- ohos:text_size="20fp"
- />
-
- </DirectionalLayout>

为一个组件绑定监听事件的时候,出现多个可以实现监听事件的接口时,确定需要实现哪一个接口的方法就是找到该组件的父类,实现它父类中的监听事件
- public class MainAbilitySlice extends AbilitySlice implements AbsButton.CheckedStateChangedListener {
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
-
- //1、找到组件
- Switch choose = (Switch) findComponentById(ResourceTable.Id_choose);
-
- //给开关组件绑定事件
- //单击事件
- //状态绑定事件
- choose.setCheckedStateChangedListener(this);
-
- }
-
- @Override
- public void onActive() {
- super.onActive();
- }
-
- @Override
- public void onForeground(Intent intent) {
- super.onForeground(intent);
- }
-
- //监听事件方法
- //当开关组件状态发生改变的时候,就会触发该方法
- //参数1:状态改变的组件
- //参数2:组件的当前状态
- @Override
- public void onCheckedChanged(AbsButton absButton, boolean b) {
- if(b) {
- MyToast.showDialog(this, "开关开启了");
- } else {
- MyToast.showDialog(this, "开关关闭了");
- }
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。