首页 > 移动平台 > 详细

android----ToggleButton&Switch

时间:2014-02-19 21:28:19      阅读:409      评论:0      收藏:0      [点我收藏+]

XML代码:

bubuko.com,布布扣
<ToggleButton
        android:id="@+id/firstToggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/sexGroup"
        android:layout_marginLeft="61dp"
        android:layout_marginTop="50dp"
     //android:textOn&Off是设置按钮开启时或关闭时显示的文字 android:textOn="ON" android:textOff="OFF" android:checked="true"/> <ToggleButton android:id="@+id/secondToggle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/firstToggle" android:layout_below="@+id/firstToggle" android:layout_marginTop="23dp" android:textOn="ON" android:textOff="OFF" /> <Switch android:id="@+id/mySwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/secondToggle" android:layout_below="@+id/secondToggle" android:layout_marginTop="40dp" android:textOn="ON" android:textOff="OFF"/>
bubuko.com,布布扣

java代码:
注意这里使用的是CompoundButton.OnCheckedChangeListener

bubuko.com,布布扣
firstToggle=(ToggleButton)findViewById(R.id.firstToggle);
        firstToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this, "你打开了--->>", 2).show();
                }else{
                    Toast.makeText(MainActivity.this, "你关上了--->>", 2).show();
                }
            }
        });
        secondToggle=(ToggleButton)findViewById(R.id.secondToggle);
        secondToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                System.out.println("---->>"+isChecked);
                if(isChecked){
                    Toast.makeText(MainActivity.this, "你打开了--->>", 2).show();
                }else{
                    Toast.makeText(MainActivity.this, "你关上了--->>", 2).show();
                }
            }
        });
        mySwitch=(Switch)findViewById(R.id.mySwitch);
        mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton btn, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(MainActivity.this, "你打开了--->>", 2).show();
                }else{
                    Toast.makeText(MainActivity.this, "你关上了--->>", 2).show();
                }
            }
        });
bubuko.com,布布扣

android----ToggleButton&Switch

原文:http://www.cnblogs.com/zaqn/p/3555699.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!