1.XML文件:
<!--textOff:关闭状态显示的文字-->
<!--textOn:开启状态显示的文字-->
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sc_main_swi"
android:textOn="关"
android:textOff="开"
/>
2.代码:
sw_main_swi = (Switch) findViewById(R.id.sc_main_swi);
//给ToggleButton设置监听事件
sw_main_swi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//开启状态
}else{
//关闭状态
}
}
});
<!--textOff:关闭状态显示的文字-->
<!--textOn:开启状态显示的文字-->
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sc_main_swi"
android:textOn="关"
android:textOff="开"
/>
2.代码:
sw_main_swi = (Switch) findViewById(R.id.sc_main_swi);
//给ToggleButton设置监听事件
sw_main_swi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//开启状态
}else{
//关闭状态
}
}
});