码迷,mamicode.com
首页 > 移动开发 > 详细

android 多选按钮CheckBox的使用

时间:2015-02-12 16:32:03      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.sadhu.s01_e09_checkbox.MainActivity$PlaceholderFragment" >

    <CheckBox 
        android:id="@+id/eatId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="吃饭" />
    <CheckBox 
        android:id="@+id/sleepId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="睡觉" />
    <CheckBox 
        android:id="@+id/dotaId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dota" />
    <CheckBox 
        android:id="@+id/allCheckedBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="全选"></CheckBox>

</LinearLayout>
package com.sadhu.s01_e09_checkbox;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
	//定义三个变量
	private CheckBox eatBox;
	private CheckBox sleepBox;
	private CheckBox dotaBox;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        
        //找到三个对象并赋值到相应的变量
        eatBox = (CheckBox)findViewById(R.id.eatId);
        sleepBox =  (CheckBox)findViewById(R.id.sleepId);
        dotaBox = (CheckBox)findViewById(R.id.dotaId);
        
        //选中状态发生改变后执行此监听器,两个参数,一个是对象,一个是是否选中。CompoundButton是CheckBox的超类
        CheckBoxListener cbListener = new CheckBoxListener();
        eatBox.setOnCheckedChangeListener(cbListener);
        sleepBox.setOnCheckedChangeListener(cbListener);
        dotaBox.setOnCheckedChangeListener(cbListener);
        
        //为全选按钮定义一个改变状态触发的监听器
        ((CheckBox)findViewById(R.id.allCheckedBox)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton checkBox, boolean isChecked) {
				eatBox.setChecked(isChecked);
				sleepBox.setChecked(isChecked);
				dotaBox.setChecked(isChecked);
			}
		});;
        
        /*
        //实例化点击事件监听类
        OnBoxClickListener onBoxClick = new OnBoxClickListener();
        //为三个对象设置点击事件
        eatBox.setOnClickListener(onBoxClick);
        sleepBox.setOnClickListener(onBoxClick);
        dotaBox.setOnClickListener(onBoxClick);*/
     
    }
    //定义一个选中状态发生改变就会执行此类
    class CheckBoxListener implements OnCheckedChangeListener
    {

		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if(buttonView.getId()==R.id.eatId){
				System.out.println("eatBox");
			}
			else if(buttonView.getId()==R.id.sleepId){
				System.out.println("sleepBox");
			}
			else if(buttonView.getId()==R.id.dotaId)
			{
				System.out.println("dotaBox");
			}
			if(isChecked)
			{
				System.out.println("checked");
			}
			else
			{
				System.out.println("unchecked");
			}
		}
    	
    }
    
    //定义一个单击事件监听类
    /*
    class OnBoxClickListener implements OnClickListener {
    	
    	@Override
    	public void onClick(View view)
    	{
    		CheckBox box = (CheckBox)view;
    		if(box.getId()==R.id.eatId)
    		{
    			System.out.println("eatBox");
    		}
    		else if(box.getId()==R.id.sleepId)
    		{
    			System.out.println("sleepBox");
    		}
    		else if(box.getId()==R.id.dotaId)
    		{
    			System.out.println("dotaBox");
    		}
    		if(box.isChecked())
    		{
    			System.out.println("checked");
    		}
    		else
    		{
    			System.out.println("unchecked");
    		}
    	}
    }*/

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}

多选按钮CheckBox很简单,就是为用户提供多选功能,他有两个监听器,一个是单击(OnClickListener)控件触发的监听器,一个是按钮选中状态发生改变后执行的监听器(OnCheckedChangeListener)。

android 多选按钮CheckBox的使用

标签:

原文地址:http://my.oschina.net/Sadhu/blog/378047

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!