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. svg格式的中国地图轮廓图

    <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C ...

  2. ASP.NET TextBox 当鼠标点击后清空默认提示文字

    ASP.NET TextBox 当鼠标点击后清空默认提示文字 [ 方法一] 前台代码: <div>    <asp:TextBox ID="txtName" ru ...

  3. 1ms引发的问题

    最近在跟SQLServer数据库进行交互的时候发现一个奇怪的问题,在往数据库里边插入日期型数据的时候,在C#里面赋值的为 2014/05/19 23:59:59,但是存到数据库里边就变成了2014/0 ...

  4. Ubuntu上安装Robomongo及添加到启动器

    到目前为止,Robomongo仍是MongoDB最好的客户端管理工具,如需在Ubuntu上安装Robomongo,可直接从官网下载.tar.gz压缩包进行解压,然后直接运行bin目录下的robomon ...

  5. eclipse启动时报告错误:Java was started but returned exit code=-805306369

    这两天也没改过eclipse和java的配置,但eclipse启动时报告错误:Java was started but returned exit code=-805306369 后来在eclipse ...

  6. How To Use Goto?

    看到,网上很多人对于goto的询问, 因为本身在工作中经常使用到,所以写下此文, 如有错误, 请指出. 本人写博文的时候主要从事C++工作 对于goto的态度,本人目前成长如下: 学生时代 老师课堂上 ...

  7. Oracle 创建普通用户,并赋予权限

    采用sys or system / manager as sysdba; 连接数据库. 创建普通用户konglin: create user konglin identified by pwd_ora ...

  8. slave IO流程之一:mysql登陆过程(mysql_real_connect)

    最近看了slave IO的源码,发现slave IO的写relay log貌似是单线程单连接的,这让我有点小失望. slave IO的主函数是handle_slave_io,处理流程如下: 图1 ha ...

  9. jQuery对象与DOM对象之间的转换方法

    刚开始学习jquery,可能一时会分不清楚哪些是jQuery对象,哪些是DOM对象.至于DOM对象不多解释,我们接触的太多了,下面重点介绍一下jQuery,以及两者相互间的转换. 什么是jQuery对 ...

  10. 模拟jsonp的实现

    function prescript(s) { if (s.cache === undefined) { s.cache = false; } if (s.crossDomain) { s.type ...