Harmony OS — Switch开关状态
2023-09-14 09:13:54 时间
1、Switch 是什么?
简单:状态组件
官方:Switch是切换单个设置开/关两种状态的组件
2、简单使用
<Switch
ohos:id="$+id:btn_switch"
ohos:height="60vp"
ohos:width="100vp"
ohos:top_margin="50fp"
ohos:layout_alignment="center"
ohos:text_state_off="OFF"
ohos:text_state_on="ON"
/>
3、常用属性
(1)设置Switch在开启和关闭时的文本
<Switch
...
ohos:text_state_off="OFF"
ohos:text_state_on="ON"/>
Switch btnSwitch = (Switch) findComponentById(ResourceTable.Id_btn_switch);
btnSwitch.setStateOffText("OFF");
btnSwitch.setStateOnText("ON");
(2)设置响应Switch状态改变的事件
btnSwitch.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
// 回调处理Switch状态改变事件
@Override
public void onCheckedChanged(AbsButton button, boolean isChecked) {
}
});
3、实战:自定义Switch
<Switch
ohos:id="$+id:btn_switch"
ohos:height="60vp"
ohos:width="100vp"
ohos:top_margin="50fp"
ohos:layout_alignment="center"
ohos:text_state_off="OFF"
ohos:text_state_on="ON"
/>
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//创建shape实例,并设置样式
// 开启状态下滑块的样式
ShapeElement elementThumbOn = new ShapeElement();
elementThumbOn.setShape(ShapeElement.OVAL);
elementThumbOn.setRgbColor(RgbColor.fromArgbInt(0xFF1E90FF));
elementThumbOn.setCornerRadius(50);
// 关闭状态下滑块的样式
ShapeElement elementThumbOff = new ShapeElement();
elementThumbOff.setShape(ShapeElement.OVAL);
elementThumbOff.setRgbColor(RgbColor.fromArgbInt(0xFFFFFFFF));
elementThumbOff.setCornerRadius(50);
// 开启状态下轨迹样式
ShapeElement elementTrackOn = new ShapeElement();
elementTrackOn.setShape(ShapeElement.RECTANGLE);
elementTrackOn.setRgbColor(RgbColor.fromArgbInt(0xFF87CEFA));
elementTrackOn.setCornerRadius(50);
// 关闭状态下轨迹样式
ShapeElement elementTrackOff = new ShapeElement();
elementTrackOff.setShape(ShapeElement.RECTANGLE);
elementTrackOff.setRgbColor(RgbColor.fromArgbInt(0xFF808080));
elementTrackOff.setCornerRadius(50);
Switch btnSwitch = (Switch) findComponentById(ResourceTable.Id_btn_switch);
btnSwitch.setTrackElement(trackElementInit(elementTrackOn, elementTrackOff));
btnSwitch.setThumbElement(thumbElementInit(elementThumbOn, elementThumbOff));
}
//设置轨迹样式
private StateElement trackElementInit(ShapeElement on, ShapeElement off){
StateElement trackElement = new StateElement();
trackElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, on);
trackElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, off);
return trackElement;
}
//设置滑块样式
private StateElement thumbElementInit(ShapeElement on, ShapeElement off) {
StateElement thumbElement = new StateElement();
thumbElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, on);
thumbElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, off);
return thumbElement;
}
4、了解更多
相关文章
- 【【henuacm2016级暑期训练】动态规划专题 M】Little Pony and Harmony Chest
- 【53.57%】【codeforces 610C】Harmony Analysis
- Open Harmony移植:build lite配置目录全梳理
- Open Harmony移植:build lite编译构建过程
- cellchat_xfy_sepsis 标准流程harmony standard pipelines
- harmony与sct与harmony seurat中的 SCT连用
- harmony去批次整合模态数据seurat findintegrationanchors transferdata reference-based label transfer注释超大数据集整合
- 标准流程surat subset_data allmerge Seurat流程 碎石图ElbowPlot harmony
- open harmony
- Harmony OS — Text文本框
- Harmony OS — TextField输入框
- Harmony OS — Image图片
- Harmony OS — TabList和Tab分页栏
- Harmony OS — Picker滑动选择器
- Harmony OS — DatePicker日期选择器
- Harmony OS — TimePicker时间选择器
- Harmony OS — RadioButton & RadioContainer单选按钮&单选按钮组
- Harmony OS — Checkbox多选框
- Harmony OS — ProgressBar垂直、水平进度条
- Harmony OS — RoundProgressBar圆形进度条
- Harmony OS — ToastDialog提示对话框
- Harmony OS — ScrollView滑动视图
- Harmony OS — ListContainer列表
- Harmony OS — PageSlider滑动页面
- Harmony OS — PageSliderIndicator滑动页面指示器
- harmony去批次整合模态数据seurat findintegrationanchors transferdata reference-based label transfer注释超大数据集整合
- 标准流程surat subset_data allmerge Seurat流程 碎石图ElbowPlot harmony