RadioGroup、RadioButton、CheckBox、Toast用法

  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();
      }
    }
  }

时间: 2024-08-05 02:02:50

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

Android高级UI ImageView ImageButton RadioButton CheckBox ProgressBar属性和用法总结

高级UI ImageView  ImageButton  RadioButton  CheckBox  ProgressBar 1.ImageView 图片组件 src 指定要加载的图片 缩放问题 1.按着图片原始比例(不失真) 2.不按着比例(失真) ScaleType 1.fitXY   强制让图片缩放以填充整个imageview 2.fitCenter  按着比例缩放以居中显示图片 3.fitEnd     按着比例缩放以局下部显示图片 4.fitStart   按着比例缩放以居上部显示图

android基本控件学习-----RadioButton&amp;CheckBox

RadioButton(单选框)和CheckBox(复选框)讲解: 一.基本用法和事件处理 (1)RadioButton单选框,就是只能选择其中的一个,我们在使用的时候需要将RadioButton放到RadioGroup中使用,同时我们还可以在RadioGroup中设置  orientation属性来控制单选框的方向. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:androi

ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast

一.代码 1.xml(1)radio.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_

Android学习笔记:常用控件 RadioGroup和CheckBox

RadioGroup和CheckBox是android的常用控件,本文自做简单介绍和学习笔记,所以所用的控件样式选用android默认的样式. 先看下代码实现的效果图 图中,上面两个(male和female)为一个RadioGroup中的两个RadioButton,下面三个为CheckBox. 一个RadioGroup里面的内容只可单选,CheckBox可多选. 接下来是代码部分 布局文件代码activity_main.xml : <LinearLayout xmlns:android="

android单选按钮选择,RadioGroup,radioButton

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

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

原文:背水一战 Windows 10 (33) - 控件(选择类): ListBox, RadioButton, CheckBox, ToggleSwitch [源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) ListBox RadioButton CheckBox ToggleSwitch 示例1.ListBox 的示例Controls/SelectionControl/ListBoxDemo.xaml <Page x:Class="Window

radio和checkBox的用法:

radio和checkBox的用法: 配置内容: (function(){ Ext.onReady(function(){ new Ext.form.Panel({ title:'Ext.form.field.checkBox和Ext.form.field.Radio示例', bodyStyle:'padding 5 5 5 5 ', frame:true, height:150, width:400, renderTo:'form', defaults:{ labelSeparator:':'

Android RadioGroup/RadioButton

RadioGroup RadioButton的集合,提供多选一的机制 属性: android:orientation="horizontal/vertical" vertical     垂直排布 horizontal     水平排布          决定当前RadioGroup中RadioButton以什么形式排列 private RadioGroup rg ; @Override protected void onCreate(Bundle savedInstanceState

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

  和按钮类似,这里采用cb1.setOnCheckedChangeListener(this);方法分别对3个CheckBox进行CheckChange事件绑定,然后在onCheckedChanged抽象函数中对点击CheckBox的状态进行获取并用Toast显示. 1 //使用状态改变检查监听器 2 public class MainActivity extends Activity implements OnCheckedChangeListener { 3 private CheckBo