android 入门 CheckBox

package com.zte.android.lession;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class Activity_checkbox_005 extends Activity
{
	private CheckBox cb1,cb2,cb3,cb4;
	private TextView txt ;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.checkbox_layout_005);
		//实例化组件
		cb1 = (CheckBox)findViewById(R.id.l005_checkBox1);
		cb2 = (CheckBox)findViewById(R.id.l005_checkBox2);
		cb3 = (CheckBox)findViewById(R.id.l005_checkBox3);
		cb4 = (CheckBox)findViewById(R.id.l005_checkBox4);
		
		txt = (TextView)findViewById(R.id.l005_textView1);
		
		cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(cb1.isChecked()){
					txt.setText("cb1 选中");
				}else{
					txt.setText("cb1 取消");
				}
			}
		});
		cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(cb1.isChecked()){
					txt.setText("cb1 选中");
				}else{
					txt.setText("cb1 取消");
				}
			}
		});
		cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(cb2.isChecked()){
					txt.setText("cb2 选中");
				}else{
					txt.setText("cb2 取消");
				}
			}
		});
		
	}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/l005_checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox1" />

    <CheckBox
        android:id="@+id/l005_checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox2" />

    <CheckBox
        android:id="@+id/l005_checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox3" />

    <CheckBox
        android:id="@+id/l005_checkBox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox4" />

    <TextView
        android:id="@+id/l005_textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

你可能感兴趣的:(checkbox)