Android入门之多选按钮(CheckBox)

效果图:

代码如下:

activity_main.xml

<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"
    tools:context=".MainActivity" >
	<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"
	    />
</LinearLayout>

有2种方法;

第一种是OnClickListener的使用方法:

代码如下:

MainActivity.java

package com.jk.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
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.activity_main);
		eatBox=(CheckBox)findViewById(R.id.eatid);
		sleepBox=(CheckBox)findViewById(R.id.sleepid);
		dotaBox=(CheckBox)findViewById(R.id.dotaid);
		onBoxClickListener listener =new onBoxClickListener();
		eatBox.setOnClickListener(listener);
		sleepBox.setOnClickListener(listener);
		dotaBox.setOnClickListener(listener);
	}
	class onBoxClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			CheckBox box = (CheckBox)v;
			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("is checked!");
			else
				System.out.println("is 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;
	}
}

第二种是OnCheckedChangeListener的使用方法:

package com.jk.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
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.activity_main);
		eatBox=(CheckBox)findViewById(R.id.eatid);
		sleepBox=(CheckBox)findViewById(R.id.sleepid);
		dotaBox=(CheckBox)findViewById(R.id.dotaid);
		CheckBoxListener listener =new CheckBoxListener();
		eatBox.setOnCheckedChangeListener(listener);
		sleepBox.setOnCheckedChangeListener(listener);
		dotaBox.setOnCheckedChangeListener(listener);
	}
	class CheckBoxListener implements OnCheckedChangeListener{
		public void onCheckedChanged(CompoundButton v, boolean isChecked) {
			// TODO Auto-generated method stub
			CompoundButton box = (CompoundButton)v;
			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(isChecked)
				System.out.println("is checked!");
			else
				System.out.println("is unchecked!");
		}
	}
	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;
	}
}

另外下面是实现全选的功能:

package com.jk.test;

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

public class MainActivity extends Activity {
	private CheckBox eatBox;
	private CheckBox sleepBox;
	private CheckBox dotaBox;
	private CheckBox allClickBox;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		eatBox = (CheckBox) findViewById(R.id.eatid);
		sleepBox = (CheckBox) findViewById(R.id.sleepid);
		dotaBox = (CheckBox) findViewById(R.id.dotaid);
		allClickBox = (CheckBox) findViewById(R.id.allClickid);
		CheckBoxListener listener = new CheckBoxListener();
		eatBox.setOnCheckedChangeListener(listener);
		sleepBox.setOnCheckedChangeListener(listener);
		dotaBox.setOnCheckedChangeListener(listener);
		allClickBox.setOnCheckedChangeListener(listener);
	}
	class CheckBoxListener implements OnCheckedChangeListener {
		public void onCheckedChanged(CompoundButton v, boolean isChecked) {
			CompoundButton box = (CompoundButton) v;
			if (box.getId() == R.id.eatid) {
				if (isChecked)
					System.out.println("eatBox is checked!");
				else
					System.out.println("eatBox is unchecked!");
			}
			else if (box.getId() == R.id.eatid) {
				if (isChecked)
					System.out.println("sleepBox is checked!");
				else
					System.out.println("sleepBox is unchecked!");
			}
			else if (box.getId() == R.id.eatid) {
				if (isChecked)
					System.out.println("dotaBox is checked!");
				else
					System.out.println("dotaBox is unchecked!");
			}
			else if (box.getId() == R.id.allClickid) {
				if (isChecked) {
					eatBox.setChecked(true);
					sleepBox.setChecked(true);
					dotaBox.setChecked(true);
				}
				else {
					eatBox.setChecked(false);
					sleepBox.setChecked(false);
					dotaBox.setChecked(false);
				}
			}
		}
	}
	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;
	}
}
时间: 2024-08-08 16:09:48

Android入门之多选按钮(CheckBox)的相关文章

android开发-购物车选择商品,checkbox全选和全取消问题

问题描述 购物车选择商品,checkbox全选和全取消问题 最近在做一个购物项目,在实现选中商品时,选择了使用checkbox进行实现,我现在想实现一个效果就是点击所有条目后,全选按钮自动打钩,当取消其中一个商品后取消全选打钩状态,经过测试发现是因为我在通过setcheck()方法改变checkbox状态时,自动调用了checkbox监听,将所有的条目都取消了选中状态,相当烦人,求大神告知有没有解决方案,最好能给个demo 解决方案 http://blog.sina.com.cn/s/blog_

Android入门之Activity四种启动模式(standard、singleTop、singleTask、singleInstance)_Android

当应用运行起来后就会开启一条线程,线程中会运行一个任务栈,当Activity实例创建后就会放入任务栈中.Activity启动模式的设置在AndroidManifest.xml文件中,通过配置Activity的属性android:launchMode=""设置. 一.启动模式介绍 启动模式简单地说就是Activity启动时的策略,在AndroidManifest.xml中的标签的android:launchMode属性设置: 启动模式有4种,分别为standard.singleTop.s

js实现(全选)多选按钮的方法【附实例】_javascript技巧

第一种,全部选中: <html> <head> <title>复选框checked属性</title> <script language="JavaScript" type="text/javascript"> function changeState(isChecked) { var chk_list=document.getElementsByTagName("input"); fo

js-求大神指导下拉框多选 按钮问题

问题描述 求大神指导下拉框多选 按钮问题 多选下拉框 带有按钮 点击确定提交数据 一个页面有多个这样的下拉框 提交后前一个下拉菜单选项不清空 解决方案 当你点击下一个下拉框时在js中判断上一个值是否为空 解决方案二: 我想知道怎么做成这个样子 解决方案三: 我想知道的是怎么实现整个效果 解决方案四: 这个就不是下拉菜单.只不过是一些元素加了样式. 比如div,里面包含很多li标签.每个标签内部有一个checkbox和一个span,然后加样式和js就行了

app-启动APP的时候想出现一个双选按钮,左边是A模式,右边是B模式,怎么弄

问题描述 启动APP的时候想出现一个双选按钮,左边是A模式,右边是B模式,怎么弄 1.手机上的APP都是中文名称,比如蹲蹲乐,是不是用android studio新建project的时候也要用中文名称,比如蹲蹲乐,2.启动APP的时候想出现一个双选按钮,左边是A模式,右边是B模式,怎么弄? 谢谢指点 解决方案 直接用ImageButton就可以了. 解决方案二: http://blog.sina.com.cn/s/blog_70677d110100qzhl.html 解决方案三: http://

Android入门教程之ListView的应用示例_Android

本文实例讲述了Android ListView的简单应用.分享给大家供大家参考,具体如下: 我们今天要讲的内容是Android中ListView中的实现.一共分为四个步骤,我将一一讲解: Step one:创建一个新的Android工程,命名为ListViewDemo. Step two:找到ListViewDemo.Java,把我们习惯的继承Activity,改成ListActivity,如下: public class ListViewDemo extends ListActivity St

Android入门之使用eclipse进行源码开发的方法_Android

本文实例讲述了Android入门之使用eclipse进行源码开发的方法.分享给大家供大家参考,具体如下: 一.版本说明: 1. eclipse for javaEE 3.5.2 2. jdk1.6 3. adt12.0 4. linux/Ubuntu10.04 或者 linux/ubuntu10.10 二.准备工作: 1. 下载 Android2.3.7 源码 欲了解具体内容可以参看 android 官网. 2. 编译源码 必须编译源码,否则会引发很多问题.记住:如果下载没问题的话,编译只是时间

jquery获取复选框checkbox的值实现方法_jquery

jQuery API : each(callback)::以每一个匹配的元素作为上下文来执行一个函数. :checked :匹配所有选中的被选中元素(复选框.单选框等,不包括select中的option) js: //js获取复选框值 var obj = document.getElementsByName("interest");//选择所有name="interest"的对象,返回数组 var s='';//如果这样定义var s;变量s中会默认被赋个null值

Android入门之源码开发基础教程_Android

本文讲述了Android入门之源码开发基础教程.分享给大家供大家参考,具体如下: 下载 Android 源码之后,接下来就是学习或者进行开发. 在开发之前,谈一些开发必备知识或者工具,工欲善其事必先利其器嘛! 在前面一篇<Android入门之使用eclipse进行源码开发的方法>中基本上说了开发使用工具. 但是我们如何使用模拟器开发呢?! 当然你去删除或者增加app到模拟器就不可以按常规来操作了,花 5 -10 分钟了解一下,如果你有需要. 0. 编译源码 直接在下载的源码根目录下面,执行: