xml布局文件如下:

  <RadioGroup
    android:id="@+id/sex"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/number2"
    android:orientation="vertical">
    <RadioButton
      android:id="@+id/female"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="女"/>
    <RadioButton
      android:id="@+id/male"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="男"/>
  </RadioGroup>
  <CheckBox
    android:id="@+id/swim"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/sex"
    android:text="游泳"/>
  <CheckBox
    android:id="@+id/football"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/swim"
    android:text="足球"/>

MainActivity.java的OnCreate方法中相应的代码如下:

    genderGroup = (RadioGroup)findViewById(R.id.sex);
    femaleButton = (RadioButton)findViewById(R.id.female);
    maleButton = (RadioButton)findViewById(R.id.male);
    genderGroup.setOnCheckedChangeListener(new GenderGroupListener());
    swimBox = (CheckBox)findViewById(R.id.swim);
    footBallBox = (CheckBox)findViewById(R.id.football);
    swimBox.setOnCheckedChangeListener(new HobbykBoxListener());
    footBallBox.setOnCheckedChangeListener(new HobbykBoxListener());

  定义genderGroup、CheckBox的监听器,注意二者的监听器的参数不同:

  class GenderGroupListener implements OnCheckedChangeListener{

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
      // TODO Auto-generated method stub
      //group点击的组的对象,checkedId组中的RadioButton对象的ID
      if(femaleButton.getId() == checkedId){
        Toast.makeText(MainActivity.this, "女", Toast.LENGTH_SHORT).show();
      }
      else if(maleButton.getId() == checkedId){
        Toast.makeText(MainActivity.this, "男", Toast.LENGTH_SHORT).show();
      }
    }
  }

  class HobbykBoxListener implements android.widget.CompoundButton.OnCheckedChangeListener{

    @Override
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
      // TODO Auto-generated method stub
      //isChecked是否选中,如果选中则传入真,否则传入假
      if(isChecked){
        Toast.makeText(MainActivity.this, buttonView.getText().toString(), Toast.LENGTH_SHORT).show();
      }
    }
  }

RadioGroup、RadioButton、CheckBox、Toast用法的更多相关文章

  1. android基本控件学习-----RadioButton&CheckBox

    RadioButton(单选框)和CheckBox(复选框)讲解: 一.基本用法和事件处理 (1)RadioButton单选框,就是只能选择其中的一个,我们在使用的时候需要将RadioButton放到 ...

  2. 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch

    [源码下载] 背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch 作者:webabcd 介绍背水一 ...

  3. android单选按钮选择,RadioGroup,radioButton

    android单选按钮选择,RadioGroup,radioButton 14. 四 / android基础 / 没有评论   单选布局绑定 如何识别选择

  4. Android RadioGroup/RadioButton

    RadioGroup      RadioButton的集合,提供多选一的机制      属性:   android:orientation="horizontal/vertical&quo ...

  5. 重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, RadioButton, CheckBox, ToggleSwitch

    原文:重新想象 Windows 8 Store Apps (2) - 控件之按钮控件: Button, HyperlinkButton, RepeatButton, ToggleButton, Rad ...

  6. JSTL select和checkbox的用法

    select的 用法 <select id="roleIds" name="roleIds" multiple="true" back ...

  7. [Android]--RadioGroup+RadioButton实现底部导航栏

    RadioGroup+RadioButton组合方式打造简单实用的底部导航栏 代码块: <?xml version="1.0" encoding="utf-8&qu ...

  8. [安卓] 4、CheckBox、RadioButton和Toast简单用法

      和按钮类似,这里采用cb1.setOnCheckedChangeListener(this);方法分别对3个CheckBox进行CheckChange事件绑定,然后在onCheckedChange ...

  9. Android checkbox和radiobutton 以及Toast和AlertDialog的使用

    package com.example.radiobutton_01; import android.app.Activity; import android.os.Bundle; import an ...

随机推荐

  1. PDF转JPG

    问题:对于上次解决PB调用不起PDF插件的问题,所以将PDF转换为HTML.但是转换完之后发现一个问题,pdf2htmlEX插件转换出的html只支持IE9以上,IE9以下的都会各式错乱, 所以只能换 ...

  2. ubuntu 安装与开始学习

    下载地址 http://cn.ubuntu.com/download/ 经验: 1.遇到安装问题,首先尝试解读错误,再使用  ./configure --help  不行再上Stack overflo ...

  3. js 预处理用户上传图片

    前几个月闲得无聊写得一段代码,没想最近刚好用上了,在硬盘里翻了半天找回来,还没好好整理直接用上了手机用户拍照上传的图片一般都在1M 到4M 之间,如果只是用作头像尺寸就可以缩小很多,1M甚至几M的图转 ...

  4. 初识Windows程序

    首先,我们创建第一个Windows程序,一共分为4个步骤: 1.打开Visual Studio开发工具 2.选择"文件"→"新建"→"项目" ...

  5. Unity小游戏制作 - 暗影随行

    用Unity制作小游戏 - 暗影惊吓 最近玩了一个小游戏,叫做暗影惊吓,虽然是一个十分简单的小游戏,但是感觉还是十分有趣的.这里就用Unity来实现一个类似的游戏. 项目源码:DarkFollow 主 ...

  6. 单元测试模拟框架:Nsubstitute

         Nsubstitute是一个开源的框架,源码是C#实现的.你可以在这里获得它的源码:https://github.com/nsubstitute/NSubstitute NSubstitut ...

  7. Linux 挂载管理(mount)

    标签:mount,umount 概述 在上一章增加linux操作系统空间中已经使用过了mount命令对分区进行挂载,这一章详细介绍挂载管理,该命令涉及的知识点也挺多的而且也还比较重要,是需要掌握的一个 ...

  8. ANDROID下面的游戏更新目录

    更新模块的整体方案终于搞定了,包括launcher的自更新,以及framework,app等代码的更新,均测试通过. 很激动地拿到ANDROID上去测试,一下就傻眼了. 在创建upd目录的时候,就遇上 ...

  9. fir.im Log Guru 正式开源,快速找到 iOS 应用无法安装的原因

    很开心的宣布 Log Guru 正式开源! Log Guru,是 fir.im 开发团队创造的小轮子,用在 Mac 电脑上的日志获取,Github 地址:FIRHQ/LogGuru. Log Guru ...

  10. JAVAScript控制多个下拉框

    方法:获取多选下拉框对象数组→循环判断option选项的selected属性(true为选中,false为未选中)→使用value属性取出选中项的值.实例演示如下: 这个东西我是在百度上查的,我为了方 ...