该工程的功能实现在一个activity中显示一个单选框和一个多选框

以下代码是MainActivity.java文件中的代码

  1. package com.example.checkbox;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.widget.CheckBox;
  8. import android.widget.CompoundButton;
  9. import android.widget.RadioButton;
  10. import android.widget.RadioGroup;
  11. import android.widget.Toast;
  12.  
  13. public class MainActivity extends Activity {
  14. //对控件对象进行声明
  15. private RadioGroup gendergroup = null;
  16. private RadioButton femaleButton = null;
  17. private RadioButton maleButton = null;
  18. private CheckBox swimBox = null;
  19. private CheckBox runBox = null;
  20. private CheckBox readBox = null;
  21.  
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. //通过控件的ID来得到代表控件的对象
  27. gendergroup = (RadioGroup)findViewById(R.id.genderGroup);
  28. femaleButton = (RadioButton)findViewById(R.id.femaleButton);
  29. maleButton = (RadioButton)findViewById(R.id.maleButton);
  30. swimBox = (CheckBox)findViewById(R.id.swim);
  31. runBox = (CheckBox)findViewById(R.id.run);
  32. readBox = (CheckBox)findViewById(R.id.read);
  33. //设置监听器
  34. gendergroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  35.  
  36. @Override
  37. public void onCheckedChanged(RadioGroup group, int checkedId) {
  38. // TODO Auto-generated method stub
  39. if(femaleButton.getId() == checkedId){
  40. System.out.println("female");
  41. Toast.makeText(MainActivity.this, "female", Toast.LENGTH_SHORT).show();
  42. }
  43. else if(maleButton.getId() == checkedId)
  44. {
  45. System.out.println("male");
  46. Toast.makeText(MainActivity.this, "male", Toast.LENGTH_SHORT).show();
  47. }
  48. }
  49. });
  50. //为多选按钮添加监听器
  51. swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  52.  
  53. @Override
  54. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  55. // TODO Auto-generated method stub
  56. if(isChecked)
  57. {
  58. System.out.println("swim is checked");
  59. }
  60. else
  61. {
  62. System.out.println("swim is unchecked");
  63. }
  64. }
  65. });
  66. runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  67.  
  68. @Override
  69. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  70. // TODO Auto-generated method stub
  71. if(isChecked)
  72. {
  73. System.out.println("run is checked");
  74. }
  75. else
  76. {
  77. System.out.println("run is unchecked");
  78. }
  79. }
  80. });
  81. readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  82.  
  83. @Override
  84. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  85. // TODO Auto-generated method stub
  86. if(isChecked)
  87. {
  88. System.out.println("read is checked");
  89. }
  90. else
  91. {
  92. System.out.println("read is unchecked");
  93. }
  94. }
  95. });
  96. }
  97. }

以下代码是activity_main.xml文件中的代码

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/LinearLayout1"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical"
  7. tools:context="${relativePackage}.${activityClass}" >
  8.  
  9. <TextView
  10. android:id="@+id/textView1"
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:text="@string/hello_world" />
  14.  
  15. <RadioGroup
  16. android:id="@+id/genderGroup"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:orientation="vertical"
  20. >
  21.  
  22. <RadioButton
  23. android:id="@+id/femaleButton"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="@string/female"
  27. />
  28.  
  29. <RadioButton
  30. android:id="@+id/maleButton"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:text="@string/male"
  34. />
  35. </RadioGroup>
  36.  
  37. <CheckBox
  38. android:id="@+id/swim"
  39. android:layout_width="wrap_content"
  40. android:layout_height="wrap_content"
  41. android:text="@string/swim"
  42. />
  43. <CheckBox
  44. android:id="@+id/run"
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content"
  47. android:text="@string/run"
  48. />
  49. <CheckBox
  50. android:id="@+id/read"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:text="@string/read"
  54. />
  55.  
  56. </LinearLayout>

以下代码是string.xml文件中的代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.  
  4. <string name="app_name">CheckBox</string>
  5. <string name="hello_world">Hello world!</string>
  6. <string name="female"></string>
  7. <string name="male"></string>
  8. <string name="swim">游泳</string>
  9. <string name="run">跑步</string>
  10. <string name="read">读书</string>
  11. </resources>

Android学习笔记——CheckBox的更多相关文章

  1. 【转】 Pro Android学习笔记(十九):用户界面和控制(7):ListView

    目录(?)[-] 点击List的item触发 添加其他控件以及获取item数据 ListView控件以垂直布局方式显示子view.系统的android.app.ListActivity已经实现了一个只 ...

  2. Android 学习笔记之Volley(七)实现Json数据加载和解析...

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  3. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  4. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

  5. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  6. udacity android 学习笔记: lesson 4 part b

    udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

  7. Android学习笔记36:使用SQLite方式存储数据

    在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...

  8. Android学习笔记之Activity详解

    1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...

  9. Pro Android学习笔记 ActionBar(1):Home图标区

     Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...

随机推荐

  1. FTP服务器

    FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为"文传协议".用于Internet上的控制文件的双向传输. 下载"文件就是从 ...

  2. 天气预报API获取

    1.citycode: http://mobile.weather.com.cn/js/citylist.xml http://files.cnblogs.com/files/ys-wuhan/cit ...

  3. Linux vsftp配置本地用户

    主要讲的是配置本地用户, ftp现在用的也少了,一般都用ssh和svn 1. 安装ftp  yum -y install vsftpd 2. 配置 /etc/vsftpd/vsftpd.conf # ...

  4. HV和VM 内存性能测试对比结果

    安装工具: apt-get install mbw 使用 mbw 1024 测试 VM ubuntu12.04 : ldd --versionldd (Ubuntu EGLIBC 2.15-0ubun ...

  5. win7如何让局域网其他电脑通过IP直接访问自己电脑的网站

    一.打开控制面板 二.打开防火墙 三.点击右侧高级设置 四.点击防火墙属性 五.点击防火墙状态选择为关闭,确定 六.点击右侧允许或功能通过windows防火墙 七.执行第六部会打开防火墙通信例外窗口, ...

  6. 一个最简单的ftpsever

    没有什么事情可以做,无聊的很 写个最简单的ftp吧---说白了就是一个简单的文件上传.QAQ 思路:client --读取文件的一行 然后发到server端 然后server 读取 写入文件的一行 先 ...

  7. Java并发和多线程(一)基础知识

    1.java线程状态 Java中的线程可以处于下列状态之一: NEW: 至今尚未启动的线程处于这种状态. RUNNABLE: 正在 Java 虚拟机中执行的线程处于这种状态. BLOCKED: 受阻塞 ...

  8. hadoop报错:WARN mapred.JobClient: Error reading task outputNo route to host

    解决方案: /etc/sysconfig/network/etc/hosts$hostname 这三处的主机名都要一样. 具体参考:http://blog.itpub.net/28254374/vie ...

  9. Leetcode Find Minimum in Rotated Sorted Array I and II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  10. 【poj1160】 Post Office

    http://poj.org/problem?id=1160 (题目链接) 题意 按照递增顺序给出一条直线上坐标互不相同的n个村庄,要求从中选择p个村庄建立邮局,每个村庄使用离它最近的那个邮局,使得所 ...