开关按钮ToggleButton与Switch

开关按钮布局文件
<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;
}

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值