控件_CheckBox(多选按钮)

 1 import android.os.Bundle;
 2 import android.app.Activity;
 3 import android.widget.CheckBox;
 4 import android.widget.CompoundButton;
 5 import android.widget.CompoundButton.OnCheckedChangeListener;
 6 
 7 public class MainActivity extends Activity {
 8     private CheckBox swimBox = null;
 9     private CheckBox runBox = null;
10     private CheckBox readBox = null;
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_main);
14         
15         swimBox = (CheckBox) findViewById(R.id.swim);
16         runBox = (CheckBox) findViewById(R.id.run);
17         readBox = (CheckBox) findViewById(R.id.read);
18         
19         CheckBoxListener listener = new CheckBoxListener();
20         swimBox.setOnCheckedChangeListener(listener);
21         runBox.setOnCheckedChangeListener(listener);
22         readBox.setOnCheckedChangeListener(listener);
23         /*
24         //多个控件可以使用同一个监听器
25         OnBoxClickListener listener = new OnBoxClickListener();
26         swimBox.setOnClickListener(listener);
27         runBox.setOnClickListener(listener);
28         readBox.setOnClickListener(listener);*/
29     }
30     
31     //
32     class CheckBoxListener implements OnCheckedChangeListener{
33         @Override
34         public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
35             if(buttonView.getId()==R.id.read){
36                 System.out.println("read");
37             }else if(buttonView.getId()==R.id.run){
38                 System.out.println("run");
39             }else if(buttonView.getId()==R.id.swim){
40                 System.out.println("swim");
41             }
42             if(isChecked){
43                 System.out.println("Checked");
44             }else{
45                 System.out.println("UnChecked");
46             }
47         }
48         
49     }
50     
51     
52 /*    OnClickListener的使用方法
53     class OnBoxClickListener implements OnClickListener{
54         public void onClick(View view) {                //CheckBox是View的子类,所以可以接收
55             CheckBox box = (CheckBox)view;
56             if(view.getId()==R.id.read)
57                 System.out.println("read");
58             else if(view.getId()==R.id.run)
59                 System.out.println("run");
60             else if(view.getId()==R.id.swim)
61                 System.out.println("swim");
62             
63             if(box.isChecked()){//isChecked方法不是view中的方法所以要向下转型,该方法可以判段是否被选中,如果选中返回真
64                 System.out.println("Checked");
65             }else
66                 System.out.println("unChecked");
67         }
68         
69     }
70 */
71 }
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context=".MainActivity" >
11 
12     
13     
14     <CheckBox
15     android:id="@+id/swim"
16      android:layout_width="wrap_content" 
17       android:layout_height="wrap_content" 
18       android:text="吃饭"
19       />
20     <CheckBox
21     android:id="@+id/run"
22      android:layout_width="wrap_content" 
23       android:layout_height="wrap_content" 
24       android:text="打游戏"
25       />
26     <CheckBox
27     android:id="@+id/read"
28      android:layout_width="wrap_content" 
29       android:layout_height="wrap_content" 
30       android:text="睡觉"
31       />
32 </LinearLayout>

实现全选功能:

 1 import android.app.Activity;
 2 import android.os.Bundle;
 3 import android.widget.CheckBox;
 4 import android.widget.CompoundButton;
 5 import android.widget.CompoundButton.OnCheckedChangeListener;
 6 
 7 public class MainActivity extends Activity {
 8     private CheckBox all;
 9     private CheckBox swim;
10     private CheckBox run;
11     private CheckBox read;
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.activity_main);
15         
16         all = (CheckBox) findViewById(R.id.all);
17         swim = (CheckBox) findViewById(R.id.swim);
18         run = (CheckBox) findViewById(R.id.run);
19         read = (CheckBox) findViewById(R.id.read);
20         
21         AllCheckListener listener = new AllCheckListener();
22         all.setOnCheckedChangeListener(listener);
23     }
24     class AllCheckListener implements OnCheckedChangeListener{
25         public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
26             
27             swim.setChecked(isChecked);
28             run.setChecked(isChecked);
29             read.setChecked(isChecked);
30             /*
31             if(isChecked){
32                 swim.setChecked(true);
33                 run.setChecked(true);
34                 read.setChecked(true);
35             }else{
36                 swim.setClickable(false);
37                 run.setChecked(false);
38                 read.setChecked(false);
39             }
40             */
41         }
42         
43     }
44     
45 
46 }
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context=".MainActivity" >
11 
12     <CheckBox
13     android:id="@+id/all"
14      android:layout_width="wrap_content" 
15       android:layout_height="wrap_content" 
16       android:text="全选"
17       />
18     
19     <CheckBox
20     android:id="@+id/swim"
21      android:layout_width="wrap_content" 
22       android:layout_height="wrap_content" 
23       android:text="吃饭"
24       />
25     <CheckBox
26     android:id="@+id/run"
27      android:layout_width="wrap_content" 
28       android:layout_height="wrap_content" 
29       android:text="打游戏"
30       />
31     <CheckBox
32     android:id="@+id/read"
33      android:layout_width="wrap_content" 
34       android:layout_height="wrap_content" 
35       android:text="睡觉"
36       />
37 </LinearLayout>
原文地址:https://www.cnblogs.com/LO-ME/p/3584705.html