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. Linux入门之路

    一.linux简介 Linux前身:Minix,由Andrew S. Tanenbaum教授参考Unix编写 Linux创始人:Linus Torvalds Linux内核版(只有内核)和发行版(在内 ...

  2. windows和linux中搭建python集成开发环境IDE——如何设置多个python环境

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  3. u盘写入Ubuntu后容量变小,恢复方式

    具体请参考网址:http://jingyan.baidu.com/article/59703552e754e48fc00740ed.html 经过验证,方法是可以的

  4. java中类的加载情况

    当一个类存在继承且与其他类有关联情况时,类中各模块加载顺序如下: 1.首先找到public类,判断该类是否继承其他类,如果没有继承其他类(Object类除外),则加载该类:否则转去加载该类的超类,超类 ...

  5. CYQ.Data 支持WPF相关的数据控件绑定(2013-08-09)

    事件的结果 经过多天的思考及忙碌的开发及测试,CYQ.Data 终于在UI上全面支持WPF,至此,CYQ.Data 已经可以方便支持wpf的开发,同时,框架仍保留最低.net framework2.0 ...

  6. Android安全开发之安全使用HTTPS

    Android安全开发之安全使用HTTPS 1.HTTPS简介 阿里聚安全的应用漏洞扫描器中有证书弱校验.主机名弱校验.webview未校验证书的检测项,这些检测项是针对APP采用HTTPS通信时容易 ...

  7. java中文乱码解决之道(二)-----字符编码详解:基础知识 + ASCII + GB**

    在上篇博文(java中文乱码解决之道(一)-----认识字符集)中,LZ简单介绍了主流的字符编码,对各种编码都是点到为止,以下LZ将详细阐述字符集.字符编码等基础知识和ASCII.GB的详情. 一.基 ...

  8. 七天学会ASP.NET MVC (五)——Layout页面使用和用户角色管理

    系列文章 七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC 七天学会ASP.NET MVC (二)——ASP.NET MVC 数据传递 七天学会ASP.NET MVC (三)— ...

  9. Npm install failed with “cannot run in wd”

    Linux环境下,root账户,安装某些npm包的时候报下面的错误,例如安装grunt-contrib-imagemin时: Error: EACCES, mkdir '/usr/local/lib/ ...

  10. [译]libev和libevent的设计差异

    本文译自what's the difference between libev and libevent? 作者是libev作者 [问]两个库都是为异步io调度而设计,在Linux上都是使用epoll ...