android 多选按钮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"
    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)。

时间: 2025-01-17 21:40:20

android 多选按钮CheckBox的使用的相关文章

Android——复选按钮和开关按钮

复选按钮和开关按钮代码如下: 1 <LinearLayout 2 android:layout_width="match_parent" 3 android:layout_height="wrap_content"> 4 <CheckBox 5 android:layout_width="wrap_content" 6 android:layout_height="wrap_content" 7 androi

zepto全选按钮之全选会根据按钮是否被全部选中更改状态

在做手机端二次开发购物车的时候,发现zepto全选,没找到,或者功能不是自己想要的 后来做好,分享给需要的人 //全选或多选处理      var CheckAll = $('#items_check_all');    var checkbox = $('input[name^="check"]');    var removeUrl = '<{link app=b2c ctl=wap_cart act=remove}>';        //初始化,把所有选中的加上状态

android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发

android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发 本博文主要讲述的是android开发中的单选和多选按钮的使用,具体情况请看实例代码: MainActivity.java: package com.example.radiotest; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.CheckBox; imp

安卓开发_复选按钮控件(CheckBox)的简单使用

复选按钮 即可以选择若干个选项,与单选按钮不同的是,复选按钮的图标是方块,单选按钮是圆圈 复选按钮用CheckBox表示,CheckBox是Button的子类,支持使用Button的所有属性 一.由于复选框可以选中多项,所有为了确定用户是否选择了某一项,还需要为每一个选项添加setOnCheckedChangeListener事件监听 例如: 为id为like1的复选按钮添加状态改变事件监听,代码如下 1 final CheckBox like1 = (CheckBox)findViewById

android的listview加checkbox实现单保存checkbox的勾选信息

最近做一个项目时想要增加安卓端的一个功能,就是增加一个activity上面有很多checkbox的勾选信息,然后可以给人选择多选框,看起来还算很简单..但是真正做起来就有很多麻烦了.. 虽然我对安卓有点了解,以前也看过一些代码,但都是看别人的代码,看代码当然简单啦,这下要全部自己写,这下有点棘手了,一开始真是无从下手啊,后来去网上查了一下,觉得有个人的写法不错,就是用listview导入adapter的布局这样子,于是我就照着他的代码的原型开始改了起来. 改了N久,终于改的有点样子了,其中也花了

Android初级教程小案例之单选框RadioGroup与复选框CheckBox

Android里面的单选框和html中的其实是一样的效果.这里用到两个控件:CheckBox和RadioGroup.直接上代码: radio.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation=&quo

【Android】标签页、计时器、单选按钮、复选按钮

写一个小程序把安卓程序中的几个基础组件的基本用法串联起来. 如下图所示: 在安卓程序中,一个计时器,一直在不断地计时,每10秒弹出一个提示. MainActivity被一个标签页分成两部分,一部分,有单选按钮与复选按钮,最后有一个提交按钮, 结果在另一个标签页中显示. 用这个程序来说明安卓中标签页.计时器.单选按钮.复选按钮的用法. 首先贴上res\values\string.xml中,各个组件的字符串. <?xml version="1.0" encoding="ut

UI控件之RadioButton(单选按钮)&amp;Checkbox(复选按钮)

(一)概述: (二)RadioButton的基本用法与事件处理: 效果图: 实现代码: xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:

checkbox做全选按钮

1.先写一个html页面,里面写一个全选按钮和几个复选框,实现下面2个要求 (1)点击全选按钮选中时,所有的复选框选中. (2)点击全选按钮取消选中时,所有复选框取消选中. <input type="checkbox" id="quanxuan" />全选<br /> <input type="checkbox" class="qx" />aa<br /> <input t