赞
踩
<?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="horizontal">
<Switch
ohos:id="$+id:choose"
ohos:height="40vp"
ohos:width="100vp"
ohos:text_state_on="开"
ohos:text_state_off="关"
ohos:text_size="20vp"
/>
<!--
设置滑块
ohos:thumb_element="#07C160"
设置滑轨
ohos:track_element="#FF0000"-->
</DirectionalLayout>
package com.example.switchapplication.slice;
import com.example.switchapplication.ResourceTable;
import com.example.switchapplication.dialogutils.ToastUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Component;
import ohos.agp.components.Switch;
public class MainAbilitySlice extends AbilitySlice implements AbsButton.CheckedStateChangedListener {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//找到开关组件
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);
}
//当开关组件状态发生改变的时候,那么就会调用这个方法
//参数一:表示状态改变的那个开关组件
//参数二:表示组件当前的状态
@Override
public void onCheckedChanged(AbsButton absButton, boolean b) {
if(b){
//ToastUtils.showDialog(this,"开关开启了");
//可以打开某个设置
}else{
//ToastUtils.showDialog(this,"开关关闭了");
//可以关闭某个设置
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。