开关按钮布局文件
<ToggleButton android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="开"
android:textOff="关"
android:onClick="onToButton"
/>
<ToggleButton android:id="@+id/toggleButto2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/toggleButton1"
android:textOn="开"
android:textOff="关"
/>
<Switch android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/toggleButto2"
android:onClick="onSwitch"
/>
《====================================mainActivity===========================================》
public class MainActivity extends ActionBarActivity implements OnCheckedChangeListener{
ToggleButton tb,tb2;
Switch sw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb=(ToggleButton)findViewById(R.id.toggleButton1);
tb2=(ToggleButton)findViewById(R.id.toggleButto2);
tb2.setOnCheckedChangeListener(this);
sw=(Switch)findViewById(R.id.switch1);
}
//Switch事件方法
public void onSwitch(View view){
Switch sw=(Switch)view;
boolean on=sw.isChecked();
if(on){
Toast.makeText(this, "开", 0).show();
}else{
Toast.makeText(this, "关", 0).show();
}
}
//选择按钮1的事件方法
public void onToButton(View view){
ToggleButton tbn=(ToggleButton)view;
boolean on=tbn.isChecked();
if(on){
Toast.makeText(this, "开", 0).show();
}else{
Toast.makeText(this, "关", 0).show();
}
}
//实现接口的事件方法
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.toggleButto2:
if(isChecked){
Toast.makeText(this, "开",0).show();
}
else{
Toast.makeText(this, "关",0).show();
}
break;
default:break;
}
}
}
<ToggleButton android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="开"
android:textOff="关"
android:onClick="onToButton"
/>
<ToggleButton android:id="@+id/toggleButto2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/toggleButton1"
android:textOn="开"
android:textOff="关"
/>
<Switch android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/toggleButto2"
android:onClick="onSwitch"
/>
《====================================mainActivity===========================================》
public class MainActivity extends ActionBarActivity implements OnCheckedChangeListener{
ToggleButton tb,tb2;
Switch sw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb=(ToggleButton)findViewById(R.id.toggleButton1);
tb2=(ToggleButton)findViewById(R.id.toggleButto2);
tb2.setOnCheckedChangeListener(this);
sw=(Switch)findViewById(R.id.switch1);
}
//Switch事件方法
public void onSwitch(View view){
Switch sw=(Switch)view;
boolean on=sw.isChecked();
if(on){
Toast.makeText(this, "开", 0).show();
}else{
Toast.makeText(this, "关", 0).show();
}
}
//选择按钮1的事件方法
public void onToButton(View view){
ToggleButton tbn=(ToggleButton)view;
boolean on=tbn.isChecked();
if(on){
Toast.makeText(this, "开", 0).show();
}else{
Toast.makeText(this, "关", 0).show();
}
}
//实现接口的事件方法
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.toggleButto2:
if(isChecked){
Toast.makeText(this, "开",0).show();
}
else{
Toast.makeText(this, "关",0).show();
}
break;
default:break;
}
}
}