[android] 04_常见控件2-多选框(CheckBox)

Android 4.0

常见控件2-多选框(CheckBox)

多选框(CheckBox)
每个多选框都是独立的,可以通过迭代所有多选框,然后根据其状态是否被选中再获取其值。
l CheckBox.setChecked(true);//设置成选中状态。
l CheckBox.getText();//获取多选框的值
l调用setOnCheckedChangeListener()方法,处理多选框被选择事件,把CompoundButton.OnCheckedChangeListener实例作为参数传入。
1、布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent" >
    <CheckBox
        android:id="@+id/checkboxjava"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="java" />
    <CheckBox
        android:id="@+id/checkboxdotNet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dotNet" />
    <CheckBox
        android:id="@+id/checkboxphp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PHP" />
    <Button
        android:id="@+id/checkboxButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="获取值" />
</LinearLayout>
2、核心代码:
package cn.zengfansheng.checkbox;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class MainActivity extends Activity {
    
    private static final String TAG = "CheckBoxActivity";
    private List<CheckBox> checkboxs = new ArrayList<CheckBox>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 1、将每个复选框添加到集合
        checkboxs.add((CheckBox) findViewById(R.id.checkboxdotNet));
        checkboxs.add((CheckBox) findViewById(R.id.checkboxjava));
        checkboxs.add((CheckBox) findViewById(R.id.checkboxphp));
        checkboxs.get(1).setChecked(true);// 设置成选中状态
        // 2、为每一个复选框设置监听事件
        for (CheckBox box : checkboxs) {
            box.setOnCheckedChangeListener(listener);
        }
        Button button = (Button) findViewById(R.id.checkboxButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> values = new ArrayList<String>();
                // 3、遍历CheckBox的集合,判断是否被选中
                for (CheckBox box : checkboxs) {
                    if (box.isChecked()) {
                        values.add(box.getText().toString());
                    }
                }
                Toast.makeText(MainActivity.this, values.toString(), 1).show();
            }
        });
    }
    // 监听复选框是否被选中的监听器
    CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {    
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            CheckBox checkBox = (CheckBox) buttonView;
            Log.i(TAG"isChecked="+ isChecked +",value="+ checkBox.getText());//输出单选框的值
        }
    };
}
3、结果: