checkBox

1.代码

  1 CheckBoxActivity:
2 package com.vanceinfo.CheckBox;
3
4 import android.app.Activity;
5 import android.os.Bundle;
6 import android.view.Gravity;
7 import android.view.View;
8 import android.view.View.OnClickListener;
9 import android.widget.Button;
10 import android.widget.CheckBox;
11 import android.widget.CompoundButton;
12 import android.widget.CompoundButton.OnCheckedChangeListener;
13 import android.widget.Toast;
14
15 public class CheckBoxActivity extends Activity
16 {
17 private CheckBox mCheckBox01, mCheckBox02, mCheckBox03, mCheckBox04, mCheckBox05;
18 private Button button;
19
20 @Override
21 public void onCreate(Bundle savedInstanceState)
22 {
23 super.onCreate(savedInstanceState);
24 setContentView(R.layout.main);
25 button = (Button) findViewById(R.id.button);
26 // 取得每一个CheckBox对象
27 mCheckBox01 = (CheckBox) findViewById(R.id.checkBox01);
28 mCheckBox02 = (CheckBox) findViewById(R.id.checkBox02);
29 mCheckBox03 = (CheckBox) findViewById(R.id.checkBox03);
30 mCheckBox04 = (CheckBox) findViewById(R.id.checkBox04);
31 mCheckBox05 = (CheckBox) findViewById(R.id.checkBox05);
32 // 对每个选择设置监听事件
33 mCheckBox01.setOnCheckedChangeListener(listener);
34 mCheckBox02.setOnCheckedChangeListener(listener);
35 mCheckBox03.setOnCheckedChangeListener(listener);
36 mCheckBox04.setOnCheckedChangeListener(listener);
37 mCheckBox05.setOnCheckedChangeListener(listener);
38 // 对按钮设置监听事件
39 button.setOnClickListener(l);
40 }
41
42 OnCheckedChangeListener listener = new OnCheckedChangeListener()
43 {
44 @Override
45 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
46 {
47 if (mCheckBox01.isChecked())
48 {
49 DisplayToast("你选择了:" + mCheckBox01.getText());
50 }
51 if (mCheckBox02.isChecked())
52 {
53 DisplayToast("你选择了:" + mCheckBox02.getText());
54 }
55 if (mCheckBox03.isChecked())
56 {
57 DisplayToast("你选择了:" + mCheckBox03.getText());
58 }
59 if (mCheckBox04.isChecked())
60 {
61 DisplayToast("你选择了:" + mCheckBox04.getText());
62 }
63 if (mCheckBox05.isChecked())
64 {
65 DisplayToast("你选择了:" + mCheckBox05.getText());
66 }
67 }
68 };
69
70 OnClickListener l = new OnClickListener()
71 {
72 @Override
73 public void onClick(View v)
74 {
75 int num = 0;
76 if (mCheckBox01.isChecked())
77 {
78 num++;
79 }
80 if (mCheckBox02.isChecked())
81 {
82 num++;
83 }
84 if (mCheckBox03.isChecked())
85 {
86 num++;
87 }
88 if (mCheckBox04.isChecked())
89 {
90 num++;
91 }
92 if (mCheckBox05.isChecked())
93 {
94 num++;
95 }
96 DisplayToast("您一共选择了" + num + "项");
97 }
98 };
99
100 public void DisplayToast(String string)
101 {
102 Toast toast = Toast.makeText(this, string, Toast.LENGTH_SHORT);
103 toast.setGravity(Gravity.TOP, 0, 200);
104 toast.show();
105 }
106 }
 1 Layout/main.xml:
2 <?xml version="1.0" encoding="utf-8"?>
3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4 android:orientation="vertical"
5 android:layout_width="fill_parent"
6 android:layout_height="fill_parent"
7 >
8 <TextView
9 android:id="@+id/textView"
10 android:layout_width="fill_parent"
11 android:layout_height="wrap_content"
12 android:text="@string/hello"
13 />
14 <CheckBox
15 android:id="@+id/checkBox01"
16 android:layout_width="wrap_content"
17 android:layout_height="wrap_content"
18 android:text="@string/CheckBox01"
19 />
20 <CheckBox
21 android:id="@+id/checkBox02"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:text="@string/CheckBox02"
25 />
26 <CheckBox
27 android:id="@+id/checkBox03"
28 android:layout_width="wrap_content"
29 android:layout_height="wrap_content"
30 android:text="@string/CheckBox03"
31 />
32 <CheckBox
33 android:id="@+id/checkBox04"
34 android:layout_width="wrap_content"
35 android:layout_height="wrap_content"
36 android:text="@string/CheckBox04"
37 />
38 <CheckBox
39 android:id="@+id/checkBox05"
40 android:layout_width="wrap_content"
41 android:layout_height="wrap_content"
42 android:text="@string/CheckBox05"
43 />
44 <Button
45 android:id="@+id/button"
46 android:layout_width="wrap_content"
47 android:layout_height="wrap_content"
48 android:text="@string/button"
49 />
50 </LinearLayout>

2.效果图

编辑器加载中...

原文地址:https://www.cnblogs.com/jh5240/p/2227120.html