首先,在布局文件里注冊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:

最新文章

  1. cxf ServerFactoryBean 生成基于soap1.2的WebServices
  2. javascript设计模式实践之职责链--具有百叶窗切换图片效果的JQuery插件(三)
  3. js操作document文档元素 节点交换交换
  4. tomcat内存溢出处理
  5. Scala之类型参数和对象
  6. 富文本HTML编辑器UEditor
  7. C/C++中虚函数的调用
  8. Android技术——切换视图(两)随着ViewPage达到Tab幻灯片浏览
  9. .NET Core快速入门教程 1、开篇:说说.NET Core的那些事儿
  10. CentOS中安装配置Nginx
  11. js动态生成层方法 不懂得加QQ 2270312758
  12. Hibernate 4.3.11 下问题的解决
  13. 29、sass
  14. Eclipse错误集合
  15. python自动化开发-7
  16. 元素的组合(dfs)
  17. 如何下载网页上的视频和flash的方法
  18. Hadoop环境搭建及wordcount程序
  19. [转] 机器学习是什么&mdash;&mdash;周志华
  20. bootstrap 问题

热门文章

  1. java开发微信公众号支付(JSAPI)
  2. win10如何设置开机自动启动热点WIFI?
  3. [TJOI2017]城市(树的直径)
  4. TortoiseGit 弹出 git@xxx.com&#39;s password 对话框
  5. CheckBox:屏蔽setChecked方法对OnCheckedChangeListener的影响
  6. mysql通过DATE_FORMAT将错误数据恢复
  7. 分享:FIFO 同步、异步以及Verilog代码实现
  8. quartz 添加监听器listener
  9. cd---切换工作目录
  10. git hub在windows上的使用