Bin's

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
package org.ben.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidTest_CheckBoxActivity extends Activity {
	/** Called when the activity is first created. */
	CheckBox cb1, cb2, cb3, cb4;
	Button btn;
	TextView textView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		textView = (TextView) findViewById(R.id.textview);
		btn = (Button) findViewById(R.id.btn);

		cb1 = (CheckBox) findViewById(R.id.cb1);
		cb2 = (CheckBox) findViewById(R.id.cb2);
		cb3 = (CheckBox) findViewById(R.id.cb3);
		cb4 = (CheckBox) findViewById(R.id.cb4);
		
		cb1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (cb1.isChecked()){
					DisplayToast("你选择了:"+cb1.getText());
				}				
			}			
		});
		
		cb2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (cb2.isChecked()){
					DisplayToast("你选择了:"+cb2.getText());
				}				
			}			
		});
		
		cb3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (cb3.isChecked()){
					DisplayToast("你选择了:"+cb3.getText());
				}				
			}			
		});
		
		cb4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (cb4.isChecked()){
					DisplayToast("你选择了:"+cb4.getText());
				}				
			}			
		});
		
		btn.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v)
			{
				int num = 0 ;
				
				if (cb1.isChecked()){
					num++;
				}
				if (cb2.isChecked()){
					num++;
				}
				if (cb3.isChecked()){
					num++;
				}
				if (cb4.isChecked()){
					num++;
				}
				
				DisplayToast("谢谢参与,您一共选择了:"+num+" 项");
			}
		});
	}
	
	public void DisplayToast(String str)
	{
		Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.TOP,0,270);
		toast.show();
	}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/title" />
    
    <CheckBox 
        android:id="@+id/cb1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/cb1"
        />
    <CheckBox 
        android:id="@+id/cb2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/cb2"
        />
    <CheckBox 
        android:id="@+id/cb3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/cb3"
        />
    <CheckBox 
        android:id="@+id/cb4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/cb4"
        />
    
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"/>

</LinearLayout>
posted on 2011-11-08 00:18  Jesuca  阅读(248)  评论(0编辑  收藏  举报