1)ChexkBox继承自CompoundButton组件;

2)isChecked()--确定是否选中;setChecked(bool checked)--设置选中或取消选中;

3)监听事件:CompoundButton.OnCheckedChangeListener

使用checkbox,并实现监听测试:

1)效果:

2)源代码:

res\layout\activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity" >
  10.  
  11. <CheckBox
  12. android:id="@+id/cbJava"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentLeft="true"
  16. android:layout_marginLeft="27dp"
  17. android:text="Java Runtime 9.0" />
  18.  
  19. <TextView
  20. android:id="@+id/tvCheckedValue"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_alignLeft="@+id/cbJava"
  24. android:layout_below="@+id/cbPython"
  25. android:layout_marginLeft="14dp"
  26. android:layout_marginTop="54dp"
  27. android:text="" />
  28.  
  29. <CheckBox
  30. android:id="@+id/cbPython"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_alignLeft="@+id/cbJava"
  34. android:layout_below="@+id/cbJava"
  35. android:layout_marginTop="22dp"
  36. android:text="Python runtime2.7" />
  37.  
  38. </RelativeLayout>

MainActivity.java

  1. package com.example.helloword;
  2.  
  3. import java.text.BreakIterator;
  4.  
  5. import android.R.bool;
  6. import android.R.string;
  7. import android.app.Activity;
  8. import android.app.AlertDialog;
  9. import android.content.DialogInterface;
  10. import android.content.DialogInterface.OnClickListener;
  11. import android.os.Bundle;
  12. import android.renderscript.Script.KernelID;
  13. import android.test.IsolatedContext;
  14. import android.util.Log;
  15. import android.view.KeyEvent;
  16. import android.view.Menu;
  17. import android.view.View;
  18. import android.widget.Button;
  19. import android.widget.CheckBox;
  20. import android.widget.CompoundButton;
  21. import android.widget.EditText;
  22. import android.widget.ImageView;
  23. import android.widget.RadioButton;
  24. import android.widget.RadioGroup;
  25. import android.widget.TextView;
  26.  
  27. public class MainActivity extends Activity {
  28. private CheckBox cbJava, cbPython;
  29. private TextView tvCheckedValue;
  30.  
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main);
  35. this.cbJava = (CheckBox) this.findViewById(R.id.cbJava);
  36. this.cbPython = (CheckBox) this.findViewById(R.id.cbPython);
  37. this.tvCheckedValue = (TextView) this.findViewById(R.id.tvCheckedValue);
  38.  
  39. CheckBoxOnCheckedChangeListener listener = new CheckBoxOnCheckedChangeListener();
  40.  
  41. this.cbJava.setOnCheckedChangeListener(listener);
  42. this.cbPython.setOnCheckedChangeListener(listener);
  43.  
  44. }
  45.  
  46. private StringBuffer stringBuffer;
  47.  
  48. private class CheckBoxOnCheckedChangeListener implements
  49. CompoundButton.OnCheckedChangeListener {
  50. private String checkJava = cbJava.getText().toString() + " ";
  51. private String checkPython = cbPython.getText().toString() + " ";
  52. private StringBuffer stringBuffer = new StringBuffer();
  53.  
  54. @Override
  55. public void onCheckedChanged(CompoundButton compoundButton,
  56. boolean ischecked) {
  57. switch (compoundButton.getId()) {
  58. case R.id.cbJava:
  59. Log.i("info", "operator " + cbJava.getText());
  60. break;
  61. case R.id.cbPython:
  62. Log.i("info", "operator " + cbPython.getText());
  63. break;
  64. default:
  65. break;
  66. }
  67.  
  68. if (compoundButton == cbJava) {
  69. changeValue(ischecked, checkJava);
  70. } else if (compoundButton == cbPython) {
  71. changeValue(ischecked, checkPython);
  72. }
  73.  
  74. tvCheckedValue.setText(stringBuffer.toString());
  75. }
  76.  
  77. private void changeValue(boolean ischecked, String checkValue) {
  78. int start = stringBuffer.indexOf(checkValue);
  79. int end = start + checkValue.length();
  80. if (ischecked) {
  81. if (start == -1) {
  82. stringBuffer.append(checkValue);
  83. }
  84. } else {
  85. if (start != -1) {
  86. stringBuffer.delete(start, end);
  87. }
  88. }
  89. }
  90. }
  91.  
  92. @Override
  93. public boolean onCreateOptionsMenu(Menu menu) {
  94. // Inflate the menu; this adds items to the action bar if it is present.
  95. getMenuInflater().inflate(R.menu.main, menu);
  96. return true;
  97. }
  98.  
  99. @Override
  100. public boolean onKeyUp(int keyCode, KeyEvent event) {
  101. // 当点击回退时,弹出该窗口(也就相当于关闭操作)
  102. if (keyCode == KeyEvent.KEYCODE_BACK) {
  103. new AlertDialog.Builder(this).setTitle("是否退出?")
  104. .setPositiveButton("确定", new OnClickListener() {
  105. @Override
  106. public void onClick(DialogInterface arg0, int arg1) {
  107. finish();
  108. }
  109. }).setNegativeButton("取消", null).show();
  110. return true;
  111. }
  112. return super.onKeyUp(keyCode, event);
  113. }
  114. }

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

  1. android CheckBox控件的定义及事件监听

    http://www.beijibear.com/index.php?aid=336 android CheckBox控件的定义及事件监听,本例实现CheckBox控件的定义及点击事件的监听并显示结果 ...

  2. Android开发CheckBox控件,全选,反选,取消全选

    在Android开发中我们经常会使用CheckBox控件,那么怎么实现CheckBox控件的全选,反选呢 首先布局我们的界面: <?xml version="1.0" enc ...

  3. Android 基本控件相关知识整理

    Android应用开发的一项重要内容就是界面开发.对于用户来说,不管APP包含的逻辑多么复杂,功能多么强大,如果没有提供友好的图形交互界面,将很难吸引最终用户.作为一个程序员如何才能开发出友好的图形界 ...

  4. 一个Demo让你掌握Android所有控件

    原文:一个Demo让你掌握Android所有控件 本文是转载收藏,侵删,出处:"安卓巴士"      下面给出实现各个组件的源代码: 1.下拉框实现--Spinner packag ...

  5. android 基础控件(EditView、SeekBar等)的属性及使用方法

        android提供了大量的UI控件,本文将介绍TextView.ImageView.Button.EditView.ProgressBar.SeekBar.ScrollView.WebView ...

  6. Android基本控件之Menus

    在我们的手机中有很多样式的菜单,比如:我们的短信界面,每条短信,我们长按都会出现一个菜单,还有很多的种类.那么现在,我们就来详细的讨论一下安卓中的菜单 Android的控件中就有这么一个,叫做Menu ...

  7. checkBox控件的CheckedChanged与CheckedStateChanged区别

    Checked属性为bool类型,CheckState属性为枚举类型(CheckState.Checked.CheckState.Unchecked和CheckState.Indeterminate) ...

  8. Android:控件布局(相对布局)RelativeLayout

    RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列. 相对布局常用属性: 子类控件相对子类控件:值是另外一个控件的id android:layout_above-- ...

  9. Android:控件布局(线性布局)LinearLayout

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 决定性属性:必须有的! android:orientation:vertical (垂直方向) .horizontal(水平方向) ...

  10. 矩阵, 矩阵 , Android基础控件之ImageView

    天下文章大家抄,以下所有内容,有来自copy,有来自查询,亦有自己的总结(目的是总结出自己的东西),所以说原创,不合适,说是转载也不恰当,所以我称之为笔记,可惜没有此分类选项,姑且不要脸一点,选择为原 ...

随机推荐

  1. Flask入门HelloWorld

    Flask入门HelloWorld Flask官网:http://flask.pocoo.org/ Flask中文翻译:http://dormousehole.readthedocs.io/en/la ...

  2. 笔记:Jersey REST API 设计

    REST 统一接口 REST 使用 HTTP 协议的通用方法作为统一接口的标准词汇,REST 服务所提供的方法信息都在 HTTP 方法里,每一种HTTP请求方法都可以从安全性和幂等性两方面考虑,这对正 ...

  3. spring boot jsp页面

    相关内容访问: http://www.cnblogs.com/zj0208/p/5985698.html

  4. Spring Boot 定时任务的使用

    @Configuration @EnableScheduling public class ScheduleConfig { private final Logger logger = LoggerF ...

  5. 【Python】 virtualenv虚拟环境建设和管理

    [virtualenv] 用Python开发时面临的一个大问题就是每个项目需要的依赖包不一致.如果是包本身不一样倒还好,如果不同项目需要的是不同版本的包的话就会很麻烦.如果采用统一的系统Python环 ...

  6. 基于jquery的插件开发

    最近在公司做一个项目,由于后台数据太多需要分页显示,在网上找了很多插件都没有找到合适的分页插件,所有的分页插件始终达不到自己想要的效果.由于这个项目也不是很赶,就在网上查找各种资料,自己写一个基于jq ...

  7. 随记PC-win7 64位系统网络连接状态一直转圈、等待状态的异常解决方案

    各位看官好~ 最近电脑也做了下升级,入手个士必得360G的SSD来玩玩,顺便也下个新系统,看看有什么区别,想想顺便升级下系统也是好的,就开始了装机,装系统的路程~~~~~~ 好了不说废话,直接进入主题 ...

  8. Python-turtle库知识小结(python绘图工具)

    turtle:海龟(海龟库) Turtle库是Python语言中一个很流行的绘制图像的函数库 使用之前需要导入库:import turtle • turtle.setup(width,height,s ...

  9. JavaScript(第三十一天)【JSON】

    前两章我们探讨了XML的结构化数据,但开发人员还是觉得这种微型的数据结构还是过于烦琐.冗长.为了解决这个问题,JSON的结构化数据出现了.JSON是JavaScript的一个严格的子集,利用JavaS ...

  10. 结对作业NO.2

    结对NO.2 1. 引言 1.1 项目地址 github 生成的一组好数据 1.2 项目简介 按照老师给的项目要求:"编码实现一个部门与学生的智能匹配的程序".由于数据需要自己生成 ...