首先,在布局文件里注冊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:orientation="vertical"
tools:context=".MainActivity"> <TextView android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <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>

MainActivity.java

CheckBox 即能够绑定 view.OnClickListener,也能够绑定 CompoundButton.OnCheckedChangeListener.

package com.example.litingdong.checkboxtest;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton; public class MainActivity extends ActionBarActivity { 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);
//OnClickListener
OnButtonClickListener listener=new OnButtonClickListener();
listener = new OnButtonClickListener();
eatBox.setOnClickListener(listener);
sleepBox.setOnClickListener(listener);
dotaBox.setOnClickListener(listener); //OnCheckedChangedListener
CheckBoxListener changeListener =new CheckBoxListener();
eatBox.setOnCheckedChangeListener(changeListener);
sleepBox.setOnCheckedChangeListener(changeListener);
dotaBox.setOnCheckedChangeListener(changeListener);
} class OnButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
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{
System.out.println("dotaBox");
} if (box.isChecked()){
System.out.println("Checked!");
}else{
System.out.println("UnChecked!");
}
}
} class CheckBoxListener implements CompoundButton.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{
System.out.println("dotaBox");
} if (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.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(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}

logcat:

Android 控件:CheckBox的更多相关文章

  1. Android控件系列之CheckBox

    学习目的: 1.掌握在Android中如何建立CheckBox 2.掌握CheckBox的常用属性 3.掌握CheckBox选中状态变换的事件(监听器) CheckBox简介: CheckBox和Bu ...

  2. 一步一步学android控件(之十六)—— CheckBox

    根据使用场景不同,有时候使用系统默认的CheckBox样式就可以了,但是有时候就需要自定义CheckBox的样式.今天主要学习如何自定义CheckBox样式.在CheckBox状态改变时有时需要做一些 ...

  3. Android控件系列之RadioButton&RadioGroup(转)

    学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...

  4. 一步一步学android控件(之十五) —— DegitalClock & AnalogClock

    原本计划DigitalClock和AnalogClock单独各一篇来写,但是想想,两个控件的作用都一样,就和在一起写一篇了. DegitalClock和AnalogClock控件主要用于显示当前时间信 ...

  5. Android控件TextView的实现原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8636153 在前面一个系列的文章中,我们以窗口 ...

  6. Android控件介绍

    1. 介绍 Android控件大多位于android.widget, android.view.View为他们的父类对于Dialog系列, android.app.Dialog为父类 Android的 ...

  7. [Android Pro] android控件ListView顶部或者底部也显示分割线

    reference to  :  http://blog.csdn.net/lovexieyuan520/article/details/50846569 在默认的Android控件ListView在 ...

  8. Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像

    此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...

  9. Android 控件架构及View、ViewGroup的测量

    附录:示例代码地址 控件在Android开发的过程中是必不可少的,无论是我们在使用系统控件还是自定义的控件.下面我们将讲解一下Android的控件架构,以及如何实现自定义控件. 1.Android控件 ...

  10. Android - 控件android:ems属性

    Android - 控件android:ems属性http://blog.csdn.net/caroline_wendy/article/details/41684255?utm_source=tui ...

随机推荐

  1. python 基础使用list、dict、set、可变与不可变对象

    参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/1017104324028448 dict是字典,可以储存键值对类型的值,set与dict ...

  2. token登录验证机制

    一张图解释 token登录验证机制

  3. Spring Boot学习总结(1)——Spring Boot入门

    摘要:Spring Boots是为了帮助开发人员很容易的创建出独立运行和产品级别的基于 Spring 框架的应用. 从 Spring Boot 项目名称中的 Boot 可以看出来,Spring Boo ...

  4. android 在短信发送界面, 短信发送失败时,提示音不完整,会被中断

    1. 当一条SMS到来, 此时SMS是unseen状态, 就会弹出Notification提示用户 2. 但假设处于同一个联系人的界面下, 用户会立马看到这条SMS, 此时这条SMS会被高速的标记为s ...

  5. Linux系统编程——进程间通信:信号中断处理

    什么是信号? 信号是 Linux 进程间通信的最古老的方式.信号是url=474nN303T2Oe2ehYZjkrggeXCaJPDSrmM5Unoh4TTuty4wSgS0nl4-vl43AGMFb ...

  6. 前端编程提高之旅(十二)----position置入值应用

    这次内推项目用到的遮罩及其页面下方button都涉及一个概念position置入值得概念.效果图例如以下: 一个元素position属性不是默认值static.那么该元素被称为定位元素. 定位的元素生 ...

  7. webpack02

    consumer-index.html <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

  8. BZOJ3376: [Usaco2004 Open]Cube Stacking 方块游戏

    [传送门:BZOJ3376] 简要题意: 约翰和贝茜在玩一个方块游戏.编号为1到n的n(1≤n≤30000)个方块正放在地上.每个构成一个立方柱. 游戏开始后,约翰会给贝茜发出P(1≤P≤100000 ...

  9. zzulioj--1816--矩形(好题数学)

    1816: 矩形 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 88  Solved: 24 SubmitStatusWeb Board Descri ...

  10. OpenCV与Socket实现树莓派获取摄像头视频至电脑

    OpenCV能够为我们带来便捷的图像处理接口,但是其处理速度在一块树莓派上肯定是不尽如人意的.尤其当我们想要使用复杂的算法时,只能把算法托到服务器上才有可能.这里介绍了一种方法,实现树莓派传输Mat至 ...