Android控件——CheckBox的使用说明

CheckBox复选框,继承于Button,并且提供了可以选中的功能:

实现入下效果:

当点击全选的时候,所有的邮件都被选中,取消时也一起取消;当点击删除时,需要提示用于是否已选择要删除的checkBox。

在activity_main.xml中的代码入下:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical"
 5     android:padding="16dp" >
 6 
 7     <TextView
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:text="收件箱" />
11 
12     <CheckBox
13         android:id="@+id/check_all"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:text="全选" />
17 
18     <View
19         android:layout_width="match_parent"
20         android:layout_height="1px"
21         android:background="#ff0000" />
22 
23     <LinearLayout
24         android:id="@+id/email_layout"
25         android:layout_width="match_parent"
26         android:layout_height="wrap_content"
27         android:orientation="vertical" >
28 
29         <CheckBox
30             android:layout_width="wrap_content"
31             android:layout_height="wrap_content"
32             android:text="邮件1" />
33 
34         <CheckBox
35             android:layout_width="wrap_content"
36             android:layout_height="wrap_content"
37             android:text="邮件2" />
38 
39         <CheckBox
40             android:layout_width="wrap_content"
41             android:layout_height="wrap_content"
42             android:text="邮件3" />
43 
44         <CheckBox
45             android:layout_width="wrap_content"
46             android:layout_height="wrap_content"
47             android:text="邮件4" />
48 
49         <CheckBox
50             android:layout_width="wrap_content"
51             android:layout_height="wrap_content"
52             android:text="邮件5" />
53     </LinearLayout>
54     
55     <Button
56         android:id="@+id/btn_delete"
57         android:layout_width="wrap_content"
58         android:layout_height="wrap_content"
59         android:layout_marginTop="5dp"
60         android:layout_gravity="right"
61         android:text="删除" />
62 
63 </LinearLayout>

在MainActivity中的代码入下:

 1 public class MainActivity extends Activity {
 2 
 3     private CheckBox deleteAllBox;
 4     private LinearLayout emailLayout;
 5     private Button deleteBtn;
 6     private int num;
 7 
 8     @Override
 9     protected void onCreate(Bundle savedInstanceState) {
10         super.onCreate(savedInstanceState);
11         setContentView(R.layout.activity_main);
12 
13         // 获取控件
14         deleteAllBox = (CheckBox) findViewById(R.id.check_all);
15         emailLayout = (LinearLayout) findViewById(R.id.email_layout);
16         deleteBtn = (Button) findViewById(R.id.btn_delete);
17         // 全选CheckBox添加监听
18         deleteAllBox
19                 .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
20 
21                     @Override
22                     public void onCheckedChanged(CompoundButton buttonView,
23                             boolean isChecked) {
24                         for (int i = 0; i < emailLayout.getChildCount(); i++) {
25                             CheckBox checkBox = (CheckBox) emailLayout
26                                     .getChildAt(i);
27                             checkBox.setChecked(isChecked);
28                         }
29                     }
30                 });
31         // 删除按钮添加监听
32         deleteBtn.setOnClickListener(new View.OnClickListener() {
33 
34             @Override
35             public void onClick(View v) {
36                 
37                 for (int i = 0; i < emailLayout.getChildCount(); i++) {
38                     CheckBox checkBox = (CheckBox) emailLayout.getChildAt(i);
39                     if (checkBox.isChecked()) {
40                         num++;
41                     }
42                 }
43                 //Toast.makeText(MainActivity.this, "num--->"+num, Toast.LENGTH_LONG).show();
44                 if (num > 0) {
45                     for (int i = 0; i < emailLayout.getChildCount(); i++) {
46                         CheckBox checkBox = (CheckBox) emailLayout
47                                 .getChildAt(i);
48                         if (checkBox.isChecked()) {
49                             /*
50                              * View.GONE不可见且不占据空间 View.VISIBLE正常可见
51                              * View.INVISIBLE不可见但占据空间
52                              */
53                             checkBox.setVisibility(View.GONE);
54                             //移除
55                             emailLayout.removeView(checkBox);
56                             num--;
57                             //Toast.makeText(MainActivity.this, "剩余--->"+emailLayout.getChildCount(), Toast.LENGTH_LONG).show();
58                         }
59                     }
60                 } else {
61                     Toast.makeText(MainActivity.this, "尚未选择",
62                             Toast.LENGTH_SHORT).show();
63                 }
64 
65             }
66         });
67 
68     }
69 
70 }

 

转载于:https://www.cnblogs.com/zhangkun0614/p/9668572.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值