switch控件的使用

java中:

 1 public class MainActivity extends Activity implements OnCheckedChangeListener{
 2 
 3     private Switch aSwitch;
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9         aSwitch = (Switch)findViewById(R.id.switch1);
10         aSwitch.setOnCheckedChangeListener(this);
11     }
12     @Override
13     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
14         if(isChecked){
15             Log.e("", "checked");
16         }
17         if(!isChecked){
18             Log.e("", "not checked");
19         }
20 
21     }

xml中:

1             <Switch
2                 android:layout_width="wrap_content"
3                 android:layout_height="wrap_content"
4                 android:id="@+id/switch1"
5                 android:checked="true" />

注意记住要再onCreate时.setOnCheckedChangeListener(this)

posted @ 2015-10-09 10:14  龟窝  阅读(456)  评论(0编辑  收藏  举报