ANDROID_MARS学习笔记_S01_005CheckBox

一、

1.checkbox_layout.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     <CheckBox 
 7         android:id="@+id/checkAllId"
 8         android:layout_height="wrap_content"
 9         android:layout_width="wrap_content"
10         android:text="全选"/>
11     <CheckBox
12         android:id="@+id/eatId"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:background="#ff0000"
16         android:textSize="50dp"
17         android:text="吃饭"/>
18     <CheckBox
19         android:id="@+id/sleepId" 
20         android:layout_width="wrap_content"
21         android:layout_height="wrap_content"
22         android:background="#0000ff"
23         android:textSize="50sp"
24         android:text="睡觉"/>
25 
26 </LinearLayout>

 

2.MainActivity.java

 1 @SuppressLint("NewApi")
 2 public class MainActivity extends ActionBarActivity {
 3 
 4     private TextView textView;
 5     private Button button;
 6     int count = 0;
 7     //CheckBox
 8     private CheckBox eateBox;
 9     private CheckBox sleepBox;
10     private CheckBox checkAll;
11     
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.checkbox_layout);
16         
17         //checkbox
18         eateBox = (CheckBox) findViewById(R.id.eatId);
19         sleepBox = (CheckBox) findViewById(R.id.sleepId);
20         checkAll = (CheckBox) findViewById(R.id.checkAllId);
21         OnBoxChickListener boxListener = new OnBoxChickListener();
22 //        eateBox.setOnClickListener(boxListener);
23 //        sleepBox.setOnClickListener(boxListener);
24         OnBoxChangeListener changeListener = new OnBoxChangeListener();
25         eateBox.setOnCheckedChangeListener(changeListener);
26         sleepBox.setOnCheckedChangeListener(changeListener);
27         checkAll.setOnCheckedChangeListener(changeListener);
28         
29 //        setContentView(R.layout.dpsp_layout);
30 //        setContentView(R.layout.activity_main);
31         //setContentView(R.layout.first_layout);
32         
33         /*TextView textView = (TextView) findViewById(R.id.textView);
34         textView.setText("Hello View");
35         textView.setBackgroundColor(Color.CYAN);
36         
37         button = (Button) findViewById(R.id.button);
38         ButtonListener buttonListener = new ButtonListener();
39         button.setOnClickListener(buttonListener);*/
40 
41     }
42 
43     class OnBoxChickListener implements OnClickListener {
44 
45         @Override
46         public void onClick(View v) {
47             System.out.println(v.getId());
48             CheckBox box = (CheckBox) v;
49             System.out.println(box.isChecked());
50             System.out.println("CheckBox is clicked");
51         }
52         
53     }
54     
55     class OnBoxChangeListener implements OnCheckedChangeListener {
56 
57         @Override
58         public void onCheckedChanged(CompoundButton buttonView,
59                 boolean isChecked) {
60             if(R.id.eatId == buttonView.getId()) {
61                 System.out.println("eatButton");
62             } else if(R.id.sleepId == buttonView.getId()) {
63                 System.out.println("sleepButton");
64             } else if(R.id.checkAllId == buttonView.getId()) {
65                 eateBox.setChecked(isChecked);
66                 sleepBox.setChecked(isChecked);
67             }
68             System.out.println(isChecked ? "选中" : "取消");
69         }
70         
71     }

 

你可能感兴趣的:(ANDROID_MARS学习笔记_S01_005CheckBox)