demo例子

1. 布局xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<CheckBox
android:id="@+id/apple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="apple"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
/>

<CheckBox
android:id="@+id/banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="banana"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
/>

<CheckBox
android:id="@+id/grape"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="grape"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
/>

<CheckBox
android:id="@+id/orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="orange"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
/>

<CheckBox
android:id="@+id/peach"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="peach"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
/>

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHOW TOAST"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
/>

</LinearLayout>

2. Actiivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

List<String> list = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//获取控件
CheckBox apple = findViewById(R.id.apple);
CheckBox banana = findViewById(R.id.banana);
CheckBox grape = findViewById(R.id.grape);
CheckBox orange = findViewById(R.id.orange);
CheckBox peach = findViewById(R.id.peach);

//设置监听者
apple.setOnCheckedChangeListener(this);
banana.setOnCheckedChangeListener(this);
grape.setOnCheckedChangeListener(this);
orange.setOnCheckedChangeListener(this);
peach.setOnCheckedChangeListener(this);

//按钮
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = " ";
for (String s : list) {
text = text + s + " ";
}

Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//是否被选中
if (isChecked){
list.add(buttonView.getText().toString());
}else{
list.remove(buttonView.getText().toString());
}
}
}

3. 运行结果