最新版本:多项选择(CheckBox)的使用
之前介绍过单项选择功能RadioGroup和RadioButton的使用,那在Android中如何实现多项选择的呢?对,就是使用checkbox,CheckBox的使用相对更简单,现在我们开始来简单的实现CheckBox。
动作一
创建CheckBoxTest项目工程,这是我完成之后的目录:
动作二
在布局文件中,添加一个4个CheckBox控件,顺便再加上一个Textview和Button来显示文本和提交答案,完整代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView
- android:id="@+id/testView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/question" />
- <CheckBox
- android:id="@+id/checkbox1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/checkbox1" />
- <CheckBox
- android:id="@+id/checkbox2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/checkbox2" />
- <CheckBox
- android:id="@+id/checkbox3"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/checkbox3" />
- <CheckBox
- android:id="@+id/checkbox4"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/checkbox4" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/button" />
- </LinearLayout>
这是我们使用到的一些String,如下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">CheckboxTest</string>
- <string name="question">在android开发中,有那些组件?</string>
- <string name="checkbox1">Activity组件</string>
- <string name="checkbox2">Broadcast Receiver组件</string>
- <string name="checkbox3">Service组件</string>
- <string name="checkbox4">Content Provider组件</string>
- <string name="button">提交</string>
- </resources>
动作三
修改主要的代码,理解见注解:
- package org.ourunix.android.checkboxtest;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.TextView;
- import android.widget.Toast;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- public class CheckboxTestActivity extends Activity {
- private TextView mTextView;
- private CheckBox mCheckBox1;
- private CheckBox mCheckBox2;
- private CheckBox mCheckBox3;
- private CheckBox mCheckBox4;
- private Button mButton;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- /* 获取layout中的各个组件 */
- mTextView = (TextView) findViewById(R.id.testView);
- mCheckBox1 = (CheckBox) findViewById(R.id.checkbox1);
- mCheckBox2 = (CheckBox) findViewById(R.id.checkbox2);
- mCheckBox3 = (CheckBox) findViewById(R.id.checkbox3);
- mCheckBox4 = (CheckBox) findViewById(R.id.checkbox4);
- mButton = (Button) findViewById(R.id.button);
- // 我们对每个CheckBox都使用setOnCheckedChangeListener来监听
- mCheckBox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- if (buttonView.isChecked()) {
- display("你选择了" + buttonView.getText());
- }
- }
- });
- mCheckBox2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- if (buttonView.isChecked()) {
- display("你选择了" + buttonView.getText());
- }
- }
- });
- mCheckBox3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- if (buttonView.isChecked()) {
- display("你选择了" + buttonView.getText());
- }
- }
- });
- mCheckBox4.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- public void onCheckedChanged(CompoundButton buttonView,
- boolean isChecked) {
- if (buttonView.isChecked()) {
- display("你选择了" + buttonView.getText());
- }
- }
- });
- // 这边检测每一个CheckBox的isChecked
- mButton.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- int num = 0;
- if (mCheckBox1.isChecked()) {
- num++;
- }
- if (mCheckBox2.isChecked()) {
- num++;
- }
- if (mCheckBox3.isChecked()) {
- num++;
- }
- if (mCheckBox4.isChecked()) {
- num++;
- }
- display("你一共选择了" + num + "项");
- }
- });
- }
- // 使用toast来显示勾选情况
- public void display(String s) {
- Toast toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
- toast.show();
- }
- }
动作四
运行CheckBoxTest,效果如下:
运行CheckBoxTest
选择了第一项,并有toast提示
选择全部四项,并提交
源码:CheckBoxTest