多项选择(CheckBox)的使用

最新版本:多项选择(CheckBox)的使用

之前介绍过单项选择功能RadioGroupRadioButton的使用,那在Android中如何实现多项选择的呢?对,就是使用checkboxCheckBox的使用相对更简单,现在我们开始来简单的实现CheckBox

动作一

创建CheckBoxTest项目工程,这是我完成之后的目录:

项目工程目录

动作二

在布局文件中,添加一个4CheckBox控件,顺便再加上一个TextviewButton来显示文本和提交答案,完整代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     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.     <TextView  
  8.         android:id="@+id/testView"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/question" />  
  12.     <CheckBox  
  13.         android:id="@+id/checkbox1"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="@string/checkbox1" />  
  17.     <CheckBox  
  18.         android:id="@+id/checkbox2"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="@string/checkbox2" />  
  22.     <CheckBox  
  23.         android:id="@+id/checkbox3"  
  24.         android:layout_width="fill_parent"  
  25.         android:layout_height="wrap_content"  
  26.         android:text="@string/checkbox3" />  
  27.     <CheckBox  
  28.         android:id="@+id/checkbox4"  
  29.         android:layout_width="fill_parent"  
  30.         android:layout_height="wrap_content"  
  31.         android:text="@string/checkbox4" />  
  32.     <Button  
  33.         android:id="@+id/button"  
  34.         android:layout_width="wrap_content"  
  35.         android:layout_height="wrap_content"  
  36.         android:text="@string/button" />  
  37. </LinearLayout>

这是我们使用到的一些String,如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">CheckboxTest</string>  
  4.     <string name="question">在android开发中,有那些组件?</string>  
  5.     <string name="checkbox1">Activity组件</string>  
  6.     <string name="checkbox2">Broadcast Receiver组件</string>  
  7.     <string name="checkbox3">Service组件</string>  
  8.     <string name="checkbox4">Content Provider组件</string>  
  9.     <string name="button">提交</string>  
  10. </resources>

动作三

修改主要的代码,理解见注解:

  1. package org.ourunix.android.checkboxtest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.CheckBox;  
  9. import android.widget.CompoundButton;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12. import android.widget.CompoundButton.OnCheckedChangeListener;  
  13.   
  14. public class CheckboxTestActivity extends Activity {  
  15.     private TextView mTextView;  
  16.     private CheckBox mCheckBox1;  
  17.     private CheckBox mCheckBox2;  
  18.     private CheckBox mCheckBox3;  
  19.     private CheckBox mCheckBox4;  
  20.     private Button mButton;  
  21.   
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.main);  
  25.         /* 获取layout中的各个组件 */  
  26.         mTextView = (TextView) findViewById(R.id.testView);  
  27.         mCheckBox1 = (CheckBox) findViewById(R.id.checkbox1);  
  28.         mCheckBox2 = (CheckBox) findViewById(R.id.checkbox2);  
  29.         mCheckBox3 = (CheckBox) findViewById(R.id.checkbox3);  
  30.         mCheckBox4 = (CheckBox) findViewById(R.id.checkbox4);  
  31.         mButton = (Button) findViewById(R.id.button);  
  32.   
  33.         // 我们对每个CheckBox都使用setOnCheckedChangeListener来监听  
  34.         mCheckBox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  35.             public void onCheckedChanged(CompoundButton buttonView,  
  36.                     boolean isChecked) {  
  37.                 if (buttonView.isChecked()) {  
  38.                     display("你选择了" + buttonView.getText());  
  39.                 }  
  40.             }  
  41.         });  
  42.   
  43.         mCheckBox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  44.             public void onCheckedChanged(CompoundButton buttonView,  
  45.                     boolean isChecked) {  
  46.                 if (buttonView.isChecked()) {  
  47.                     display("你选择了" + buttonView.getText());  
  48.                 }  
  49.             }  
  50.         });  
  51.   
  52.         mCheckBox3.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  53.             public void onCheckedChanged(CompoundButton buttonView,  
  54.                     boolean isChecked) {  
  55.                 if (buttonView.isChecked()) {  
  56.                     display("你选择了" + buttonView.getText());  
  57.                 }  
  58.             }  
  59.         });  
  60.   
  61.         mCheckBox4.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  62.             public void onCheckedChanged(CompoundButton buttonView,  
  63.                     boolean isChecked) {  
  64.                 if (buttonView.isChecked()) {  
  65.                     display("你选择了" + buttonView.getText());  
  66.                 }  
  67.             }  
  68.         });  
  69.   
  70.         // 这边检测每一个CheckBox的isChecked  
  71.         mButton.setOnClickListener(new OnClickListener() {  
  72.             public void onClick(View v) {  
  73.                 int num = 0;  
  74.                 if (mCheckBox1.isChecked()) {  
  75.                     num++;  
  76.                 }  
  77.                 if (mCheckBox2.isChecked()) {  
  78.                     num++;  
  79.                 }  
  80.                 if (mCheckBox3.isChecked()) {  
  81.                     num++;  
  82.                 }  
  83.                 if (mCheckBox4.isChecked()) {  
  84.                     num++;  
  85.                 }  
  86.                 display("你一共选择了" + num + "项");  
  87.             }  
  88.         });  
  89.   
  90.     }  
  91.   
  92.     // 使用toast来显示勾选情况  
  93.     public void display(String s) {  
  94.         Toast toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);  
  95.         toast.show();  
  96.     }  

动作四

运行CheckBoxTest,效果如下:

运行CheckBoxTest

运行CheckBoxTest

选择了第一项,并有toast提示

选择了第一项,并有toast提示

选择全部四项,并提交

选择全部四项,并提交


源码:CheckBoxTest

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值