Android学习第二天--checkbox

继续第一天的内容,接下来介绍第三个组件:

  checkBox

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hobby" />
    
    <CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox1"
        android:text="篮球"/>
    
    <CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox2"
        android:text="足球"/>
    
    <CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkbox3"
        android:text="橄榄球"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buuton"
        android:text="确定" />
   
</LinearLayout>

mainactivity代码

  

package cn.core.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioGroup;

import android.widget.Toast;

public class MainActivity extends Activity {
    CheckBox []checkBox = new CheckBox[3];
    int [] id = {R.id.checkbox1,R.id.checkbox2,R.id.checkbox3};
    Button button = null; 
    String str="";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        for (int i = 0; i < checkBox.length; i++) {
            
            checkBox[i]=(CheckBox)findViewById(id[i]);
            checkBox[i].setOnCheckedChangeListener(new OnCheckedChangeListenerTest());
        }
        button=(Button)findViewById(R.id.buuton);
        
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                str="";
                for (int i = 0; i < id.length; i++) {
                    checkBox[i]=(CheckBox)findViewById(id[i]);
                    if(checkBox[i].isChecked())
                    {
                        str+=checkBox[i].getText().toString();
                    }
                    
                }
                Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private final class OnCheckedChangeListenerTest implements OnCheckedChangeListener
    {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
            if(isChecked)
            {
                Toast.makeText(MainActivity.this, buttonView.getText().toString(),Toast.LENGTH_SHORT).show();
            }
            
        }
    }

}
checkBox[i].setOnCheckedChangeListener(new OnCheckedChangeListenerTest());为每个checkbox添加事件监听器
posted @ 2013-03-09 16:29  小三小山  阅读(213)  评论(0)    收藏  举报