1、简介
ToggleButton和Switch都是开关按钮,只不过Switch要Android4.0之后才能使用!
ToggleButton Switch
2、简单使用
xml布局文件
Java文件,点击事件处理:
ToggleButton toggleButton = (ToggleButton)findViewById(R.id.toggle); toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ Log.d("toggle","打开了"); }else { Log.d("switch","关闭了"); } } }); final Switch switch1 = (Switch)findViewById(R.id.switch11); switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ Log.d("switch","开了"); }else { Log.d("switch","关了"); } } });